Date: 02-16-2022
Return to Index
created by gbSnippets
'This code shows how to resize a Graphic control as the dialog
'is resized. Also, the image is redrawn to fill the graphic control
'while maintaining the original image aspect ratio.
'Compiler Comments:
'This code compiles only in PBWin9. It compiles in PBWin10 but to work
'correctly, you must replace this line:
Graphic Get Client To wImg, hImg 'image dimensions
'with this line.input
Graphic Get Canvas To wImg, hImg 'image dimensions
'See this link, http://gbl_01136, for an improved PBWin10 version.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
%IDC_Visible = 600
Global hDlg As DWord
Function PBMain() As Long
Local Style As Long
Style = %WS_OverlappedWindow Or %WS_ClipChildren '%WS_OverlappedWindow
Dialog New Pixels, 0, "Image Resize",300,300,300,200, Style To hDlg
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x,y,wNew,hNew As Single
Local w,h As Long
Static wImg, hImg As Single, hBMP As DWord
Select Case CB.Msg
Case %WM_InitDialog
'put the image into a memory bitmap
Graphic Bitmap Load "about", 0, 0 To hBMP 'load image resource
Graphic Attach hBMP, 0
Graphic Get Client To wImg, hImg 'image dimensions
Case %WM_Size
Dialog Send hDlg, %WM_SETREDRAW, 0, 0 'prevent flickering
Dialog Get Client hDlg To w,h
'kill/create graphic control, sized to the dialog (cannot resize, must kill then create again)
Control Kill hDlg, %IDC_Visible
Control Add Graphic, hDlg, %IDC_Visible,"",10,10,w-20,h-20, %WS_Border
Graphic Attach hDlg, %IDC_Visible, Redraw
Graphic Color GetSysColor(%COLOR_WINDOWTEXT), GetSysColor(%COLOR_WINDOW)
Graphic Color %Black, %Gray : Graphic Clear
'get resized image dimensions, copy image from memory bitmap to visible control (centered)
GetNewImageSize(w-20,h-20,wImg,hImg,wNew,hNew) 'get resized image dimensions
Dialog Set Text hDlg, "Image Resize " + Format$(wImg/hImg," 0.0000") + Format$(wNew/hNew," 0.0000")
x = (w-20-wNew)/2 : y = (h-20-hNew)/2 'upper/left position so resized image is centered
Graphic Stretch hBMP, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1) 'copy (resized) from memory bitmap to visible image
Dialog Send hDlg, %WM_SETREDRAW, 1, 0 ' Turn on redraw
Dialog Redraw hDlg ' and redraw
End Select
End Function
Sub GetNewImageSize(wCont As Single, hCont As Single, wImg As Single, hImg As Single, wNew As Single, hNew As Single)
'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
wNew = wImg / Max(wImg / wCont, hImg / hCont)
hNew = hImg / Max(wImg / wCont, hImg / hCont)
End Sub
'gbs_00528
'Date: 03-03-2012
http://www.garybeene.com/sw/gbsnippets.htm