-
November 26th, 2008, 08:48 AM
#1
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.
-
November 26th, 2008, 08:50 AM
#2
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.
-
November 26th, 2008, 09:00 AM
#3
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.
-
November 26th, 2008, 10:21 AM
#4
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);
}
-
November 26th, 2008, 12:00 PM
#5
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.
-
November 26th, 2008, 04:02 PM
#6
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.
-
November 26th, 2008, 04:48 PM
#7
Re: How to disable/grayed-out the 'X' close button in dialog?
 Originally Posted by laitinen
That is standard windows behaviour, and you should try your very best to conform to the standard whenever you can.
Good point. . .
-
November 27th, 2008, 02:28 AM
#8
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.
-
November 27th, 2008, 04:46 AM
#9
Re: How to disable/grayed-out the 'X' close button in dialog?
Ok, I've found all I was looking for - Thanks to everybody !!
:-)
-
December 17th, 2009, 11:06 AM
#10
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?
-
December 17th, 2009, 11:24 AM
#11
Re: How to disable/grayed-out the 'X' close button in dialog?
 Originally Posted by jinkazama
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
-
September 17th, 2019, 05:52 AM
#12
Re: How to disable/grayed-out the 'X' close button in dialog?
 Originally Posted by laitinen
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.
-
September 17th, 2019, 06:27 AM
#13
Re: How to disable/grayed-out the 'X' close button in dialog?
 Originally Posted by vcdebugger
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
-
September 17th, 2019, 07:11 AM
#14
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)
-
September 17th, 2019, 07:43 AM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|