Solved

Simple question...

Posted on 1998-09-25
8
135 Views
Last Modified: 2013-11-20
Hello everyone,

I am very VERY new to MFC.  I am curious, how does one prevent the main window of the dialog application from being closed by pressing ESC and ENTER?  Whenever I press either of these keys, the application closes.

Thank you.
0
Comment
Question by:joeshmo
8 Comments
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1322628
It looks like you are using AppWizard to create the program (a good choice for a new user! :-)

Here is what you want to do:
1) Open MyApplicationDlg.h (notice the "Dlg", that must be there)
2) Add:
   void OnOK ();
   void OnCancel ();

   just after the first "public:" (no quotes of course).
3) Open MyApplicationDlg.cpp
   Add this to the bottom of it:

   void CMyAppDlg::OnOK ()
   {
   }

   void CMyAppDlg::OnCancel ()
   {
   }

4) Recompile.
5) Run it and test!

If you have any questions about how to implement any of the above, please feel free to ask!
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1322629
Note that there are other ways to do it, but that is probably the easiest.  So if your program doesn't require much processing power, I would do the above.
0
 

Expert Comment

by:umarama
ID: 1322630
Enter equates to clicking on the control that has the current focus. By default, the OK button is built as the default button.
You can uncheck this by going to your resource editor, selecting the control (OK button in this case), go to properties, tab styles and then unchecking the Default Button property. This way the OK button won't get the focus by default and hence by pressing Enter your application won't execute the default OnOK().
The same holds true for the Cancel button. If it gets the focus and you press Enter your app quits.

Now, the solution suggested by thresher_shark is good enough. After all you are going to provide some functionality on these handlers !!

Now, the ESCAPE character translates to Cancel operation by default. If you wish to trap this but at the same time allow the user to click on Cancel and get the desired functionality, here is what you need to do.

Using class Wizard, insert an handler for PreTranslateMessage.

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
// ADD THE FOLLOWING LINES
      if (pMsg->message == WM_KEYDOWN)
      {
            if (pMsg->wParam == VK_ESCAPE)
                  return TRUE;
      }
      return CDialog::PreTranslateMessage(pMsg);
}

Hope you find this useful.
0
 

Expert Comment

by:SHULTZ
ID: 1322631
For OK button you should edit the button proprieties and make the "Default" button unchecked. Default means that pressing the enter key correspond to clicking the button.

For escape button you need to override the translation of WM_KEYDOWN message, because the default behaviour translates the escape to cancel.
With class wizard, override the PreTranslateMessage as follows:

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
 {
      if (pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE)
          return TRUE;
      else
         return CDialog::PreTranslateMessage(pMsg);
 }


0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:polimetla
ID: 1322632
Dear joeshmo,

over write the virtual functions OnOk() and OnCancel();
After doing some thing in the code,
if you want to exit from the dialog box you need to use EndDialog(x);
here x value must not equel to either 1or 2. because Ok or Enter vaules are 1
and Cancel or Esc values are 2. If you say EndDialog(1) or EndDialog(2)
you can't exit from the Dialog.

One more advantage is by using EndDialog()
you can return your required value to the calee program.

If any doubts please ask me.

with love,
Bhavani P Polimetla
bhavani_73@hotmail.com
0
 

Author Comment

by:joeshmo
ID: 1322633
Wow, so much response.  I am rejecting SHULTZ's answer because thresher_shark and umarama answered the question in comments before he did.

thresher_shark - I have implemented your suggestion, and it does exactly what I want.  Please post it as an answer and you can get an A.

umarama - Your solution did not work.  I unchecked the "Default" option but upon running the program and pressing enter, it still exited.  No, the OK button did not have a dotted line within it indicating it had the "focus" (Is that the term?).  Your solution for dealing with the ESC key did work though.

polimetla - Thank you for your suggestion, I have implemented thresher_shark's idea however.
0
 
LVL 6

Accepted Solution

by:
thresher_shark earned 150 total points
ID: 1322634
Wow, thanks.  Glad you liked my answer.

If you have any further questions regarding this, please feel free to ask! Thanks.
0
 

Author Comment

by:joeshmo
ID: 1322635
No, I think your answer about covers it.  Thanks for the input everybody!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

21 Experts available now in Live!

Get 1:1 Help Now