Date: 02-16-2022
Return to Index
created by gbSnippets
'This code allows drawing a rectangle on the dialog by using the mouse.
'Compiler Comments:
'This code is written to compile in PBWin10. To compile in PBWin9, split pt
'into pt.x and pt.y as arguments wherever the PtinRect() API is used (2 places).
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, hDC As Dword, R, R2 As Rect, lastx,lasty As Long, pt as Point
Function PBMain() As Long
Dialog New Pixels, 0, "Draw Rectangle",300,300,420,250, %WS_OverlappedWindow To hDlg
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_RButtonDown
pt.x = Lo(Word,Cb.LParam)
pt.y = Hi(Word,Cb.LParam)
If PtinRect(R,pt) = 0 Then DrawRect 0 : Reset R
Case %WM_LButtonDown
R.nLeft = Lo(Word, Cb.LParam) : R.nTop = Hi(Word, Cb.LParam)
Case %WM_MouseMove
pt.x = Lo(Word,Cb.LParam)
pt.y = Hi(Word,Cb.LParam)
If (Cb.WParam And %MK_LBUTTON) Then
R.nRight = pt.x : R.nBottom = pt.y
DrawRect 1
ElseIf (Cb.WParam And %MK_RButton) And PtinRect(R,pt) Then
DrawRect 0
If (lastx + lasty) Then OffsetRect R, pt.x - lastx, pt.y - lasty
lastx = pt.x : lasty = pt.y
DrawRect 1
End If
End Select
End Function
Sub DrawRect(Flag As Long) '1=moving 0=done
Local tempR As Rect
hDC = GetDC(hDlg)
DrawFocusRect hDC, R2
tempR.nLeft = Min(R.nLeft,R.nRight) : tempR.nRight = Max(R.nLeft,R.nRight)
tempR.nTop = Min(R.nTop,R.nBottom) : tempR.nBottom = Max(R.nTop,R.nBottom)
If Flag Then DrawFocusRect hDC, tempR : R2 = tempR Else Reset R2
ReleaseDC hDlg, hDC
End Sub
'gbs_00876
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm