Never ever close me – how to disable close button in WPF
Well, there is no such option in WPF. The only thing you can do is to go a bit down to unmanaged and hook WM messages to apply your own style to the system menu. How to do it?
First of all, we have to know to to hook system events in WPF. For such action, we have HwndSource. I wrote a bit about it here and here, so it’s the simple one.
protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource; if (hwndSource != null) { hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook)); } } IntPtr hwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) {
Next, let’s do a bit of Interop
[DllImport("user32.dll")] static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll")] static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); const uint MF_BYCOMMAND = 0x00000000; const uint MF_GRAYED = 0x00000001; const uint MF_ENABLED = 0x00000000; const uint SC_CLOSE = 0xF060; const int WM_SHOWWINDOW = 0x00000018; const int WM_CLOSE = 0x10;
And now, we can hook WM_SHOWWINDOW and WM_CLOSE to apply those styles
IntPtr hwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { if (msg == WM_SHOWWINDOW) { IntPtr hMenu = GetSystemMenu(hwnd, false); if (hMenu != IntPtr.Zero) { EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED); } } else if (msg == WM_CLOSE) { handled = true; } return IntPtr.Zero; }
We done. Now just think about how to close such window
Happy programming
You may also be interested with:
September 26th, 2007 · Comments (6)
6 Responses to “Never ever close me – how to disable close button in WPF”
Leave a Reply
Discover other tags
.NET 3.5
Accessibility
blogging general
blogging tools
C#
CodeProject
demos
DEV
DevAcademy2
DirectX
download
events
fun
Hardware
help
Interop
jobs
LINQ
Microsoft
Mobile
My tools
Performance
promo
Silverlight
soft
source
teched
TechedIsrael2008
thoughts
Tips and Tricks
tutorial
Vista
Vista Battery Saver
Visual Studio
VSTS
WCF
Web
Windows Gadgets
Windows Live
Work process
WPF
WPF/E
WPF crossbow
XNA
XPS
My tools
- .NET Framework Detector
- Duplicate images finder
- Exchange Security Policy for Windows Mobile Devices Fix
- Gas Price Windows Vista SideBar gadget
- Israel Traffic Information Windows Vista SideBar gadget
- Localization fix for SAP ES Explorer for Visual Studio
- LocTester
- RTL and LTR in Windows Live Writer
- Silverlight controls library
- Snipping tool integration plugin for WLW
- USB FM receiver library
- Vista Battery Saver
- WebCam control for WPF
- Windows Live SkyDrive attachment for Windows Live Writer
- Wireless Migrator
- WPF Virtual Keyboard




January 1st, 2009 at 12:23 am
Great post. Please add:
using System.Windows.Interop;
using System.Runtime.InteropServices;
January 1st, 2009 at 12:23 am
Challenge: create visual clipboard watcher, that displays and saves Bitmaps right after they arrives
January 1st, 2009 at 12:23 am
Gagan, thank you for response. The point in this article is not to prevent the window from closing, but disable close button in the window system menu
January 1st, 2009 at 12:23 am
There is a much simpler but WPF solution too:
Just subscribe to the Windows Closing Event and in the handler set e.Cancel = true
eg:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
}
void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
}
}
July 15th, 2009 at 8:09 am
Can you please change the source code color……..it’s really not readable. I mean light blue and light green they are not clearly visible
September 15th, 2009 at 11:45 am
HEY YOU DUMB AHOLE TAMIT THE HOMO HOW ABOUT POSTING A WPF PROJECT WITH SOURCE INSTEAD OF JUST COPYING AND PASTING