WPF将窗体设置为桌面背景【动态桌面】
#region MyRegion private IntPtr programIntPtr = IntPtr.Zero; bool inited { get; set; } public void Init() { try { if (inited) return; // 通过类名查找一个窗口,返回窗口句柄。 programIntPtr = Win32.FindWindow("Progman", null); // 窗口句柄有效 if (programIntPtr != IntPtr.Zero) { IntPtr result = IntPtr.Zero; // 向 Program Manager 窗口发送 0x52c 的一个消息,超时设置为0x3e8(1秒)。 Win32.SendMessageTimeout(programIntPtr, 0x52c, IntPtr.Zero, IntPtr.Zero, 0, 0x3e8, result); // 遍历顶级窗口 Win32.EnumWindows((hwnd, lParam) => { // 找到包含 SHELLDLL_DefView 这个窗口句柄的 WorkerW if (Win32.FindWindowEx(hwnd, IntPtr.Zero, "SHELLDLL_DefView", null) != IntPtr.Zero) { // 找到当前 WorkerW 窗口的,后一个 WorkerW 窗口。 IntPtr tempHwnd = Win32.FindWindowEx(IntPtr.Zero, hwnd, "WorkerW", null); // 隐藏这个窗口 Win32.ShowWindow(tempHwnd, 0); } return true; }, IntPtr.Zero); } Win32.SetParent(new System.Windows.Interop.WindowInteropHelper(this).Handle, programIntPtr); inited = true; } catch (Exception ex) { } } #endregion
public static class Win32 { [DllImport("user32.dll")] public static extern IntPtr FindWindow(string className, string winName); [DllImport("user32.dll")] public static extern IntPtr SendMessageTimeout(IntPtr hwnd, uint msg, IntPtr wParam, IntPtr lParam, uint fuFlage, uint timeout, IntPtr result); [DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc proc, IntPtr lParam); public delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam); [DllImport("user32.dll")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string winName); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow); [DllImport("user32.dll")] public static extern IntPtr SetParent(IntPtr hwnd, IntPtr parentHwnd); }