Disable dialog cancel box

How can I disable the cancel box of a dialog on the upper right corner?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you don't need : delete it.  

If you need for futur use put in the OnInitDialog() function :

   GetDlgItem(IDCANCEL)->EnableWindow ( FALSE );

this instruction will put your cancel in gray (false disabled and
true enabled)

shibaiAuthor Commented:
I don't mean the cancel button. I mean the little box with an "X" on it on the upper right corner of a dialog.
1)  Add DefWindowProc() message handler to your dialog box.
2)  Catch message=WM_SYSMENU and wparam=SC_CLOSE and don't call the default handler.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Sorry Shibai I mistaken your question

THE Answer of tflai, it's for C and not for MFC programming, and don't remove or GRAY the 'X' button.

In MFC you have many solutions for your problems, just choose the  one you need, because it's not clear :

1) If you use the DialogBox in resource :  Go in the dialog box   properties in Styles and remove the check in System menu.

2) If it's just a disable (gray) X button you what, put in your     OnInitDialog() function :

   CMenu * ptr_system;
   ptr_system = GetSystemMenu(FALSE);
   ptr_system->EnableMenuItem(1, MF_GRAYED | MF_BYPOSITION);

If you need something else, like for create modeless dialog, let me know...
This is for MFC (disabling SC_CLOSE):

void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
    if (nID == SC_CLOSE)
      CDialog::OnSysCommand(nID, lParam);

And yes, the following grayed it out:
    pSysMenu = GetSystemMenu(FALSE);
    pSysMenu->EnableMenu(SC_CLOSE,MF_GRAYED |                                                           MF_BYCOMMAND);


if you use this in OnSysCommand(), in the if you have on error
you need to write :

if ((nID & 0xfff0) == SC_CLOSE)
  CDialog::OnSysCommand(nID, lParam)

Just becarefull all the SC_ constant need a mask 0xfff0, otherwise you will have some surprise.

shibaiAuthor Commented:
Thanks, tflai! Thanks, shogi, you are great!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.