2011-06-19 48 views

回答

18

您可以使用Windows API執行此操作。這裏是C#中的示例代碼,它將切換桌面圖標。

[DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 
    [DllImport("user32.dll", SetLastError = true)] static extern IntPtr GetWindow(IntPtr hWnd, GetWindow_Cmd uCmd); 
    enum GetWindow_Cmd : uint 
    { 
     GW_HWNDFIRST = 0, 
     GW_HWNDLAST = 1, 
     GW_HWNDNEXT = 2, 
     GW_HWNDPREV = 3, 
     GW_OWNER = 4, 
     GW_CHILD = 5, 
     GW_ENABLEDPOPUP = 6 
    } 
    [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); 

    private const int WM_COMMAND = 0x111; 

    static void ToggleDesktopIcons() 
    { 
     var toggleDesktopCommand = new IntPtr(0x7402); 
     IntPtr hWnd = GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD); 
     SendMessage(hWnd, WM_COMMAND, toggleDesktopCommand, IntPtr.Zero); 
    } 

這將消息發送到普羅格曼的SHELLDLL_DefView子窗口,告訴它切換可見性(通過添加或刪除的WS_VISIBLE風格),它是唯一的孩子,「文件夾視圖」。 「FolderView」是包含圖標的實際窗口。

要測試,看看是否圖標是可見或不可見,你可以查詢使用GetWindowInfo功能WS_VISIBLE風格,如下圖所示:

[return: MarshalAs(UnmanagedType.Bool)] 
    [DllImport("user32.dll", SetLastError = true)] 
    private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi); 

    [StructLayout(LayoutKind.Sequential)] 
    public struct RECT 
    { 
     private int _Left; 
     private int _Top; 
     private int _Right; 
     private int _Bottom; 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    struct WINDOWINFO 
    { 
     public uint cbSize; 
     public RECT rcWindow; 
     public RECT rcClient; 
     public uint dwStyle; 
     public uint dwExStyle; 
     public uint dwWindowStatus; 
     public uint cxWindowBorders; 
     public uint cyWindowBorders; 
     public ushort atomWindowType; 
     public ushort wCreatorVersion; 

     public WINDOWINFO(Boolean? filler) 
      : this() // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)". 
     { 
      cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO))); 
     } 

    } 

下面是調用上面的代碼,並返回true功能如果窗口可見,則返回false。

static bool IsVisible() 
    { 
     IntPtr hWnd = GetWindow(GetWindow(FindWindow("Progman", "Program Manager"), GetWindow_Cmd.GW_CHILD), GetWindow_Cmd.GW_CHILD); 
     WINDOWINFO info = new WINDOWINFO(); 
     info.cbSize = (uint)Marshal.SizeOf(info); 
     GetWindowInfo(hWnd, ref info); 
     return (info.dwStyle & 0x10000000) == 0x10000000; 
    } 

與有關窗口樣式的更多信息,以及Windows API的代碼可以在這裏找到:http://www.pinvoke.net/default.aspx/user32/GetWindowInfo.html

+2

真棒,即時將從現在開始將它放入我的所有應用程序並隨機切換()。 :) – Gleno

+0

它似乎並沒有在我的電腦上工作...我使用的是Windows 7。這個操作系統是否依賴?它應該適用於所有版本的Windows?如果是這樣,我會尋找另一種解決方案,適用於多個版本的Windows ... – Tibi

+0

更新:它確實工作,顯然我必須重新啓動explorer.exe,但現在它的工作。非常感謝你。另一個問題......我怎麼知道它是否開啓或關閉? – Tibi

1

您可以創建一個全屏視圖應用程序,並使其成爲最頂層窗口。

然後讓你的應用程序啓動與Windows。

+2

如果我讓最頂端,這將是對所有其他應用程序之上......它需要是完全相反的,最底層的窗口,除了任務欄。 – Tibi

0

你要對這個錯誤的方式。你真正想要做的是更換外殼。 Windows提供了這個功能,所以你應該利用它。編寫你自己的shell來代替瀏覽器。

+1

我不是想替換shell,只是桌面。我將擁有一些不錯的小部件,而不是無聊的圖標。 – Tibi

0

這裏描述,您可以在註冊表編輯器 HKEY_CURRENT_USER \ SOFTWARE \微軟\的Windows \ CurrentVersion \ Explorer中\高級 變化HideIcons做到這一點,以1

static void HideIcons() 
    { 
     RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced", true); 
     if (myKey != null) 
     { 
      myKey.SetValue("HideIcons", 1); 
      myKey.Close(); 
     } 
    } 

使用註冊表類。

http://msdn.microsoft.com/en-us/library/microsoft.win32.registry.aspx

相關問題