Perl‎ > ‎Win32::GUI‎ > ‎Scrollbars‎ > ‎

Scrollbars - Part 1 - The Scratch Program

There was a question asked earlier today on the perl-win32-gui-users mailing list about how to use scrollbars on a main window to enable display of more controls than would fit into the client area of the window.

Using scrollbars under the Win32 API is a fairly complex topic, and it's not a whole load easier using Win32::GUI, but there are some helper functions, and along with an understanding of how scrollbars function it is possible to write clean, easily maintained code.

This is going to be a series of articles that introduces scrollbar usage under Win32::GUI and slowly builds to solve the problem asked, along with some that weren't.

To get us started here is my 'scratch program', a snippet that many of you will have seen me base examples on in the past, but that I have never documented before. All this program does is display a main window.

1 #!perl -w
2 use strict;
3 use warnings;

Always use strict and warnings. It's a Perl Best Practice, and helps prevent many stupid mistakes, as well as some not-so-stupid ones.

4
5 use Win32::GUI 1.05 qw( CW_USEDEFAULT );

Unless you know the constructs that you are using are safe on all versions of Win32::GUI, then specify a version on the 'use' line. Although everything in the scratch program should be safe back to Win32::GUI v1.03 (or so) there's some newer stuff that we'll be using later. If you need to use constants supplied by Win32::GUI, then list them explicitly on the 'use' line.

6
7 my $mw = Win32::GUI::Window->new(
8 -title => "Scrollbar 01: Scratch Program",
9 -left => CW_USEDEFAULT,
10 -size => [ 400, 300 ],
11 );

Construct our main window. Passing CW_USEDEFAULT as the left co-ordinate of the window asks the window manager to position the window for us. This avoid having all instances of the window start in exactly the same place, as that can be very confusing for a user who starts a second instance but sees no change on the screen as the 2 windows align perfectly. It's equally confusing once you have started 2 instances to close the top one, and see no change in screen appearance.

12
13 $mw->Show();

Top-level windows are created hidden by Win32::GUI, so that you can create controls on them without the user seeing them being drawn. As a result you need to show your window to the user once you have finished constructing it.

14 Win32::GUI::Dialog();

Do the message pump, to run the UI.

15 $mw->Hide();

Hide the UI as soon as you are done with it, so that if something is slow in your system your users don't see the UI being slowly destroyed piece by piece.

16 exit(0);

Finally, exit explicitly, and return an exit code to the OS that indicates success - you never know when you might use your script as part of another script, and this might be important.

Finally, here's the code all in one place, so that you can cut&paste it.

#!perl -w
use strict;
use warnings;

use Win32::GUI 1.05 qw( CW_USEDEFAULT );

my $mw = Win32::GUI::Window->new(
-title => "Scrollbar 01: Scratch Program",
-left => CW_USEDEFAULT,
-size => [ 400, 300 ],
);

$mw->Show();
Win32::GUI::Dialog();
$mw->Hide();
exit(0);

Source code can be downloaded from the series index page. Move on to Part 2.

Comments