[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1076
  • Last Modified:

Draw on Window Desktop Background

I need ot draw directly onto the windows desktop, behind the desktop icons.

Simply setting the desktop wallpaper is far to so, how can I draw onto this space directly in vb.net.

I know I can get the handle of the desktop from GetShellWindow, but I have no idea how to draw it behind the desktop icons.

I cant even get it to draw into this handle, even if it were to draw over the icons it would be a start!

0
UHAX
Asked:
UHAX
  • 2
3 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You can draw over everything with:
Public Class Form1

    Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim desktop As IntPtr = GetDesktopWindow()
        Dim G As Graphics = Graphics.FromHwnd(desktop)
        G.DrawLine(Pens.Red, 0, 0, Screen.GetBounds(Me).Width, Screen.GetBounds(Me).Height)
        G.Dispose()
    End Sub

End Class

Open in new window

0
 
Carl TawnSystems and Integration DeveloperCommented:
Or, alternatively:
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Object) As IntPtr 
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

        Dim hdc As IntPtr = GetDC(Nothing)
        Dim g As Graphics = Graphics.FromHdc(hdc)
        g.DrawLine(Pens.Red, 0, 0, 1024, 768)
        g.Dispose()

End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
GetDC() should be paired with a ReleaseDC() call otherwise you'll get an eventual GDI leak.

See: http://msdn.microsoft.com/en-us/library/aa921543.aspx

    "After painting with a common device context, the ReleaseDC function must be called to release the device context."

If going that route I would do:

    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
 
    ...
        Dim desktopHwnd As IntPtr = GetDesktopWindow()
        Dim desktopHdc As IntPtr = GetWindowDC(desktopHwnd)
        Dim G As Graphics = Graphics.FromHdc(desktopHdc)
        G.DrawLine(Pens.Red, 0, 0, 1024, 768)
        G.Dispose()
        ReleaseDC(desktopHwnd, desktopHdc)
0
 
UHAXAuthor Commented:
Thanks
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now