ControlPaint.FillReversibleRectangle

Hawkvalley1
Hawkvalley1 used Ask the Experts™
on
Hi am looking to try to get a similar look to the windows mousedown rectangle you see in Vista on the desktop. Semi-translucent light blue rectangle. I have tried this in the mousemove event - (r) being the updated points:

Dim color As Color = Color.FromArgb(30, 0, 200, 255)
ControlPaint.FillReversibleRectangle(r, color)

Other ideas? I am an advanced-beginner in GUI+ , thanks for the help...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Where are you trying to get this?...on your form?...or on the desktop too?

Author

Commented:
On my picturebox - winforms, sorry I missed that. VB 2008.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Sorry for the delay...my laptop crashed and I decided to go out to lunch instead of taking drastic measures with a hammer...  ;)

ControlPaint.FillReversibleRectangle() would work but will flicker A LOT.

An alternative is to simply "float" a borderless form over the PB:
Public Class Form1

    Private startPT, endPT As Point
    Private sf As New SelectionForm(Color.FromArgb(0, 200, 255), 0.3)

    Private Class SelectionForm
        Inherits Form

        Public Sub New(ByVal clr As Color, ByVal opacity As Double)
            Me.BackColor = clr
            Me.Opacity = opacity
            Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
            Me.ShowInTaskbar = False
            Me.StartPosition = FormStartPosition.Manual
        End Sub

    End Class

    Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Left Then
            startPT = Cursor.Position
            endPT = startPT
            Cursor.Clip = PictureBox1.RectangleToScreen(PictureBox1.ClientRectangle)
            Dim RC As Rectangle = NormalizedRC(startPT, endPT)
            sf.SetBounds(RC.X, RC.Y, RC.Width, RC.Height)
            sf.Show()
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If e.Button = Windows.Forms.MouseButtons.Left Then
            endPT = Cursor.Position
            Dim RC As Rectangle = NormalizedRC(startPT, endPT)
            sf.SetBounds(RC.X, RC.Y, RC.Width, RC.Height)
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Cursor.Clip = Nothing
            Dim RC As Rectangle = PictureBox1.RectangleToClient(sf.Bounds)
            sf.Hide()
            Debug.Print(RC.ToString)
        End If
    End Sub

    Private Function NormalizedRC(ByVal ptA As Point, ByVal ptB As Point) As Rectangle
        Return New Rectangle(New Point(Math.Min(ptA.X, ptB.X), Math.Min(ptA.Y, ptB.Y)), New Size(Math.Abs(ptB.X - ptA.X), Math.Abs(ptB.Y - ptA.Y)))
    End Function

End Class

Open in new window

OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
Yes that's it. I had to adjust the startPT and endPT with PointToScreen coordinates, but it works great. I want a crisp edge for it so I'll work on the paint event for it to draw a border - see if that will work. You have always have a great mind for things like this - no pun intended LOL.
   

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Yeah...you should be able to pop something pretty easily into the paint event to get a border.  =)

Author

Commented:
Thanks again.

Author

Commented:
This made a very crisp border!

ControlPaint.DrawReversibleFrame(sf.Bounds, Drawing.Color.Black, FrameStyle.Thick)

Painting one was not as crisp due to the opacity.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial