c# - 如何以编程方式隐藏桌面图标?

标签 c# .net icons desktop show-hide

如何使用 C# 以编程方式显示/隐藏桌面图标?

我正在尝试创建一个替代桌面,它使用小部件,我需要隐藏旧图标。

最佳答案

您可以使用 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);
    }

这会向 Progman 的 SHELLDLL_DefView 子窗口发送一条消息,告诉它切换它唯一的子窗口“FolderView”的可见性(通过添加或删除 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

关于c# - 如何以编程方式隐藏桌面图标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6402834/

相关文章:

delphi - 在Delphi 7中设置EXE图标

c# - Xamarin - GoogleMap 类移动到特定位置

.net - 何时创建属性实例?

c# - nhtmlunit javascript 不工作

c# - 恢复 C# 异步方法时,System.Diagnostics.StackTrace 中缺少 Visual Studio 2017 调用堆栈中可见的堆栈帧

c# - 在 C# 中以编程方式设置 dllimport

Android Canvas背景透明

css - Font Awesome Icons 作为列表项或普通背景图像?

c# - Stringbuilder 从多个数组追加表单

c# - "Could not create SSL/TLS secure channel"尝试从 Visual Studio 添加 Https 服务时出错