How can you draw using gdi on the screen instead of a control ?

I want to draw some lines on a panel, but the lines always appear behind any of the controls on the panel.
I want to be able to draw over the controls (if possible) ?

thanks in advance,
LVL 10
Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You can draw on the desktop itself:

    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As Integer) As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim desktopHwnd As IntPtr = GetDesktopWindow()
        Dim desktopHdc As IntPtr = GetWindowDC(desktopHwnd)
        Dim g As Graphics = Graphics.FromHdc(desktopHdc)

        ' draw with "g" here...
        ' (Remember to use SCREEN coordinates)

        ReleaseDC(desktopHwnd, desktopHdc)
    End Sub
Well, the most direct thing I can think is to draw on a control that sits on top of the controls, perhaps with a transparent background?  If you draw on, say, the form background, .NET is pretty smart about showing and hiding things based on where controls are.
Kinger247Author Commented:
How do you clear any drawings if they're being moved ?
So on a control you would invalidate. But how can you do that to a screen ?
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you are drawing directly above your form then you can refresh the form and that will "erase" the desktop because the form redraws itself.

If you want a more targeted refresh then use the Invalidate() method:
Kinger247Author Commented:
Looks like I have some work to do .. ;)
Thanks Idle_Mind.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.