Click here to Skip to main content
15,921,697 members
Articles / Desktop Programming / MFC
Article

Preventing "Return" and "Esc" From Closing Dialog

Rate me:
Please Sign up or sign in to vote.
2.56/5 (28 votes)
11 Feb 2005 81.8K   27   11
Preventing the Return and Esc keys from closing a dialog. User will close the dialog via File-> Close or clicking the "x".

Introduction

A dialog-based application by default will close when the user hits Return, Esc, File -> Close, or the "X" in the system menu (top right of dialog/window). If your app is complicated and doesn't have an OK and/or Cancel button in the main window of the app, the user may be surprised when he hits Return (or Esc) accidentally and the app closes on him. Imagine if you were using MS Word and it closed when you hit the "Return" key, especially if you hadn't saved hours of work. Pretty upsetting.

I wanted my app to close only when the user clicked the "X" in the system menu or selected the File-> Close (or Exit) menu item.

The solution is simple.

C++
//Disable OnCancel 
void MyDialog::OnCancel()
{ 
} 

//Disable OnOK 
void MyDialog::OnOK()
{ 
}

//message map event handler executed when user selects the File Ext menu 
item
void MyDialog::OnFileClose()
{
    EndDialog(IDOK);
}

void MyDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;   //if you have an about dialog

        dlgAbout.DoModal();
    }
    //add the following code
    else if ((nID & 0xFFF0) == SC_CLOSE)
    {
        //if user clicked the "X"
        EndDialog(IDOK);   //Close the dialog with IDOK (or IDCANCEL)
        //---end of code you have added   
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a Market research Analyst for John Hancock. I have over 10 years of experience in the stock and bond markets. I am a self taught windows developer. I develope applications of many types. Recently I am working on applications that optimize portfolios using known and special algorithms I have developed.

I also play the drums (my favorite band is Metallica). I play pool and amatuer/semipro chess.

Comments and Discussions

 
GeneralThis is what I do... Pin
Tydia-kun21-Aug-05 5:56
Tydia-kun21-Aug-05 5:56 
QuestionHow can it work ,can you show me? Pin
chengser14-Aug-05 19:30
chengser14-Aug-05 19:30 
GeneralAnother way... Pin
dmsika13-Feb-05 19:30
dmsika13-Feb-05 19:30 
GeneralRe: Another way... Pin
.:floyd:.14-Feb-05 2:45
.:floyd:.14-Feb-05 2:45 
GeneralRe: Another way... Pin
Danila Korablin20-Jul-05 1:07
Danila Korablin20-Jul-05 1:07 
Then use something like this:
BOOL CModemM20Dlg::PreTranslateMessage(MSG* pMsg)
{
//...
    if(pMsg->wParam==VK_RETURN && !(GetFocus()->m_hWnd==GetDlgItem(IDC_ESOMEEDITBOX)->m_hWnd))
        return true;
//...
    return CDialog::PreTranslateMessage(pMsg);
}


Wormhole is the God divided by zero
GeneralAlso... Pin
Shog912-Feb-05 18:04
sitebuilderShog912-Feb-05 18:04 
GeneralRe: Also... Pin
JKallen14-Feb-05 12:14
JKallen14-Feb-05 12:14 
GeneralRe: Also... Pin
Shog914-Feb-05 12:33
sitebuilderShog914-Feb-05 12:33 
GeneralAn alternative solution Pin
.:floyd:.12-Feb-05 0:58
.:floyd:.12-Feb-05 0:58 
GeneralIncorrect testing of nID Pin
Michael Dunn11-Feb-05 16:31
sitebuilderMichael Dunn11-Feb-05 16:31 
GeneralRe: Incorrect testing of nID Pin
JKallen14-Feb-05 12:15
JKallen14-Feb-05 12:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.