Rubber Band (Focus Rectangle)
Posted: Mon Oct 04, 2004 11:04 pm
Code updated For 5.20+
Code: Select all
Procedure Set_Rect (x1,y1,x2,y2, *FRect.Rect)
If x2<x1
mx = x1
x1 = x2
x2 = mx
EndIf
If y2<y1
my = y1
y1 = y2
y2 = my
EndIf
If x1<0 : x1 = 0 : EndIf
If y1<0 : y1 = 0 : EndIf
*FRect\left = x1
*FRect\Top = y1
*FRect\Right = x2
*FRect\Bottom = y2
EndProcedure
If OpenWindow(0,0,0,745,425,"Démo Rectangle de sélection",#PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget)
hdc = GetDC_(WindowID(0))
DraggingOn = 0
Repeat
EventID = WaitWindowEvent()
If EventID = #WM_LBUTTONDOWN
GetCursorPos_(@StartPos.Point) ; Memorise Start position of the cursor
GetWindowRect_(WindowID(0) ,@SDRect.RECT)
If PtInRect_(SDRect,PeekQ(StartPos)) ; if the cursor is inside the window
StartPos\x - SDRect\Left-2 ; calculate relative corrdinates
StartPos\y - SDRect\Top - 27
MPos.Point\x = StartPos\x
MPos\y = StartPos\y
DraggingOn = 1
Set_Rect(WindowX(0)+3,WindowY(0)+29,WindowX(0)+3+WindowWidth(0),WindowY(0)+29+WindowHeight(0),WRect.Rect)
ClipCursor_(WRect) ; clip cursor to be sure to get the #WM_LBUTTONUP event
EndIf
EndIf
If GetAsyncKeyState_(#VK_LBUTTON) And DraggingOn ; if button is still pressed
GetCursorPos_(@OverPos.Point)
GetWindowRect_(WindowID(0) ,@SDRect.RECT)
If PtInRect_(SDRect,PeekQ(OverPos)) ; if the cursor is inside the window
OverPos\x - SDRect\Left-2 ; calculate relative corrdinates
OverPos\y - SDRect\Top - 27
If MPos.Point\x<>StartPos.Point\x Or MPos\y<>StartPos\y ; if cursor has move since start
Set_Rect (StartPos\x,StartPos\y,MPos.Point\x,MPos\y, FRect.Rect)
DrawFocusRect_(hdc,FRect) ; Erase last drawing
EndIf
If MPos.Point\x<>OverPos\x Or MPos\y<>OverPos\y
Set_Rect (StartPos\x,StartPos\y,OverPos\x,OverPos\y, FRect.Rect)
MPos\x = OverPos\x ; Memorise new drawing
MPos\y = OverPos\y
DrawFocusRect_(hdc,FRect) ; and draw it
EndIf
EndIf
EndIf
If EventID = #WM_LBUTTONUP And DraggingOn
Set_Rect (StartPos\x,StartPos\y,MPos.Point\x,MPos\y, FRect.Rect)
DrawFocusRect_(hdc,FRect) ; erase last drawing
DraggingOn = 0
ClipCursor_(0) ; free cursor
EndIf
Until EventID = #PB_Event_CloseWindow
ReleaseDC_(WindowID(0),hdc)
EndIf