dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: How to disable/grayed-out the 'X' close button in dialog?

  1. #1
    Join Date
    Dec 2004
    Posts
    293

    Question How to disable/grayed-out the 'X' close button in dialog?

    Hi.

    How can I disable or grayed-out the 'X' button that closes a simple dialog ??

    I know how to do it with the Minimize/Maximize buttons, i.e.: WS_MINIMIZEBOX/WS_MAXIMIZEBOX, but how to do it with the 'Close' button - that's my question.


    Thanks.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to disable/grayed-out the 'X' close button in dialog?

    The 'X' button is standard. I don't think you can gray it, but you can disable it's functionality. Override the 'OnClose' and do nothing in it.

  3. #3
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Couple of ways of doing it:

    1. Remove Close item from a system menu after main window is created, before main window shows. The ID for Close is SC_CLOSE.
    2. Register own window class for a main window. Specify CS_NOCLOSE as class style.
    3. Modify main window’s class style using SetClassLong and GCL_STYLE flag by adding CS_NOCLOSE.

    Approach 1 is the easiest for a dialog window.
    Last edited by JohnCz; November 26th, 2008 at 09:13 AM.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Nov 2008
    Posts
    1

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Here is the way I do it....

    In OnInitDialog function:

    CMenu* pSysMenu1 = GetSystemMenu(FALSE);

    if (pSysMenu1 != NULL)
    {
    //disable the X
    pSysMenu1->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_DISABLED);
    }

  5. #5
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Well,
    Disabling menu item suggest that it can be enabled, therefore is confusing; removing it seems more appropriate.

    Removing SC_CLOSE menu will disable close button.

    HOWEVER I have just tested it and in windows Vista, both actions will be needed: disabling first and removing next.
    Removing SC_CLOSE alone, will not render disabled button even though it does not close window.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  6. #6
    Join Date
    May 2006
    Location
    Norway
    Posts
    1,709

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Just my two pennies on this one.. I think you should have really good reasons for disabling/removing the "x" on the system menu. That is standard windows behaviour, and you should try your very best to conform to the standard whenever you can. Of course you might have good reasons for your requirement, but you have not said anything about it. So in my opinion, in most cases, this is not user-friendly design.

    Also it would be horrible to have the "x" disabled, only acceptable solution would be to remove the item from the menu.

  7. #7
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Quote Originally Posted by laitinen View Post
    That is standard windows behaviour, and you should try your very best to conform to the standard whenever you can.
    Good point. . .

  8. #8
    Join Date
    Dec 2004
    Posts
    293

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Thanks you all for answering, but the trick with SC_CLOSE is for menus/MDI architecture, I have a simple dialog.

    for some reason the CS_NOCLOSE doesn't work for me, this is what I do:

    Code:
    BOOL CCustomizedMessageBox::OnInitDialog()
    {
    	DWORD dwStyle = GetWindowLong(m_hWnd, GWL_STYLE);
    	
    	dwStyle |= CS_NOCLOSE;		
    
    	// execute new style.
    	SetWindowLong(m_hWnd, GWL_STYLE, dwStyle); 
    	
    	CDialog::OnInitDialog();
    
    	return TRUE;  // return TRUE unless you set the focus to a control
    	// EXCEPTION: OCX Property Pages should return FALSE
    }
    doesn't seem to work....

    any ideas ?!?

    Thanks.

  9. #9
    Join Date
    Dec 2004
    Posts
    293

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Ok, I've found all I was looking for - Thanks to everybody !!

    :-)

  10. #10
    Join Date
    Nov 2009
    Posts
    1

    Re: How to disable/grayed-out the 'X' close button in dialog?

    how do you disable the 'X' close button then?

    I tried

    pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_DISABLED);

    but all it does it gray out the 'X' close button but not removing it from title bar

    any ideas?

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Quote Originally Posted by jinkazama View Post
    how do you disable the 'X' close button then?

    I tried

    pSysMenu->EnableMenuItem (SC_CLOSE, MF_BYCOMMAND|MF_DISABLED);

    but all it does it gray out the 'X' close button but not removing it from title bar

    any ideas?
    Well, you wanted to *disable* 'X' close button and it was *disabled* (grayed out).
    What else?
    Victor Nijegorodov

  12. #12
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    435

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Quote Originally Posted by laitinen View Post
    Just my two pennies on this one.. I think you should have really good reasons for disabling/removing the "x" on the system menu. That is standard windows behaviour, and you should try your very best to conform to the standard whenever you can. Of course you might have good reasons for your requirement, but you have not said anything about it. So in my opinion, in most cases, this is not user-friendly design.

    Also it would be horrible to have the "x" disabled, only acceptable solution would be to remove the item from the menu.

    I have one reason for similar functionality of "greying" out the On close button, because my Modal Dialog crashes when I close it during some processing is happening in the background like " progress bar updates on my dialog box" , Edit box updates on my dialog box" , Other applications running which I had called from this application using "Shell execute:"

    Any idea how to solve this crash issue. I tried few methods like disabling the progress bar and edit box updates but not working.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Quote Originally Posted by vcdebugger View Post
    I have one reason for similar functionality of "greying" out the On close button, because my Modal Dialog crashes when I close it during some processing is happening in the background like " progress bar updates on my dialog box" , Edit box updates on my dialog box" , Other applications running which I had called from this application using "Shell execute:"

    Any idea how to solve this crash issue. I tried few methods like disabling the progress bar and edit box updates but not working.
    Sounds like you code is not good enough to allow closing your dialog cleanly.
    You just need to fix your code!
    Victor Nijegorodov

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,923

    Re: How to disable/grayed-out the 'X' close button in dialog?

    Are you handling the close message from the Modal Dialog?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  15. #15
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    435

    Re: How to disable/grayed-out the 'X' close button in dialog?

    yes. I am handling it in the function onClose();

    One more peculiar observation I found was everything goes smooth if I dont close the Dialog till the end.
    it crashes only If I close it during the processing of data is happening.

    I am killing the applications which I had opened in OnClose() function.

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)