Scrollbars - Part 6 - Setting the page size

Previous parts: 1, 2, 3, 4 and 5.

    1. In this article we set the scrollbar's page-size. Setting the page size has two effects on the scrollbars: Firstly it sets the size of the scrollbar thumb to indicate the amount of the document that is visible. Resize the window and see how the size of the thumb changes to indicate what proportion of the document is visible. Note too how the scrollbars disappear if the whole of the image is visible.
    2. Secondly it effects the range of positions available so that the position reported by the scrollbar so that when at the bottom (or right) of its range the position reported is the value needed to position the top (or left) edge of the content such that it just fits the visible window. In other words the maximum position reported is the maximum position set using the ScrollRange() method minus the page size.

42 $mw->ScrollPage( SB_HORZ, $mw->ScaleWidth() );

43 $mw->ScrollPage( SB_VERT, $mw->ScaleHeight() );

Initially we set the page size to reflect the client area. Note that as we set the range in pixels we set the page size in pixels too. If we were displaying a document, then we might set the range to reflect the number of lines in the document, and we would then set the page size in terms of the number of lines that can be displayed in the window.

23 my $mw = Win32::GUI::Window->new(

24 -title => "Scrollbar 06: Set Scroll Page Size",

25 -left => CW_USEDEFAULT,

26 -size => [ 400, 300 ],

27 -addstyle => WS_CLIPCHILDREN,

28 -vscroll => 1,

29 -hscroll => 1,

30 -onScroll => \&process_scroll,

31 -onResize => \&process_resize,

32 );

50 sub process_resize {

51 my ($self) = @_;

52

53 $self->ScrollPage( SB_HORZ, $self->ScaleWidth() );

54 $self->ScrollPage( SB_VERT, $self->ScaleHeight() );

55

56 return 1;

57 }

We also add a Resize event handler for our window, as the page size changes whenever we resize the window, and so we need to reset the page size that the scrollbars know about.

Here is the full code for this example. Note how the thumbs change size as the window is resized, and note the reduction in range of positions reported when dragging the thumbs compared to the previous example.

#!perl -w

use strict;

use warnings;

use Win32::GUI 1.05 qw(CW_USEDEFAULT WS_CLIPCHILDRENWM_HSCROLL WM_VSCROLLSB_CTL SB_HORZ SB_VERTSB_TOP SB_BOTTOM SB_LINEUP SB_LINEDOWN SB_PAGEUP SB_PAGEDOWNSB_LEFT SB_RIGHT SB_LINELEFT SB_LINERIGHT SB_PAGELEFT SB_PAGERIGHTSB_THUMBTRACK SB_THUMBPOSITION SB_ENDSCROLL);

my @vert_cmds = qw(SB_LINEUP SB_LINEDOWN SB_PAGEUP SB_PAGEDOWNSB_THUMBPOSITION SB_THUMBTRACKSB_TOP SB_BOTTOMSB_ENDSCROLL);

my @horz_cmds = qw(SB_LINELEFT SB_LINERIGHT SB_PAGELEFT SB_PAGERIGHTSB_THUMBPOSITION SB_THUMBTRACKSB_LEFT SB_RIGHTSB_ENDSCROLL);

my $mw = Win32::GUI::Window->new(

-title => "Scrollbar 06: Set Scroll Page Size",

-left => CW_USEDEFAULT,

-size => [ 400, 300 ],

-addstyle => WS_CLIPCHILDREN,

-vscroll => 1,

-hscroll => 1,

-onScroll => \&process_scroll,

-onResize => \&process_resize,

);

my $bm = Win32::GUI::Bitmap->new("kids.bmp");

$mw->AddLabel( -bitmap => $bm );

my ( $bmw, $bmh ) = $bm->Info();

$mw->ScrollRange( SB_HORZ, 0, $bmw );

$mw->ScrollRange( SB_VERT, 0, $bmh );

$mw->ScrollPage( SB_HORZ, $mw->ScaleWidth() );

$mw->ScrollPage( SB_VERT, $mw->ScaleHeight() );

$mw->Show();

Win32::GUI::Dialog();

$mw->Hide();

exit(0);

sub process_resize {

my ($self) = @_;

$self->ScrollPage( SB_HORZ, $self->ScaleWidth() );

$self->ScrollPage( SB_VERT, $self->ScaleHeight() );

return 1;

}

sub process_scroll {

my ( $self, $bar, $op, $pos ) = @_;

my ( $bar_text, $op_text );

if ( $bar == SB_VERT ) {

$bar_text = "VERTICAL";

$op_text = $vert_cmds[$op];

}

elsif ( $bar == SB_HORZ ) {

$bar_text = "HORIZONTAL";

$op_text = $horz_cmds[$op];

}

else {

$bar_text = "UNKNOWN";

$op_text = "UNKNOWN";

}

printf "%-10s : %-16s : %d\n", $bar_text, $op_text, $pos;

return 1;

}

Source code can be downloaded from the series index page. Back to Part 5. Move on to Part 7.