Solved

How to disable default OnOK and OnCancel in MFC Dialog

Posted on 2001-07-27
12
1,871 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now