Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I get OnMouseOver event to work on a label?

by ZJ.Mike.2009 (Scribe)
on Jan 30, 2010 at 13:16 UTC ( [id://820484]=perlquestion: print w/replies, xml ) Need Help??

ZJ.Mike.2009 has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks, I'm trying to modify a code snippet that has MouseOver event on a main window object to work on a label object but somehow it simply doesn't work. Can someone kindly give me some guidance on what I might be doing wrong? Thanks in advance.

use Win32::GUI(); sub TME_HOVER() {1} sub TME_LEAVE() {2} sub HOVER_DEFAULT() {0xFFFFFFFF} $state = 0; $mw = Win32::GUI::Window->new( -title => 'Test', -pos => [170,100], -size => [200,220], ); $label = $mw->AddLabel( -background => [0,0,255], -pos => [ 70, 60 ], -size => [50, 50 ], -onMouseOver => sub { $label->Change(-background => [255,0,0], ); return 0; }, -onMouseOut => sub { $label->Change(-background => [255,0,0], ); $state=0; return 0;}, -onMouseMove => \&Move, ); $mw->Show(); Win32::GUI::Dialog(); exit(0); sub Move { return unless $state == 0; $state = 1; $label->TrackMouse(500,TME_HOVER|TME_LEAVE); return 1; }

Replies are listed 'Best First'.
Re: How do I get OnMouseOver event to work on a label?
by zentara (Archbishop) on Jan 30, 2010 at 15:17 UTC
    I'm not familiar with Win32GUI, but Gtk2 has a similar behavior with Labels. The labels don't have an underlying lowlevel widget window, so they don't respond to mouse movements. In Gtk2, we get around it by putting the label into what is called an event-box.... then the event-box responds.

    Upon googling myself for "Win32GUI label event", it seems the label needs the notify=>1, option. YMMV


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku

      Thanks zentara. Problem solved :)

      Thank, Anonymous Monk :) I finally figured it out. I actually had the notify=>1 option in my original script but it didn't work like expected (didn't change the background color of the label) so I removed it afterwards, thinking it was useless. Now I have the notify=>1 option and after several failed experiments, I now realize I have to add the -text => '' option in my script to make things really work.

      Well, it's kind of weird that in order to change the background color of a label I have to add the quite irrelevant -text => '' option. But it does the trick

      Here's the code that works for me. Please note the -text => '' option in the Change() function.:
      use Win32::GUI(); use constant { TME_HOVER => 1, TME_LEAVE => 2, HOVER_DEFAULT => 0xFFFFFFFF, }; $state = 0; $mw = Win32::GUI::Window->new( -title => 'Test', -pos => [170,100], -size => [200,220], ); $label = $mw->AddLabel( -background => [0,0,255], -pos => [ 70, 60 ], -size => [50, 50 ], -notify=>1, -onMouseOver => sub { $label->Change(-background => [255,0,0],-text=>'', ); return 0; }, -onMouseOut => sub { $label->Change(-background => [0,0,255],-text=>'', ); $state=0; return 0;}, -onMouseMove => \&Move, ); $mw->Show(); Win32::GUI::Dialog(); sub Move { return unless $state == 0; $state = 1; $label->TrackMouse(100,TME_HOVER|TME_LEAVE); return 1; }

      And also thanks for the use constant suggestion :)

Re: How do I get OnMouseOver event to work on a label?
by AnomalousMonk (Archbishop) on Jan 30, 2010 at 19:09 UTC

    Just a matter of idle curiosity, but may I ask why you use
        sub TME_HOVER() {1}
    instead of the constant module, e.g.,
        use constant TME_HOVER => 1;
    or
        use constant {
            TME_HOVER     => 1,
            TME_LEAVE     => 2,
            HOVER_DEFAULT => 0xFFFFFFFF,
            };
    or perhaps better yet, the Readonly module?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found