今天遇到一件很見鬼的事: 隱藏視窗的 Close button
在 WPF 屬性裡居然找不到...........
碼的...這很簡單的視窗屬性,
MFC有,
但WPF居然沒有,
真的是讓我有點傻眼...
所以勒,
自己動手刻一個吧!!!
step1: 在Window 的 Class 裡定義視窗的style, 並將 user32.dll import 進來
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);
step2: 在Window 的 Loaded 事件裡取得 window handle, 並再設回去給視窗
private void onLoad(object sender, RoutedEventArgs e)
{
var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
}
變身前:
變身後:
以上!!
兩步驟, 惱人的 Close button 不見囉!!!
留言列表