2011-06-19 46 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

相关问题