Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
%IDC_Graphic = 500 : %IDC_Button = 501 : %IDC_TextBox = 502
Global hDlg As Dword, AR As Single
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,500,300, %WS_OverlappedWindow + %CS_HRedraw + %CS_VRedraw To hDlg
Control Add Graphic, hDlg, %IDC_Graphic, "", 0,0,500,300
Graphic Attach hDlg, %IDC_Graphic
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local x,y As Long, PS As PaintStruct, hDC As Dword
Select Case Cb.Msg
Case %WM_Size
Local wNew,hNew,w,h,x1,x2,y1,y2 As Long
Dialog Get Client hDlg To w,h
Control Set Size hDlg, %IDC_Graphic, w,h
GetDimensions(0.9,4.0,w,h,x1,y1,x2,y2) 'edge, aspect ratio, wclient, hclient, x1/y1/x2/y2 corners
Graphic Clear : Graphic Box (x1,y1)-(x2,y2),,%Red
End Select
End Function
Sub GetDimensions(edge As Single, AR As Single, w As Long, h As Long, x1 As Long, y1 As Long, x2 As Long, y2 As Long)
Local wNew, hNew As Long
'get size of largest 2:1 aspect ratio rectangle that will fit in image of size w,h
wNew = AR / Max(AR / w, 1 / h) * edge
hNew = 1 / Max(AR / w, 1 / h) * edge
'center that rectangle in w,h (get the top/left/right/bottom coordinates)
x1 = (w-wNew)/2 : x2 = w - x1
y1 = (h-hNew)/2 : y2 = h - y1
End Sub
'gbs_00859
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm