View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Laurent Rocher > Win32-GUI-1.0 > docs/tut/guitut4.pod


Annotate this POD


Open  0
View/Report Bugs

Win32::GUI Tutorial - Part 4 - Further Features of Win32::GUI ^


Many types of application need to be able to perform an action at regular intervals. The obvious example is a clock application, but other examples might be

Adding a timer to your application is easy. All you do is call the AddTimer() method on the window you want to "own" the timer. When the timer expires, its Timer event is fired, which you can catch in the normal way.

For example,

    $t1 = $main->AddTimer('T1', 1000);

    sub T1_Timer {
        print "Timer went off!\n";

Some things to note:

That's about all there is to timers.

Status Bars

To add a status bar to your window, just use

    $sb = $main->AddStatusBar();

The normal options are available, but in general you don't need them.

The only surprise is that your status bar will not resize automatically when your main window resizes. You need to include code in your main window resize event handler to resize the status bar. The following code will do the job:

    sub Main_Resize {
        $sb->Move(0, $main->ScaleHeight - $sb->Height);
        $sb->Resize($main->ScaleWidth, $sb->Height);

You can write text to your status bar using the Text() method.

    $sb->Text("This appears in the status bar");

To clear the status bar, just write an empty string.

System Tray Icons

Many utility programs these days add an icon to the Windows "System Tray" - the small area on the taskbar near the clock. Once again, this is easy with Win32::GUI - you simply use the AddNotifyIcon() method. A notify icon has four key properties - a name (which is used for event handling, just like for any other Win32::GUI object), an ID (which is just a unique number, used internally, you specify it and then ignore it), a tooltip (a string which is displayed when you hold the mouse pointer over the icon) and an icon (a Win32::GUI::Icon object - you create this using the new() constructor, passing the name of the .ico file to use). Notify icons have Click and RightClick events, to let you process mouse clicks.

The normal protocol for an application which uses a notify icon is for the main window to start hidden, and to show the window when the icon is clicked. When the main window is minimised, it hides itself, leaving just the notify icon visible.

The simplest way to demonstrate this is to show some working code...

    use Win32::GUI;

    $main = Win32::GUI::Window->new(-name => 'Main', -text => 'Perl',
                                    -width => 200, -height => 200);

    $icon = new Win32::GUI::Icon('GUIPERL.ICO');
    $ni = $main->AddNotifyIcon(-name => "NI", -id => 1,
                               -icon => $icon, -tip => "Hello");


    sub Main_Terminate {

    sub Main_Minimize {

    sub NI_Click {

Some points to note

That's it for the simpler objects available in the Win32::GUI package. Even though I have referred to them as simple, it is possible to create some fairly complicated applications using just what we have seen so far.

In the next couple of tutorials, I will move on to the more sophisticated controls available - list views and tree views.

syntax highlighting: