/// <summary> /// Disables the system menu "Close" menu command, as well as the "X" close button on the window title bar. /// </summary> public static void DisableCloseBox(IWin32Window window) { IntPtr hWnd = window.Handle; IntPtr hMenu = SafeNativeMethods.GetSystemMenu(hWnd, false); if (hMenu == IntPtr.Zero) { NativeMethods.ThrowOnWin32Error("GetSystemMenu() returned NULL"); } int result = SafeNativeMethods.EnableMenuItem( hMenu, NativeConstants.SC_CLOSE, NativeConstants.MF_BYCOMMAND | NativeConstants.MF_GRAYED); bool bResult = SafeNativeMethods.DrawMenuBar(hWnd); if (!bResult) { NativeMethods.ThrowOnWin32Error("DrawMenuBar returned FALSE"); } GC.KeepAlive(window); }