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

Hey everyone, I'm fairly new to perl and experimenting with the Win32::GUI module. This is probably very easy but I can't get it to work. I'm trying to make an 'about' messagebox (providing info about the program version) appear when clicking on a menu item of a window. This is my code so far: First making the menu
use strict; use Win32::GUI(); my $Menu_id=1; my $Menu = new Win32::GUI::Menu(); my $MenuButton1 = $Menu->AddMenuButton( -text => "Menu", -id => $Menu_id++, ); my $MenuButton1_Quit = $MenuButton1->AddMenuItem( -text => "Quit", -id => $Menu_id++, -onClick => sub{print"program closed\n"; return -1;}, ); my $MenuButton1_About = $MenuButton1->AddMenuItem( -text => "About", -id => $Menu_id++, -onClick => $MessageBox_About->DoModal(), );
Then making the first window and include the menu
my $Main = new Win32::GUI::Window( -name => "MainWindow", -title => "Main Window", -pos => [ 100, 100 ], -size => [ 300, 200 ], -menu => $Menu, ); $Main->AddLabel( -name => "Label1", -text => "Welcome", -pos => [12,10], );
Then the messagebox showing the about info. Since there is no actual MessageBox() I just made a window with a label and a close button
my $MessageBox_About = new Win32::GUI::Window( -name => "AboutWindow", -title => "About this program", -pos =>[ 150, 150 ], -size => [150,150], -parent => $Main, ); $MessageBox_About->AddLabel( -name => "MsgBox_Label", -text => "Blabla", -pos => [20,20], ); $MessageBox_About->AddButton( -name => "MsgBox_Button", -text => "Ok", -pos => [50,40], ); sub MsgBox_Button_Click {return -1;} #closes the messagebox
The problem here of course is that $MessageBox doesn't exist yet when it's called at the MenuButton1 onClick event. The messagebox has the main window as parent and DoModal is used to prevent interaction in the main window while the about window is still visible. I tried an event handler sub
sub MenuButton1_About_Click{$MessageBox_About->DoModal(); return 0;}
But also doesn't work.. How to get this to work?

Replies are listed 'Best First'.
Re: win32::GUI help
by dasgar (Priest) on Oct 15, 2014 at 06:27 UTC

    You may want to look at Win32::GUI's tutorial part 5's section on modal windows. That should help you figure out what you may have wrong with your code.

    As for providing more specific help, it's kind of hard to do that. You've provided code snippets, but not the full set of code. I've got a suspicion on what might be one thing that could potentially cause problems, but can't say for sure without seeing your full code.

    Also, you say that it "doesn't work", but you don't provide details. What doesn't work? What is being displayed? Are there error messages?

    If you can provide a full set of code (preferably a minimum amount of code to reproduce the issue) along with more description of what the problem is (including error messages), that would make it easier for others to try to reproduce what your seeing and provide more exact suggestions on fixing your code. Otherwise, you'll probably will just be getting general recommendations, such as pointers to Win32::GUI documentation.

      The code I provided is the entire program I have written. I made it only to try making a new window appear by clicking on a menu item. Later I will implement it in my main project. It's a program that searches for open reading frames in DNA sequences (bioinformatics course assignment). I thought it would be a cool addition to embed it in a user interface rather than just a plain black/white console. The code above spawns the main window with menubar. This menubar has 1 menubutton (Menu) which has 2 menu items (Quit and About). The Quit item works like it should, it closes the main window. The About item should spawn the secondary MessageBox window, but it does nothing with I click it. I later put the entire messagebox in a sub:
      sub MessageBox{ my $MessageBox_About = new Win32::GUI::Window( -name => "AboutWindow", -title => "About this program", -pos =>[ 150, 150 ], -size => [150,150], -parent => $Main, ); $MessageBox_About->AddLabel( -name => "MsgBox_Label", -text => "Blabla", -pos => [20,20], ); $MessageBox_About->AddButton( -name => "MsgBox_Button", -text => "Ok", -pos => [50,40], ); $MessageBox_About->DoModal(); return 0; }
      And changed MenuButton1_About to:
      my $MenuButton1_About = $MenuButton1->AddMenuItem( -text => "About", -id => $Menu_id++, -onClick => MessageBox(), );
      The about messagebox does appear now (A window with label Blabla and an OK button) but right at the start of the program (without clicking on the about menuitem). After clicking on Ok or terminating the window, the main window pops up. When clicking the about menuitem it gives the following error: Undefined subroutine &main::0 called at xx.pl line 127 (even though the program only has 118 lines).
        Oke after browsing trough the module's demos I found the solution. I only had to change -onClick => MessageBox(), to -onClick => 'MessageBox',
Re: win32::GUI help
by Anonymous Monk on Oct 14, 2014 at 19:14 UTC