From: Jez W. <je...@je...> - 2004-05-17 09:05:19
|
Hi, The code below shows an example of using a child window containing a = bitmap which is scrolled using the scroll bars of the child window. The = example uses a hooked paint message to pain directly to the child window = via a memory DC.=20 Assuming there is no problem with this example and people think it's = useful I'll commit it to the samples folder today. Cheers, jez. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #This example creates a scrolling bitmap within another window. This = example uses #a hooked event to paint to the window directly, rather than using a = Graphic #Control. use Win32::GUI; use strict; #create a new class which stops the WM_ERASEBKGND message from erasing = the background my $WC =3D new Win32::GUI::Class( -name =3D> "NoFlicker",=20 -style =3D> 0, ); #Create the window and child controls. my $mainwin =3D new Win32::GUI::Window ( -pos =3D> [100, 100], -size =3D> [330, 235], -name =3D> "Window", -text =3D> "Bitmap Scroll demo", -pushstyle =3D> WS_CLIPCHILDREN, -class =3D> $WC, #NEM Events for this window -onResize =3D> \&Resize, -onTerminate =3D> sub {return -1;} ); $mainwin->AddButton ( -name =3D> 'Open', -pos =3D> [205, 20], -size =3D> [110, 20], -text =3D> 'Open Bitmap', -onClick =3D> \&OpenBitmap, ); #Create a child window with a scroll bars.=20 my $ChildWin =3D new Win32::GUI::Window ( -parent =3D> $mainwin, -name =3D> "ChildWin", -pos =3D> [0, 0], -size =3D> [200, 200], -popstyle =3D> WS_CAPTION | WS_SIZEBOX, -pushstyle =3D> WS_CHILD | WS_CLIPCHILDREN, -pushexstyle =3D> WS_EX_CLIENTEDGE, -class =3D> $WC, -hscroll =3D> 1, -vscroll =3D> 1, -onScroll =3D> \&Scroll, ); #hook into the paint event of the child window $ChildWin->Hook(15, \&Paint); #Create a memory DC compatible with the child window DC my $memdc=3D$ChildWin->GetDC->CreateCompatibleDC(); #Define global variables my $bitmap; #will hold the bitmap #show both windows and enter the Dialog phase. $mainwin->Show(); $ChildWin->Show(); Win32::GUI::Dialog(); sub Paint { #Paint event handler, called when ever the window needs to be = redrawn/painted return unless $bitmap; #tell windows that we are starting to paint $ChildWin->BeginPaint; #get the DC of the child window my $dc=3D$ChildWin->GetDC; #Performa a bit block transfer of the memory DC into the child window = DC #The cordinates are based upon the possition of the scroll bars $dc->BitBlt(0, 0, = $ChildWin->Width,$ChildWin->Height,$memdc,$ChildWin->ScrollPos(0),$ChildW= in->ScrollPos(1)); #tell windows that we have finished painting. $ChildWin->EndPaint; } sub Resize { #Resize handler my ($width, $height) =3D ($mainwin->GetClientRect)[2..3]; $mainwin->Open->Left($width-120); #Resize the child window and set the scroll bar page of each scroll = bar. #This has the effect of increasing/decreasing the size of the bar = within the scroll bar #as the window is resized. $ChildWin->Resize($width-150,$height); $ChildWin->ScrollPage(0,$ChildWin->Width); $ChildWin->ScrollPage(1,$ChildWin->Height); } sub OpenBitmap { #Function to load in the bitmap my $file =3D Win32::GUI::GetOpenFileName( -owner =3D> $mainwin, -hidereadonly =3D> 0, -title =3D> "Open an bitmap file", -filter =3D> ['Bitmaps' =3D> '*.bmp', 'All files' =3D> '*.*', ], =20 ); $bitmap=3Dnew Win32::GUI::Bitmap($file); =20 if ($bitmap) { #if we have a valid bitmap, get the dimentions my ($width,$height)=3D$bitmap->Info(); #select the bitmap into the memory DC so it can be maniplated later. $memdc->SelectObject($bitmap); #set the scroll bar range and position based upon the height and = width of the bitmap. $ChildWin->ScrollRange(1,0,$height+20); #add 20 for the scroll bar $ChildWin->ScrollRange(0,0,$width+20); $ChildWin->ScrollPage(0,$ChildWin->Width); $ChildWin->ScrollPage(1,$ChildWin->Height); #set the scroll bars to 0. $ChildWin->ScrollPos(0,0); =20 $ChildWin->ScrollPos(1,0); $ChildWin->InvalidateRect(0); #invalidate the child window so = windows triggers the paint event } } sub Scroll { #Scroll event handler. We have to explicitly "move" the scroll bars. #Once they have been moved, we repaint the window. my($win,$scrollbar, $operation, $position) =3D @_; if($operation =3D=3D SB_THUMBTRACK) { $win->ScrollPos($scrollbar,$position); } elsif($operation =3D=3D SB_LINEDOWN) { $win->ScrollPos($scrollbar,$win->ScrollPos($scrollbar)+1); } elsif($operation =3D=3D SB_LINEUP) { $win->ScrollPos($scrollbar,$win->ScrollPos($scrollbar)-1); } elsif($operation =3D=3D SB_PAGEDOWN) { $win->ScrollPos($scrollbar,$win->ScrollPos($scrollbar) + = $win->ScrollPage($scrollbar)); } elsif($operation =3D=3D SB_PAGEUP) { $win->ScrollPos($scrollbar,$win->ScrollPos($scrollbar) - = $win->ScrollPage($scrollbar)); } #invalidate the child window so windows triggers the paint event $win->InvalidateRect(0); } |