Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Win32::GUI problem

by Anonymous Monk
on Sep 22, 2015 at 11:04 UTC ( [id://1142719]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, i am trying to make a fractalgenerator with the Win32::GUI module. I am trying to catch the mouse's x and y coordinates with the statement:

(x,y) = Win32::GUI::GetCursorPos();

This works fine but my problem is that i don't know how to trigger this code. I tried to use this subroutine:

sub MouseUp{ Win32::GUI::GetCursorPos();}
and i tried simply:
MouseUp{(x,y) = Win32::GUI::GetCursorPos();} etc.

Can someone please help me out here? I cant find good documentation about this item, and im temporarily without internet access. I am on a library computer. thanks.

Replies are listed 'Best First'.
Re: Win32::GUI problem
by Corion (Patriarch) on Sep 22, 2015 at 11:10 UTC

    Most likely, you need to connect the MouseUp subroutine to some event. See the Events section in the documentation. Most likely you will want to write something like sub MyWindow_MouseUp, but it's been a long time since I last did Win32::GUI stuff.

      Tx for the fast reply.

        Fast? That is not a goal. Try fract
Re: Win32::GUI problem
by james28909 (Deacon) on Sep 22, 2015 at 20:39 UTC
    You should look in the perl/site/bin folder for 'win32-gui-demos.pl'. Whenever I run into a problem with how something is suppose to work, I usually get the most help from looking through the examples. I use WxPerl, but I also tinkered with win32::gui a while ago and it has a demo module in the folder mentioned above, same for wxperl also. Good luck :)

      I found this example at Win32::GuiTest::Examples called showmouse.pl. It didn't work at first until I either added $|++ or printed to STDERR.

      #!/usr/bin/perl # This script has been written by Jarek Jurasz jurasz@imb.uni-karlsruh +e.de #$|++; ## use this if you don't want to use STDERR use Win32::GuiTest qw(GetCursorPos); while (1) { my ($x, $y) = GetCursorPos(); print STDERR "\rx:$x y:$y -"; sleep 1; }
Re: Win32::GUI problem
by dasgar (Priest) on Sep 22, 2015 at 14:29 UTC

    It's been quite a while since I've used Win32::GUI and you're trying to do something that I have not tried doing before. Looking at the common events documentation for the Win32::GUI module, here's a few untested ideas:

    • Use the MouseOver event to detect when the mouse is over the desired window object to trigger your code to get the mouse position.
    • Use the MouseMove event to detect when the mouse moves, which in turn triggers your mouse position code.
    • Use one of the click related events so that a mouse click is what triggers your code to get the mouse position.
    • I'm not sure that I like this idea, but you might even be able to use a Win32::GUI::Timer object to trigger your mouse position code at a set interval.
Re: Win32::GUI problem
by Albannach (Monsignor) on Sep 24, 2015 at 03:33 UTC
    You can catch mouse events from your window and then do whatever you want in the assigned subs:
    use strict; use warnings; use Win32::GUI; my $draw = 0; my $mw = Win32::GUI::Window->new( -title => "Click anywhere to draw", -pos => [100,100], -size => [500,500], -onMouseDown => \&down, -onMouseUp => \&up, -onMouseMove => \&move, ); my $DC = $mw->GetDC; $mw->Show(); Win32::GUI::Dialog(); exit(0); sub down { my ($object, $x, $y) = @_; $draw = 1; $DC->TextOut(10,10,"start $x,$y"); return 1; } sub up { my ($object, $x, $y) = @_; $draw = 0; $DC->TextOut(400,10,"end $x,$y"); return 1; } sub move { my ($object, $x, $y) = @_; return unless $draw; $DC->SetPixel($x,$y,[192,192,192]); $DC->TextOut(200,10,"at $x,$y"); return 1; }

    --
    I'd like to be able to assign to an luser

      Tx a lot.

      Your code is not working.

        Can you elaborate on what you're seeing? I always test code before posting so I can tell you this works on my machine at least. To be clear, you should get a square white window, and when you left-mouse-down in the window it will report the coordinates in the top left. If you hold left-mouse-down and move the mouse it will draw pixels and report the current coordinates in the top centre, and when you release the left-mouse it will report the final coordinates in the top right.

        --
        I'd like to be able to assign to an luser

Re: Win32::GUI problem
by Anonymous Monk on Sep 22, 2015 at 12:57 UTC

    Its not working, Im trying

     Main_MouseUp{}

    but the code is not being executed, although it doesnt raise an error.

      Can you maybe show us a short (20 lines) self-contained program that demonstrates the problem?

      No, I can't

        If you can't show us the code, consider working on reducing the code you have until it consists of little more than

        use Win32::GUI; # create main window # connect MouseUp handler to events of the window $main->Show();

        Most likely while reducing your program to this minimal example, you will find where the error lies.

        If you can't even do that, we cannot help you either.

Re: Win32::GUI problem
by Anonymous Monk on Sep 23, 2015 at 09:35 UTC

    Tx for the info. Must be sufficient.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1142719]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-23 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found