none
Show / Hide Desktop Icons in VB.NET

    Question

  • Hello all,
    Just a quick question. Is there a way to Show/Hide the desktop icons in .NET?
    I tried manually editing the HideIcons subkey in the registry through code, but it didn't take for some reason.
    I also tried calls to Win API methods FindWindowA but i'm not the best at handling unmanaged code.
    Any assistance is appreciated folks. Thanks!
    Thursday, July 24, 2008 4:16 PM

Answers

  • Hi,

    I can only find a solution using PInvoke for this. The sample code (with disclaimer) is as followed

    '===================================================================  
    ' DISCLAIMER:  
    '-------------------------------------------------------------------  
    '  
    ' This sample is provided as is and is not meant for use on a   
    ' production environment. It is provided only for illustrative   
    ' purposes. The end user must test and modify the sample to suit   
    ' their target environment.  
     
    ' Microsoft can make no representation concerning the content of   
    ' this sample. Microsoft is providing this information only as a   
    ' convenience to you. This is to inform you that Microsoft has not   
    ' tested the sample and therefore cannot make any representations   
    ' regarding the quality, safety, or suitability of any code or   
    ' information found here.  
     
    '===================================================================  
     
    Imports System.Runtime.InteropServices  
     
    Public Class ToggleDesktopIcon  
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function FindWindow( _  
            ByVal lpClassName As String, _  
            ByVal lpWindowName As StringAs IntPtr  
        End Function 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function GetWindow( _  
            ByVal hWnd As IntPtr, _  
            ByVal uCmd As UInteger) As IntPtr  
        End Function 
     
        Private Enum GetWindowCmd As UInteger  
            GW_HWNDFIRST = 0  
            GW_HWNDLAST = 1  
            GW_HWNDNEXT = 2  
            GW_HWNDPREV = 3  
            GW_OWNER = 4  
            GW_CHILD = 5  
            GW_ENABLEDPOPUP = 6  
        End Enum 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function ShowWindow( _  
            ByVal hwnd As IntPtr, _  
            ByVal nCmdShow As Int32) As Boolean 
        End Function 
     
        Private Enum SW As Int32  
            Hide = 0  
            Normal = 1  
            ShowMinimized = 2  
            ShowMaximized = 3  
            ShowNoActivate = 4  
            Show = 5  
            Minimize = 6  
            ShowMinNoActive = 7  
            ShowNA = 8  
            Restore = 9  
            ShowDefault = 10  
            ForceMinimize = 11  
            Max = 11  
        End Enum 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function IsWindowVisible(ByVal hwnd As IntPtr) As Boolean 
        End Function 
     
        Public Shared Sub Toggle()  
            Dim hWnd As IntPtr = FindWindow("ProgMan"Nothing)  
            hWnd = GetWindow(hWnd, GetWindowCmd.GW_CHILD)  
     
            If IsWindowVisible(hWnd) Then 
                ShowWindow(hWnd, SW.Hide)  
            Else 
                ShowWindow(hWnd, SW.ShowNoActivate)  
            End If 
        End Sub 
     
    End Class 


    Best regards,
    Huy Nguyen - Visual Basic team.
    Friday, July 25, 2008 6:06 PM
    Moderator

All replies

  • Hi,

    I can only find a solution using PInvoke for this. The sample code (with disclaimer) is as followed

    '===================================================================  
    ' DISCLAIMER:  
    '-------------------------------------------------------------------  
    '  
    ' This sample is provided as is and is not meant for use on a   
    ' production environment. It is provided only for illustrative   
    ' purposes. The end user must test and modify the sample to suit   
    ' their target environment.  
     
    ' Microsoft can make no representation concerning the content of   
    ' this sample. Microsoft is providing this information only as a   
    ' convenience to you. This is to inform you that Microsoft has not   
    ' tested the sample and therefore cannot make any representations   
    ' regarding the quality, safety, or suitability of any code or   
    ' information found here.  
     
    '===================================================================  
     
    Imports System.Runtime.InteropServices  
     
    Public Class ToggleDesktopIcon  
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function FindWindow( _  
            ByVal lpClassName As String, _  
            ByVal lpWindowName As StringAs IntPtr  
        End Function 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function GetWindow( _  
            ByVal hWnd As IntPtr, _  
            ByVal uCmd As UInteger) As IntPtr  
        End Function 
     
        Private Enum GetWindowCmd As UInteger  
            GW_HWNDFIRST = 0  
            GW_HWNDLAST = 1  
            GW_HWNDNEXT = 2  
            GW_HWNDPREV = 3  
            GW_OWNER = 4  
            GW_CHILD = 5  
            GW_ENABLEDPOPUP = 6  
        End Enum 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function ShowWindow( _  
            ByVal hwnd As IntPtr, _  
            ByVal nCmdShow As Int32) As Boolean 
        End Function 
     
        Private Enum SW As Int32  
            Hide = 0  
            Normal = 1  
            ShowMinimized = 2  
            ShowMaximized = 3  
            ShowNoActivate = 4  
            Show = 5  
            Minimize = 6  
            ShowMinNoActive = 7  
            ShowNA = 8  
            Restore = 9  
            ShowDefault = 10  
            ForceMinimize = 11  
            Max = 11  
        End Enum 
     
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _  
        Private Shared Function IsWindowVisible(ByVal hwnd As IntPtr) As Boolean 
        End Function 
     
        Public Shared Sub Toggle()  
            Dim hWnd As IntPtr = FindWindow("ProgMan"Nothing)  
            hWnd = GetWindow(hWnd, GetWindowCmd.GW_CHILD)  
     
            If IsWindowVisible(hWnd) Then 
                ShowWindow(hWnd, SW.Hide)  
            Else 
                ShowWindow(hWnd, SW.ShowNoActivate)  
            End If 
        End Sub 
     
    End Class 


    Best regards,
    Huy Nguyen - Visual Basic team.
    Friday, July 25, 2008 6:06 PM
    Moderator
  • TheOnlyInvictus said:
    Just a quick question.
    Is there a way to Show/Hide the desktop icons in .NET?

    Thank you HuyN for your friendly help.


    Hi TheOnlyInvictus,


    To hide the desktop icons, you can P/Invoke FindWindowEx API and ShowWindow API in VB.NET:
    Code sample:
    http://www.developerfusion.co.uk/show/4722/

    In addition, this thread is helpful: How to create empty desktop without any icons
    http://forums.msdn.microsoft.com/en-US/vbgeneral/thread/fa2b2e61-7c00-4bff-bb9f-c548290a5650/


    Best regards,
    Martin Xie
    Thursday, July 31, 2008 3:50 AM
  • Thanks to both of your gentlemen.
    Luckily I was sidetracked and didn't waste more time on this, now I'll go back and tackle it with quite a bit more information.


    Thursday, July 31, 2008 12:51 PM
  • Hi, I saw your thread, following is easy code to do this in VB.NET. Form has a CheckBox that specify the state of the Desktop Icons
    [DllImport("user32.dll")] 
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 
    
    [DllImport("user32.dll", SetLastError = true)] 
    static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); 
    
    
    void ShowDesktopIcons(bool show) 
    { 
        IntPtr hWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "Progman", null); 
        if (show) 
           ShowWindow(hWnd, 5); 
        else 
           ShowWindow(hWnd, 0); 
    } 
    Also, following link has a free CodeTranslator. This is make Code Translation From VB.NET <-> C# 


    http://www.carlosag.net/Tools/CodeTranslator/



    i hope this will help you
    Pr.wa'el .Net Professional
    • Proposed as answer by Wa'el Tuesday, July 14, 2009 5:26 AM
    Tuesday, July 14, 2009 5:26 AM
  • This worked you just need to change "ShowWindow(hWnd, 5)" the 5 to a 4

    Friday, September 12, 2014 6:06 PM