C# 更换windows桌面

C# 更换windows桌面,c#,wpf,windows,client,desktop,C#,Wpf,Windows,Client,Desktop,对于我正在做的软件项目,我需要替换桌面窗口 要求: 窗口需要位于桌面图标上方(不再需要这些图标) 多屏幕/监视器 其他窗口可以正常运行 我想保持任务栏不变 按windows+D或单击桌面按钮时,应显示我的应用程序 我目前一直在尝试,但这取代了整个桌面,包括任务栏。我可以写我自己的任务栏,但我觉得这是很多额外的工作,我希望可以避免 然后是函数,但我不知道使用哪个Z级别来实现我想要的 有人能告诉我实现我的要求的最佳方法是什么吗?你需要在桌面上,应用程序下面,并隐藏任务栏。 Id首先尝试通过隐藏任

对于我正在做的软件项目,我需要替换桌面窗口

要求:

  • 窗口需要位于桌面图标上方(不再需要这些图标)
  • 多屏幕/监视器
  • 其他窗口可以正常运行
  • 我想保持任务栏不变
  • 按windows+D或单击桌面按钮时,应显示我的应用程序
我目前一直在尝试,但这取代了整个桌面,包括任务栏。我可以写我自己的任务栏,但我觉得这是很多额外的工作,我希望可以避免

然后是函数,但我不知道使用哪个Z级别来实现我想要的


有人能告诉我实现我的要求的最佳方法是什么吗?

你需要在桌面上,应用程序下面,并隐藏任务栏。 Id首先尝试通过隐藏任务栏

其次,通过使用HWND_-bottom将窗口设置为最底部。 有一些奇怪的行为最好跨多个操作系统进行测试


由于任务栏一直显示,编辑-确定需要一些修改。加载时会出现闪烁,然后返回到后面。其中一些可以优化,但我将留给你-但这里有一个工作演示。我已经测试过移动任务栏和多个桌面-所有这些似乎都正常工作。您还需要防止窗口关闭(如果您想要这种行为),但挂接WM_CLOSE for ex.并忽略它(仍然不会阻止某人使用任务管理器,在这种情况下,您需要一个观察者进程和两个进程相互监视,以检测另一个进程何时关闭)。我从几篇文章和一些无聊的文章中编译了这篇文章

