So far, in this tutorial, we have been creating our main window using the Win32::GUI::Window->new() constructor. While this works fine, Win32::GUI actually offers two classes of main window.
The first of these is the Win32::GUI::Window we have been using all along, and the second is Win32::GUI::DialogBox.
"Why two classes?", you may ask. Basically, the reason is that a DialogBox adds extra functionality to the window which is not available with a simple Window. But the extra functionality has a cost, which means that we still make WIndow available for people who don't want the extra functionality of a DialogBox, or who don't want to pay the extra cost. (Actually, there's a further reason, which is that a Win32::GUI::DialogBox loses some of the flexibility which a Win32::GUI::Window has - this is a limitation of Win32::GUI, not of Windows itself, and may go away in a future version - there may even be workarounds available already. See below for a more complete picture.
The main advantage that a DialogBox has over a basic Window, is that Win32::GUI automatically performs certain types of keyboard handling for you if you use a DialogBox. (The handling in question is the standard Windows actions - Tab moves between controls, Shift-Tab moves backwards, Escape cancels the dialog, and Return does the "default" action).
To make the keyboard handling work properly, you need to add a number of options to your controls.
To make the tab keys work as expected,
you need to add the
-tabstop => 1 option to your controls.
The tab keys move the focus between the controls with the
-tabstop option set.
Other controls will be ignored when tabbing.
To make the escape key work as expected,
you need to define a button with the
-cancel => 1 option.
When the user presses the escape key,
Windows will translate that action into a
Click event on the cancel button.
It is normal to give this button a caption of
"Cancel" and to make its click event handler close the window without applying any changes - but this is up to you to implement.
Windows just fires the relevant event.
To make the return key work as expected,
you need to give one of your dialog's buttons the
-default => 1 option.
When the return key is pressed,
it is translated into a click event on the default button.
The default button is also highlighted differently from the other buttons (it has a dark border).
Note: There is a related option,
-ok => 1,
which has a similar effect as
the two seem to conflict in their effects.
I would recommend that if your default button has the caption
you should set both the
stick with just
There are other default keyboard actions which occur, but you either don't need to, or can't, write code to handle them.
As well as the keyboard handling, the main difference between a DialogBox and a basic Window is that a DialogBox has a much more limited set of options available for controlling its appearance. Specifically
In most cases, the key issues will be whether you need the keyboard handling, and whether you need the user to be able to resize your windows. Otherwise, the choice is arbitrary. To switch types, the only change you need to make is the name of the constructor you use to create your application's main window.
In the next part, we will cover some further ways in which you can increase your application's functionality.