Consider the following code:
Dim recSelection As Rectangle?
Dim pntDown As Point?
Dim pntMove As Point?
Protected Overrides Sub OnMouseDown(e As Windows.Forms.MouseEventArgs)
MyBase.OnMouseDown(e)
pntDown = Me.PointToScreen(New Point(e.X, e.Y))
pntMove = pntDown
End Sub
Protected Overrides Sub OnMouseUp(e As Windows.Forms.MouseEventArgs)
If Me.recSelection.HasValue Then
ControlPaint.DrawReversibleFrame(recSelection.Value, Me.BackColor, FrameStyle.Dashed)
End If
pntDown = Nothing
pntMove = Nothing
recSelection = Nothing
MyBase.OnMouseUp(e)
End Sub
Protected Overrides Sub OnMouseMove(e As Windows.Forms.MouseEventArgs)
MyBase.OnMouseMove(e)
If pntDown.HasValue Then
If recSelection.HasValue Then
ControlPaint.DrawReversibleFrame(recSelection.Value, Me.BackColor, FrameStyle.Dashed)
End If
pntMove = Me.PointToScreen(New Point(Math.Max(Math.Min(e.X, Me.ClientSize.Width), 0), Math.Max(Math.Min(e.Y, Me.ClientSize.Height), 0)))
recSelection = GetRectangle(pntDown, pntMove)
ControlPaint.DrawReversibleFrame(Me.recSelection.Value, Me.BackColor, FrameStyle.Dashed)
End If
End Sub
Private Function GetRectangle(pointA As Point, pointB As Point) As Rectangle
Dim intLeft As Integer = Math.Min(pointA.X, pointB.X)
Dim intTop As Integer = Math.Min(pointA.Y, pointB.Y)
Dim intRight As Integer = Math.Max(pointA.X, pointB.X)
Dim intBottom As Integer = Math.Max(pointA.Y, pointB.Y)
Return Rectangle.FromLTRB(intLeft, intTop, intRight, intBottom)
End Function
Basically I want to draw a selection rectangle over a control and its children. In MSDN documentation, it says that to erase the rectangle, I should recall the DrawReversibleFrame
method with the same parameters used to draw it in the first place.
Unfortunately, in my case that doesn't seem to work. The previous selection rectangle remains painted over the control. At one point I can end up having multiple selection rectangles accumulating:
(not the actual screenshot, I used MS Paint to reproduce the effect)
What am I doing wrong?
UPDATE:
I tried the very same code shown in the documentation and the behavior is the exact same! Might have something to do with my specific display settings. Also I'm using Windows 8.1. Could that be the issue? I'll try deploying on another system tomorrow.
Aucun commentaire:
Enregistrer un commentaire