[WPF problems] how to disable the close button on the window
Zhou yinhui
Haha, it is mainly to call the removemenu API, so there is nothing to say, just "dry goods:
Namespace versatilemessageboxlib
{
Using system;
Using system. Windows. InterOP;
Using system. runtime. interopservices;
Public class closebutton
{
[Dllimport ("USER32", charset = charset. ANSI, setlasterror = true, exactspelling = true)]
Private Static extern int getsystemmenu (INT hwnd, int revert );
[Dllimport ("USER32", charset = charset. ANSI, setlasterror = true, exactspelling = true)]
Private Static extern int enablemenuitem (INT menu, int ideenableitem, int enable );
Private const int SC _close = 0xf060;
Private const int mf_bycommand = 0x00000000;
Private const int mf_grayed = 0x00000001;
Private const int mf_enabled = 0x00000002;
Private closebutton ()
{
}
Public static void disable (INT handle)
{
// The return value specifies the previous state of the menu item
// (It is either mf_enabled or mf_grayed). 0 xffffffff indicates that
// The menu item does not exist.
Switch (enablemenuitem (getsystemmenu (handle, 0), SC _close, mf_bycommand | mf_grayed ))
{
Case mf_enabled:
Break;
Case mf_grayed:
Break;
Case-1:
Throw new exception ("the close menu item does not exist .");
Default:
Break;
}
}
}
in use: Call closebutton. the Disable (INT handle) method can pass the window handle in. If the window is a WPF window, int handle = new windowinterophelper (window) can be used ). handle. toint32 (); to obtain the handle. The handle of the windowforms window can be obtained directly.
In addition, if you want to close the cancel window rather than disable this button, you can refer to my Article : [WPF problems] Hide me! Not close