Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirsbo/9f87ec43d11d5886dd46 to your computer and use it in GitHub Desktop.
Save kirsbo/9f87ec43d11d5886dd46 to your computer and use it in GitHub Desktop.
Show form on top of desktop with WinAPI and VB.NET

Authored in 2010

Showing a form above the desktop but below ordinary applications, can be a useful method when trying to develop an application that feels integrated to the Windows UI. This could typically be desktop gadgets / widgets and similar. You want a form that is shown always, even when users perform a "Minimize all" command.

Add the following class to your project:

Public Class WinDesktop
#Region "WinAPI declarations"
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SetParentWindow Lib "user32" Alias "SetParent" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
#End Region
    Public Shared Sub AttachFormToDesktop(ByRef targetForm As Form)
        Dim hWinShell, hDesktopClass, hDesktopFileListView As Long

        'In the below: Retrieving neccessary handles, one by one.

        '"ProgMan" is the Windows shell.
        hWinShell = FindWindow("progman", vbNullString)

        'The windows desktop is a "shelldll_defview" class, and also a child to the ProgMan window.
        hDesktopClass = FindWindowEx(hWinShell, 0, "shelldll_defview", vbNullString)

        '"syslistview32" is a windows listview. This particular listview is the desktops listview, and that's where we'll append our targetForm. 
        hDesktopFileListView = FindWindowEx(hDesktopClass, 0, "syslistview32", vbNullString)

        SetParentWindow(targetForm.Handle, hDesktopFileListView) 'Set targetform's parent window to the desktops' file listview

    End Sub 'Makes the form permanently positioned on top of the desktop, above the desktop icons but below any other running applications.
End Class

The above class holds all the code neccessary to take a generic windows form as a reference parameter, and "attach" that form to the Windows desktop.

In the class, we first declare windows API functions that allows us to address various WinAPI "windows". The only sub in the class is titled AttachFormToDesktop(targetForm). In this sub, we use several Windows API commands (FindWindow and FindWindowEX) to find the neccessary windows. First we find the windows shell (ProgMan). Based on that, we find the desktop class (an instance of the shelldll_defview class), and based on that, we retrieve the desktop's filelist (a so called listView control, of the syslistview32 class type).

Finally we attach a form by setting the parent of that form to the desktop listview.

In the form we want to attach to the desktop, we can add a tiny call to the class in the Load event of the form.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WinDesktop.AttachFormToDesktop(Me)
    End Sub

Now everytime this form is loaded, it will automatically insert itself above the desktop icons but below any active applications. Note that if you have a minimize button on your form, users can still minimize your app. Therefore it is recommended that when using this approach, the formBorderStyle of the target form, is set to "none".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment