Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to disable default OnOK and OnCancel in MFC Dialog

Posted on 2001-07-27
12
Medium Priority
?
2,153 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:Sundar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
12 Comments
 
LVL 2

Expert Comment

by:smitty1276
ID: 6330728
Go into the resources editor and right click on the buttons and delete them.
0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6332063
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()
{
}
0
 

Author Comment

by:Sundar
ID: 6333158
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Sundar
ID: 6333160
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.
0
 

Author Comment

by:Sundar
ID: 6333162
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.
0
 

Author Comment

by:Sundar
ID: 6333163
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.
0
 

Author Comment

by:Sundar
ID: 6333167
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.
0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6335038
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.
0
 

Author Comment

by:Sundar
ID: 6335064
Yes jdrescher u are absolutely correct. Please get me the solution for that problem!!
0
 
LVL 1

Accepted Solution

by:
jdrescher earned 120 total points
ID: 6335231
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);
      }
     
}



0
 
LVL 1

Expert Comment

by:jdrescher
ID: 6335236
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);
      }
     
}



0
 

Author Comment

by:Sundar
ID: 6336766
Thats Really great Thank u jdrescher. Thank u very much for ur answer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question