Hide the Desktop

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This article can be espically useful if you are making a security program or just want to clear up your desktop without deleting anything.

Add a new module (modMain). Copy the following code into
the general declarations procedure:

Option Explicit
DefLng A-Z

Declare Function FindWindowEx Lib "user32" _
  Alias "FindWindowExA" (ByVal hWnd As Long, _
  ByVal hWndChild As Long, _
  ByVal lpszClassName As String, _
  ByVal lpszWindow As String) As Long
Declare Function ShowWindow Lib "user32" _
  (ByVal hWnd As Long, _
  ByVal nCmdShow As Long) As Long

'-- Constants for ShowWindow()
Public Const SW_HIDE = 0
Public Const SW_NORMAL = 1
Public Const SW_SHOWMINIMIZED = 2
Public Const SW_SHOWMAXIMIZED = 3
Public Const SW_SHOWNOACTIVATE = 4
Public Const SW_SHOW = 5
Public Const SW_MINIMIZE = 6
Public Const SW_SHOWMINNOACTIVE = 7
Public Const SW_SHOWNA = 8
Public Const SW_RESTORE = 9
Public Const SW_SHOWDEFAULT = 10

'-- Names of the shell window we'll be looking for
Public Const g_cstrShellViewWnd As String = "Progman"

Add a form (frmMain) To the form add two command buttons,
cmdHideDesktop and cmdShowDesktop. Copy the following code into the form’s General
Declarations procedure:

Option Explicit
DefLng A-Z

Private Function FindShellWindow() As Long
Dim hWnd As Long
On Error Resume Next

hWnd = FindWindowEx(0&, 0&, _
  g_cstrShellViewWnd, vbNullString)

If hWnd <> 0 Then
  FindShellWindow = hWnd
End If

End Function

Private Sub HideShowWindow(ByVal hWnd As Long, _
  Optional ByVal Hide As Boolean = False)

 Dim lngShowCmd As Long
 On Error Resume Next

 If Hide = True Then
   lngShowCmd = SW_HIDE
 Else
   lngShowCmd = SW_SHOW
 End If

 Call ShowWindow(hWnd, lngShowCmd)

End Sub

Private Sub cmdShowDesktop_Click()
 Dim hWnd As Long
 On Error Resume Next

 '-- Find the window we're 
 ' looking for and then hide it
 hWnd = FindShellWindow()

 If hWnd <> 0 Then
   Call HideShowWindow(hWnd)
 End If

End Sub

Private Sub cmdHideDesktop_Click()
 Dim hWnd As Long
 On Error Resume Next

 hWnd = FindShellWindow()

 If hWnd <> 0 Then
   Call HideShowWindow(hWnd, True)
 End If

End Sub

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories