Disable dialog cancel box

How can I disable the cancel box of a dialog on the upper right corner?
Who is Participating?
tflaiConnect With a Mentor Commented:
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.

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.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.