[WPF problems] how to disable the close button on the window

Source: Internet
Author: User
[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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.