窗口属性 在任务栏中显示=错误 WindowState=最大化 ControlBox=false FormBorderStyle=无

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DesktopReplacement { public partial class Form1 : Form { private bool _enableOverride; internal class NativeMethods { public const int WM_WINDOWPOSCHANGING = 0x46; public const int WM_WINDOWPOSCHANGED = 0x47; public const int GWL_HWNDPARENT = -8; public const int SW_SHOW = 1; [Flags()] public enum SetWindowPosFlags { SWP_NOSIZE = 0x1, SWP_NOMOVE = 0x2, SWP_NOZORDER = 0x4, SWP_NOREDRAW = 0x8, SWP_NOACTIVATE = 0x10, SWP_FRAMECHANGED = 0x20, SWP_DRAWFRAME = SWP_FRAMECHANGED, SWP_SHOWWINDOW = 0x40, SWP_HIDEWINDOW = 0x80, SWP_NOCOPYBITS = 0x100, SWP_NOOWNERZORDER = 0x200, SWP_NOREPOSITION = SWP_NOOWNERZORDER, SWP_NOSENDCHANGING = 0x400, SWP_DEFERERASE = 0x2000, SWP_ASYNCWINDOWPOS = 0x4000, } public enum WindowZOrder { HWND_TOP = 0, HWND_BOTTOM = 1, HWND_TOPMOST = -1, HWND_NOTOPMOST = -2, } [StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hWnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public SetWindowPosFlags flags; // Returns the WINDOWPOS structure pointed to by the lParam parameter // of a WM_WINDOWPOSCHANGING or WM_WINDOWPOSCHANGED message. public static WINDOWPOS FromMessage(Message msg) { // Marshal the lParam parameter to an WINDOWPOS structure, // and return the new structure return (WINDOWPOS)Marshal.PtrToStructure(msg.LParam, typeof(WINDOWPOS)); } // Replaces the original WINDOWPOS structure pointed to by the lParam // parameter of a WM_WINDOWPOSCHANGING or WM_WINDOWPSCHANGING message // with this one, so that the native window will be able to see any // changes that we have made to its values. public void UpdateMessage(Message msg) { // Marshal this updated structure back to lParam so the native // window can respond to our changes. // The old structure that it points to should be deleted, too. Marshal.StructureToPtr(this, msg.LParam, true); } } } public static class HWND { public static readonly IntPtr NOTOPMOST = new IntPtr(-2), BROADCAST = new IntPtr(0xffff), TOPMOST = new IntPtr(-1), TOP = new IntPtr(0), BOTTOM = new IntPtr(1); } public static class SWP { public static readonly int NOSIZE = 0x0001, NOMOVE = 0x0002, NOZORDER = 0x0004, NOREDRAW = 0x0008, NOACTIVATE = 0x0010, DRAWFRAME = 0x0020, FRAMECHANGED = 0x0020, SHOWWINDOW = 0x0040, HIDEWINDOW = 0x0080, NOCOPYBITS = 0x0100, NOOWNERZORDER = 0x0200, NOREPOSITION = 0x0200, NOSENDCHANGING = 0x0400, DEFERERASE = 0x2000, ASYNCWINDOWPOS = 0x4000; } [DllImport("user32.dll", SetLastError = true)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")] private static extern int ShowWindow(IntPtr hwnd, int command); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { IntPtr hprog = FindWindowEx( FindWindowEx( FindWindow("Progman", "Program Manager"), IntPtr.Zero, "SHELLDLL_DefView", "" ), IntPtr.Zero, "SysListView32", "FolderView" ); SetWindowLong(this.Handle, NativeMethods.GWL_HWNDPARENT, hprog); } protected override void WndProc(ref Message m) { if (_enableOverride) { if (m.Msg == NativeMethods.WM_WINDOWPOSCHANGING) { // Extract the WINDOWPOS structure corresponding to this message NativeMethods.WINDOWPOS wndPos = NativeMethods.WINDOWPOS.FromMessage(m); wndPos.flags = wndPos.flags | NativeMethods.SetWindowPosFlags.SWP_NOZORDER; wndPos.UpdateMessage(m); } } base.WndProc(ref m); } private void timer1_Tick(object sender, EventArgs e) { SetWindowPos(Handle, HWND.BOTTOM, 0, 0, 0, 0, SWP.SHOWWINDOW | SWP.NOMOVE | SWP.NOOWNERZORDER | SWP.NOSIZE | SWP.NOACTIVATE); IntPtr task = FindWindow("Shell_TrayWnd", ""); ShowWindow(task, NativeMethods.SW_SHOW); _enableOverride = true; } } } 使用制度; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统诊断; 使用系统图; 使用System.Linq; 使用System.Runtime.InteropServices; 使用系统文本; 使用System.Threading.Tasks; 使用System.Windows.Forms; 命名空间桌面替换 { 公共部分类Form1:Form { 私有布尔启用覆盖; 内部类NativeMethods { 公共常量int WM_WINDOWPOSCHANGING=0x46; 公共常量int WM_WINDOWPOSCHANGED=0x47; 公共const int GWL_HWNDPARENT=-8; 公共建筑国际展览=1; [标志()] 公共枚举SetWindowPosFlags { SWP_NOSIZE=0x1, SWP_NOMOVE=0x2, SWP_NOZORDER=0x4, SWP_NOREDRAW=0x8, SWP_NOACTIVATE=0x10, SWP_FRAMECHANGED=0x20, SWP_DRAWFRAME=SWP_FRAMECHANGED, SWP_显示窗口=0x40, SWP_HIDEWINDOW=0x80, SWP_NOCOPYBITS=0x100, SWP_NOOWNERZORDER=0x200, SWP_NOREPOSION=SWP_NOOWNERZORDER, SWP_NOSENDCHANGING=0x400, SWP_=0x2000, SWP_ASYNCWINDOWPOS=0x4000, } 公共枚举WindowZOrder { HWND_TOP=0, HWND_底部=1, HWND_TOPMOST=-1, HWND_NOTOPMOST=-2, } [StructLayout(LayoutKind.Sequential)] 公共结构WINDOWPOS { 公共IntPtr hWnd; 公共IntPtr HwnInsertafter; 公共int x; 公共智力; 公共int cx; 公共关系; 公共设置窗口POSFlags; //返回lParam参数指向的WINDOWPOS结构 //WM_WINDOWPOSCHANGED或WM_WINDOWPOSCHANGED消息的。 公共静态窗口POS FromMessage(消息消息消息) { //将lParam参数封送至WINDOWPOS结构, //并返回新结构 return(WINDOWPOS)Marshal.PtrToStructure(msg.LParam,typeof(WINDOWPOS)); } //替换LPRAM指向的原始WINDOWPOS结构 //WM_WindowPosChangeing或WM_WindowPsChangeing消息的参数 //使用此选项,以便本机窗口能够看到任何 //我们对其值所做的更改。 公共无效更新消息(消息消息消息) { //将此更新的结构封送回LPRAM,以便本机 //窗口可以响应我们的更改。 //它所指向的旧结构也应该删除。 StructureToPtr(this,msg.LParam,true); } } } 公共静态类HWND { 公共静态只读IntPtr NOTPMOST=新的IntPtr(-2), 广播=新的IntPtr(0xffff), TOPMOST=新IntPtr(-1), TOP=新IntPtr(0), 底部=新IntPtr(1); } 公共静态类SWP { 公共静态只读int NOSIZE=0x0001, NOMOVE=0x0002, NOZORDER=0x0004, NOREDRAW=0x0008, NOACTIVATE=0x0010, DRAWFRAME=0x0020, FRAMECHANGED=0x0020, SHOWWINDOW=0x0040, HIDEWINDOW=0x0080, NOCOPYBITS=0x0100, NOOWNERZORDER=0x0200, NOREPOSION=0x0200, NOSENDCHANGING=0x0400, 延迟擦除=0x2000, ASYNCWINDOWPOS=0x4000; } [DllImport(“user32.dll”,SetLastError=true)] 静态外部int SetWindowLong(IntPtr hW using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Windows.Threading; namespace WpfApplication1 { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { this.SourceInitialized += MainWindow_SourceInitialized; this.WindowStyle = WindowStyle.None; this.Loaded += Window_Loaded; this.WindowState = WindowState.Maximized; InitializeComponent(); DispatcherTimer dispatcherTimer = new DispatcherTimer(); dispatcherTimer.Tick += dispatcherTimer_Tick; dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 500); dispatcherTimer.Start(); } private bool _enableOverride; internal class NativeMethods { public const int WM_WINDOWPOSCHANGING = 0x46; public const int WM_WINDOWPOSCHANGED = 0x47; public const int GWL_HWNDPARENT = -8; public const int SW_SHOW = 1; [Flags] public enum SetWindowPosFlags { SWP_NOSIZE = 0x1, SWP_NOMOVE = 0x2, SWP_NOZORDER = 0x4, SWP_NOREDRAW = 0x8, SWP_NOACTIVATE = 0x10, SWP_FRAMECHANGED = 0x20, SWP_DRAWFRAME = SWP_FRAMECHANGED, SWP_SHOWWINDOW = 0x40, SWP_HIDEWINDOW = 0x80, SWP_NOCOPYBITS = 0x100, SWP_NOOWNERZORDER = 0x200, SWP_NOREPOSITION = SWP_NOOWNERZORDER, SWP_NOSENDCHANGING = 0x400, SWP_DEFERERASE = 0x2000, SWP_ASYNCWINDOWPOS = 0x4000 } public enum WindowZOrder { HWND_TOP = 0, HWND_BOTTOM = 1, HWND_TOPMOST = -1, HWND_NOTOPMOST = -2 } [StructLayout(LayoutKind.Sequential)] public struct WINDOWPOS { public IntPtr hWnd; public IntPtr hwndInsertAfter; public int x; public int y; public int cx; public int cy; public SetWindowPosFlags flags; // Returns the WINDOWPOS structure pointed to by the lParam parameter // of a WM_WINDOWPOSCHANGING or WM_WINDOWPOSCHANGED message. public static WINDOWPOS FromMessage(IntPtr lParam) { // Marshal the lParam parameter to an WINDOWPOS structure, // and return the new structure return (WINDOWPOS)Marshal.PtrToStructure(lParam, typeof(WINDOWPOS)); } // Replaces the original WINDOWPOS structure pointed to by the lParam // parameter of a WM_WINDOWPOSCHANGING or WM_WINDOWPSCHANGING message // with this one, so that the native window will be able to see any // changes that we have made to its values. public void UpdateMessage(IntPtr lParam) { // Marshal this updated structure back to lParam so the native // window can respond to our changes. // The old structure that it points to should be deleted, too. Marshal.StructureToPtr(this, lParam, true); } } } public static class HWND { public static readonly IntPtr NOTOPMOST = new IntPtr(-2), BROADCAST = new IntPtr(0xffff), TOPMOST = new IntPtr(-1), TOP = new IntPtr(0), BOTTOM = new IntPtr(1); } public static class SWP { public static readonly int NOSIZE = 0x0001, NOMOVE = 0x0002, NOZORDER = 0x0004, NOREDRAW = 0x0008, NOACTIVATE = 0x0010, DRAWFRAME = 0x0020, FRAMECHANGED = 0x0020, SHOWWINDOW = 0x0040, HIDEWINDOW = 0x0080, NOCOPYBITS = 0x0100, NOOWNERZORDER = 0x0200, NOREPOSITION = 0x0200, NOSENDCHANGING = 0x0400, DEFERERASE = 0x2000, ASYNCWINDOWPOS = 0x4000; } [DllImport("user32.dll", SetLastError = true)] static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindow(string lpWindowClass, string lpWindowName); [DllImport("user32.dll", SetLastError = true)] static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle); [DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags); [DllImport("user32.dll")] private static extern int ShowWindow(IntPtr hwnd, int command); private void dispatcherTimer_Tick(object sender, EventArgs e) { //ensure we don't overlap the taskbar. SetWindowPos(new WindowInteropHelper(this).Handle, HWND.BOTTOM, 0, 0, 0, 0, SWP.SHOWWINDOW | SWP.NOMOVE | SWP.NOOWNERZORDER | SWP.NOSIZE | SWP.NOACTIVATE); IntPtr task = FindWindow("Shell_TrayWnd", ""); ShowWindow(task, NativeMethods.SW_SHOW); _enableOverride = true; } private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (_enableOverride) { if (msg == NativeMethods.WM_WINDOWPOSCHANGING) { Debug.WriteLine("WM_WINDOWPOSCHANGING"); // Extract the WINDOWPOS structure corresponding to this message //lParam has the ptr to a WindowsPos structure if its our WM_WINDOWPOSCHANGING struct NativeMethods.WINDOWPOS wndPos = NativeMethods.WINDOWPOS.FromMessage(lParam); wndPos.flags = wndPos.flags | NativeMethods.SetWindowPosFlags.SWP_NOZORDER; wndPos.UpdateMessage(lParam); //handled = true; } } return IntPtr.Zero; } private void MainWindow_SourceInitialized(object sender, EventArgs e) { HwndSource source = PresentationSource.FromVisual(this) as HwndSource; source.AddHook(WndProc); } private void Window_Loaded(object sender, RoutedEventArgs e) { IntPtr hWnd = new WindowInteropHelper(this).Handle; IntPtr hprog = FindWindowEx( FindWindowEx( FindWindow("Progman", "Program Manager"), IntPtr.Zero, "SHELLDLL_DefView", "" ), IntPtr.Zero, "SysListView32", "FolderView" ); SetWindowLong(hWnd, NativeMethods.GWL_HWNDPARENT, hprog); } } }