Add the -id option to your MenuItems. In the code below, with -id commented out, the program behaves as you described, with the added bug of reversing the order of Database Setup and Exit. If the -id lines are uncommented, then the program behaves properly.
use strict;
use warnings;
use Win32::GUI;
my $next_menu_id = 1;
my $menuMW = Win32::GUI::Menu->new();
my $btnMenu = $menuMW->AddMenuButton(
-text => 'Configuration',
# -id => $next_menu_id++,
);
my $itmDBSetup = $btnMenu->AddMenuItem(
-text => 'Database Setup',
# -id => $next_menu_id++,
-onClick => sub {
print "I'm the first OnClick!\n";
},
);
my $itmQuit = $btnMenu->AddMenuItem(
-text => 'Exit',
# -id => $next_menu_id++,
-onClick => sub {
print "I'm the second OnClick, and I Quit!\n";
return -1;
},
);
my $MainWindow = GUI::Window->new(
-text => 'Main',
-name => 'MainWindow',
-pos => [ 10, 10 ],
-size => [ 900, 900 ],
-menu => $menuMW,
);
$MainWindow->Show();
Win32::GUI::Dialog();
sub MainWindow_Terminate {
return -1;
}
Yet Another (better, but woefully underdocumented) Way To Do It:
use strict;
use warnings;
use Win32::GUI;
# Adapted from:
# http://www.perlmonks.org/index.pl?node_id=18472
my $menuMW = Win32::GUI::Menu->new(
'&Configuration' => 'Config',
' > &Database Setup' => 'DB_Setup',
' > E&xit' => 'Exit',
'Second&TopLevel' => 'STL',
' > &foo' => 'Foo',
' > &bar' => 'Bar',
);
my $MainWindow = GUI::Window->new(
-text => 'Main',
-name => 'MainWindow',
-pos => [ 10, 10 ],
-size => [ 900, 900 ],
-menu => $menuMW,
);
$MainWindow->Show();
Win32::GUI::Dialog();
sub MainWindow_Terminate {
return -1;
}
sub DB_Setup_Click {
print "DB_Setup.\n";
# displayDlgDBSetup( $MainWindow );
}
sub Exit_Click {
print "I am exiting via the 'Exit' menu option.\n";
return -1;
}
sub Foo_Click { print "Foo.\n"; }
sub Bar_Click { print "Bar.\n"; }
Using this method, Win32::GUI::Menu internally calls MakeMenu, which automatically handles assigning -id. You cannot use OnClick with this method, though; you must use FOO_Click event procedures.
|