Solved

How to disable default OnOK and OnCancel in MFC Dialog

Posted on 2001-07-27
12
1,973 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ getting the first 10 characters of a char* string 11 104
Unable to start eclipse ? 17 163
Header of docx file 17 129
Can Live bindings change TGrid Cell Colour ? 1 30
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

762 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