How can I make a selection rectangle? Basically, my only problem is getting the rectangle to work when the mouse is dragged to the left from the selection point or to the top from the selection point.
I had to solve this same problem some time ago. Here’s my method for converting screen coordinates into a Realbasic.Rect object whose Left/Top/Width/Height can be passed directly to Graphics.DrawRect.
Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
If EndX < StartX Then
r.Left = EndX
r.Width = StartX - EndX
Else
r.Left = StartX
r.Width = EndX - StartX
End If
If EndY < StartY Then
r.Top = EndY
r.Height = StartY - EndY
Else
r.Top = StartY
r.Height = EndY - StartY
End If
Return r
End Function
Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
r.left=Min(StartX,EndX)
r.top=Min(StartY,EndY)
r.width=Max(StartX,EndX)-r.left
r.height=Max(StartY,EndY)-r.top
Return r
End Function
[quote=162477:@Dave S]
Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
r.left=Min(StartX,EndX)
r.top=Min(StartY,EndY)
r.width=Max(StartX,EndX)-r.left
r.height=Max(StartY,EndY)-r.top
Return r
End Function
No IF/THEN required[/quote]
I believe I tried something similar and it did not look good enough but I will give it a try.
[quote=162471:@Andrew Lambert]I had to solve this same problem some time ago. Here’s my method for converting screen coordinates into a Realbasic.Rect object whose Left/Top/Width/Height can be passed directly to Graphics.DrawRect.
Function GetSelectionRect(StartX As Integer, StartY As Integer, EndX As Integer, EndY As Integer) As REALbasic.Rect
Dim r As New REALbasic.Rect
If EndX < StartX Then
r.Left = EndX
r.Width = StartX - EndX
Else
r.Left = StartX
r.Width = EndX - StartX
End If
If EndY < StartY Then
r.Top = EndY
r.Height = StartY - EndY
Else
r.Top = StartY
r.Height = EndY - StartY
End If
Return r
End Function
[/quote]
THANKS