WPF没有内置的属性来隐藏标题栏的关闭按钮,但您可以用的P/Invoke的几行做到这一点。
首先,添加这些声明,以你的窗口类:
private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
然后把这个代码在窗口的Loaded事件:
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
而且你去那里:没有更多的关闭按钮。标题栏左侧也没有窗口图标,即使您右键单击标题栏也不会显示“系统”菜单 - 它们都会一起显示。
请注意,Alt + F4仍然会关闭窗口。如果您不希望在后台线程完成之前关闭窗口,那么您也可以重写OnClosing并将Cancel设置为true,正如Gabe所建议的那样。
谢谢,但我不是在谈论标题栏。我在谈论现代UI框架提供的对话窗口。 –
你能分享快照吗? – Lakhtey