Link to home
Start Free TrialLog in
Avatar of Sundar
Sundar

asked on

How to disable default OnOK and OnCancel in MFC Dialog

I'm a Beginner of MFC Programming. I opened new MFC (Single Dialog) that dialog will close when I press Esc/Enter Keys( Note: I Deleted the default OK and Cancel Buttons from the Dialog). Some of my friends are saying that it is due to default OnOK and OnCancel Functions in MFC(CDialog Based classes). If it is the case how can I over come that function so,that My dialog should not close for Esc/Enter key strokes

Note: Plese Explain with Appropriate coding
Avatar of smitty1276
smitty1276

Go into the resources editor and right click on the buttons and delete them.
Provide definitions for the OnOk() and OnCancel virtual functions and then use EndDialog( nID ) to close your dialog when you need to.

void CMyDialog::OnOK()
{
}

void CMyDialog::OnCancel()
{
}
Avatar of Sundar

ASKER

Hai jdrescher,
I'm very sorry to reject this answer I tried this already the problem is I cannot end the dialog when I press the close button from the Title bar or from the system menu please give me a way to achive this.
Avatar of Sundar

ASKER

Hai jdrescher,
I'm very sorry to reject this answer I tried this already the problem is I cannot end the dialog when I press the close button from the Title bar or from the system menu please give me a way to achieve this.
Avatar of Sundar

ASKER

Hai jdrescher,
I'm very sorry to reject this answer I tried this already the problem is I cannot end the dialog when I press the close button from the Title bar or from the system menu please give me a way to achieve this.
Avatar of Sundar

ASKER

Hai jdrescher,
I'm very sorry to reject this answer I tried this already the problem is I cannot end the dialog when I press the close button from the Title bar or from the system menu please give me a way to achieve this.
Avatar of Sundar

ASKER

Hai jdrescher,
I'm very sorry to reject this answer I tried this already the problem is I cannot end the dialog when I press the close button from the Title bar or from the system menu please give me a way to achieve this.
I thought that was what you were trying to achieve... I'll get back to you with a simple solution that allows the dialog to close using the system menu and taskbar but will not close using the esc or enter keys.
Avatar of Sundar

ASKER

Yes jdrescher u are absolutely correct. Please get me the solution for that problem!!
ASKER CERTIFIED SOLUTION
Avatar of jdrescher
jdrescher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Modify your OnSysCommand() member function so it looks like the following:


void CMyDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
   if ((nID & 0xFFF0) == IDM_ABOUTBOX)
   {
      CAboutDlg dlgAbout;
      dlgAbout.DoModal();
   }
   else
   {
      switch(nID) {
      case SC_CLOSE:
         EndDialog(IDCANCEL);
         break;
      default:
         CDialog::OnSysCommand(nID, lParam);
      }
     
   }
}

If you don't have a OnSysCommand(UINT nID, LPARAM lParam) function do the following:

1)add afx_msg void OnSysCommand(UINT nID, LPARAM lParam); to your header file right after //}}AFX_MSG

2)add ON_WM_SYSCOMMAND() right after      //}}AFX_MSG_MAP in your cpp file

3) add the folowing code for your OnSysCommand(UINT nID, LPARAM lParam) in the cpp file:

void CMyDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
      switch(nID) {
      case SC_CLOSE:
         EndDialog(IDCANCEL);
         break;
      default:
         CDialog::OnSysCommand(nID, lParam);
      }
     
}



Avatar of Sundar

ASKER

Thats Really great Thank u jdrescher. Thank u very much for ur answer