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.
Printable View
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.
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.
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.
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);
}
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.
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.
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:
doesn't seem to work....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
}
any ideas ?!?
Thanks.
Ok, I've found all I was looking for - Thanks to everybody !!
:-)
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?
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.
Are you handling the close message from the Modal 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.