Scrollbars - Part 5 - Setting the scroll range

Previous parts: 1, 2, 3 and 4.

The default set of positions that a scroll-bar can take is 0 .. 100. We are, however, free to set this range any way we wish to make our lives easy. In this case we'll choose to make the range of possible horizontal and vertical values equal to the width and hight of the bitmap that we're displaying.

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

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

35

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

37

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

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

Now if you run the sample and drag the scrollbar thumbs from one end to the other you'll see the range as set.

#!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 05: Set Scroll Range",

-left => CW_USEDEFAULT,

-size => [ 400, 300 ],

-addstyle => WS_CLIPCHILDREN,

-vscroll => 1,

-hscroll => 1,

-onScroll => \&process_scroll,

);

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->Show();

Win32::GUI::Dialog();

$mw->Hide();

exit(0);

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 4. Move on to Part 6.