从 C# WPF 中隐藏桌面图标
Posted
技术标签:
【中文标题】从 C# WPF 中隐藏桌面图标【英文标题】:Hide desktop icons from C# WPF 【发布时间】:2015-09-09 06:09:05 【问题描述】:我需要一个使用 C# WPF 以编程方式显示/隐藏桌面图标的解决方案 现在一种可能的方法是更改此注册表值:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced
HideIcons = 1
但要使其生效,我需要重新启动 Explorer.exe,这不是一个选项。 因为我的程序在桌面背景上运行。并杀死资源管理器会杀死程序,我已经重新启动它。
我也试过这个: *** Link 但它不适用于 Windows 10
那么,还有其他方法吗?请帮忙!
【问题讨论】:
【参考方案1】:创建类库在其中添加此代码 并添加命名空间 System.Runtime.InteropServices
[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;
public 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);
[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)));
public 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;
将此 dll 引用添加到您的项目中。 并调用 dllClass_name.ToggleDesktopIcon();
【讨论】:
不。当我调用该函数时,什么也没有发生。 不。不工作。我之前尝试过,即使是现在,也无法正常工作。以上是关于从 C# WPF 中隐藏桌面图标的主要内容,如果未能解决你的问题,请参考以下文章