Draw on Window Desktop Background

Posted on 2009-12-20
Last Modified: 2013-11-26
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

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!

Question by:UHAX
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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)
        End Sub
    End Class

    Open in new window

    LVL 52

    Assisted Solution

    by:Carl Tawn
    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)
    End Sub

    Open in new window

    LVL 85

    Accepted Solution

    GetDC() should be paired with a ReleaseDC() call otherwise you'll get an eventual GDI leak.


        "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)
            ReleaseDC(desktopHwnd, desktopHdc)

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now