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

I can't seem to get a Win32::GUI popup menu to invoke its callback/event handlers. The following program puts up a popup menu with two entries: 'foo' and 'bar'. When the user clicks one, it should print "GOTCHA foo" or "GOTCHA bar" but doesn't. What am I doing wrong? Thanks.
use strict; use Win32::GUI (); $|=1; my $window = Win32::GUI::Window->new(); my $menu = new Win32::GUI::Menu( "mymenu" => "mymenu", " > foo" => "foo", " > bar" => { -name => 'bar', -onClick => \&bar_onclickcallback + }, ); print "Start Tracking\n"; $window->TrackPopupMenu($menu->{mymenu}, 20,20); print "Done Tracking, if it didn't print 'GOTCHA ...' then no callback +?\n"; sub bar_onclickcallback { print "GOTCHA bar\n"; return 1; } sub foo_Click { print "GOTCHA foo\n"; return 1; }
Perl version:
This is perl, v5.10.1 built for MSWin32-x86-multi-thread + (with 2 registered patches, see perl -V for more detail) + + Copyright 1987-2009, Larry Wall + + Binary build 1007 [291969] provided by ActiveState http://www.ActiveSt +ate.com Built Jan 26 2010 23:15:11

Replies are listed 'Best First'.
Re: Win32::GUI Popup Menu no callback
by Anonymous Monk on Jun 28, 2010 at 03:32 UTC
Re: Win32::GUI Popup Menu no callback
by kejohm (Hermit) on Jun 28, 2010 at 08:47 UTC

    You could try using a callback event when calling TrackPopupMenu(), like so:

    $window->TrackPopupMenu( $menu->{mymenu}, 20, 20, 0, 0, 0, 0, TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON, \&callback ); sub callback { my ($self, $message, $wParam, $lParam) = @_; # Process message here... return 1; }

    See the docs for more info.

      That is workable (if you define/import TPM_LEFTALIGN | TPM_TOPALIGN | TPM_LEFTBUTTON,), or call  Win32::GUI::Dialog(); as per Win32::GUI::Tutorial::Part1

        It depends on what you are doing, but in most cases, I can't see why you wouldn't be using Win32::GUI::Dialog() anyway.

        Here is the above code modified to use Win32::GUI::Dialog():

        use strict; use Win32::GUI (); $|=1; my $window = Win32::GUI::Window->new(); my $menu = new Win32::GUI::Menu( "mymenu" => "mymenu", " > foo" => "foo", " > bar" => { -name => 'bar', -onClick => \&bar_onclickcallback + }, ); print "Start Tracking\n"; $window->TrackPopupMenu($menu->{mymenu}, 20,20); Win32::GUI::Dialog(); # enter Dialog() here print "Done Tracking, if it didn't print 'GOTCHA ...' then no callback +?\n"; sub bar_onclickcallback { print "GOTCHA bar\n"; return -1; # return -1 here to exit from dialog loop } sub foo_Click { print "GOTCHA foo\n"; return -1; # return -1 here to exit from dialog loop }