3 回答

TA貢獻1786條經驗 獲得超13個贊
WPF沒有內置的屬性來隱藏標題欄的“關閉”按鈕,但是您可以通過幾行P / Invoke來實現。
首先,將這些聲明添加到您的Window類中:
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);
然后將此代碼放入Window的Loaded事件中:
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
然后您就可以了:不再需要關閉按鈕。標題欄的左側也沒有窗口圖標,這意味著沒有系統菜單,即使右鍵單擊標題欄也是如此。
請注意,Alt + F4仍將關閉窗口。如果您不希望在完成后臺線程之前關閉窗口,則也可以按照Gabe的建議重寫OnClosing并將Cancel設置為true。
- 3 回答
- 0 關注
- 3485 瀏覽
相關問題推薦
添加回答
舉報