Solved

How to disable default OnOK and OnCancel in MFC Dialog

Posted on 2001-07-27
12
2,007 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
Independent Software Vendors: 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!

 

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 30 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

734 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