The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Win32::GUI::Tutorial::Part4 - Further Features of Win32::GUI

Win32::GUI Tutorial - Part 4

Timers

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

  • A file viewer, which watches at regular intervals to see if the file being displayed has changed, and shows the new contents if it has.

  • A monitor application (like the WIndows NT performance monitor) which displays updated information every second.

  • A network "ping" utility, which polls a remote server at regular intervals, to see that it is still running.

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:

  • The AddTimer() method takes two explicit parameters, the timer name and the interval (in milliseconds). The normal option => value syntax is not used.

  • The timer keeps firing repeatedly. To disable a timer, use $timer->Interval(0) or $timer->Kill().

  • To change the interval of a timer (or to re-enable it after $timer->Interval(0) or $timer->Kill()), use $timer->Interval(n). Setting an interval of zero disables the timer, just like Kill.

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 three key properties - a name (which is used for event handling, just like for any other Win32::GUI object), 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();

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

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

    Win32::GUI::Dialog();

    sub Main_Terminate {
        return -1;
    }

    sub Main_Minimize {
        $main->Disable();
        $main->Hide();
        return 1;
    }

    sub NI_Click {
        $main->Enable();
        $main->Show();
        return 1;
    }

Some points to note

  • To remove the window's icon from the taskbar, it is necessary to diable the window as well as hiding it. And hence, when we show the window again, we need to re-enable it.

  • If you want to remove the tray icon before the end of your program you can use this line:

            $main->NI->Remove();

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. On to Part 5.

VERSION

Documentation for Win32::GUI v1.11 created 08 Nov 2014

This document is autogenerated by the build process. Edits made here will be lost. Edit docs/GUI/Tutorial/Part4.pod instead.

SUPPORT

Homepage: http://perl-win32-gui.sourceforge.net/.

For further support join the users mailing list from the website at http://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users. There is a searchable list archive at http://sourceforge.net/p/perl-win32-gui/mailman/perl-win32-gui-users/.

COPYRIGHT and LICENCE

Copyright (c) 1997..2014 Aldo Calpini. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.