#region 禁用关闭按钮 [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] private static extern IntPtr GetSystemMenu(IntPtr hWnd, UInt32 bRevert); [DllImport("USER32.DLL ", CharSet = CharSet.Unicode)] private static extern UInt32 RemoveMenu(IntPtr hMenu, UInt32 nPosition, UInt32 wFlags); private const UInt32 SC_CLOSE = 0x0000F060; private const UInt32 MF_BYCOMMAND = 0x00000000; private void MainWindow_Loaded(object sender, RoutedEventArgs e) { var hwnd = new WindowInteropHelper(this).Handle; //获取window的句柄 IntPtr hMenu = GetSystemMenu(hwnd, 0); RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND); } #endregion
将此段代码放到WPF窗口的加载事件中,可以让WPF窗口标题中的关闭按钮禁用
但任务栏中缩略图的关闭按钮不会禁用(这应该是微软的bug)