Solved

Modeless dialog disappears in MFC, NOT caused by OnOK.

Posted on 2006-10-30
5
865 Views
Last Modified: 2013-11-20
I have a modeless dialog box derived from CDialog with OnOK and OnCancel both overridden as {}. However, if the dialog itself has focus (ie not a button, radiobox, etc) and the user presses SPACE, RETURN, or ENTER the box becomes invisible but is not destroyed. Any idea of what messages/commands I should be looking for that would cause this to happen?
0
Comment
Question by:rosenav
  • 2
5 Comments
 
LVL 1

Expert Comment

by:mac-will01
ID: 17839524
This is strange behaviour.  I just quickly tried a dummy project and I found that I didn't get this behaviour.
Can you post some code maybe?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 17840326
Override PreTranslateMessage function:

BOOL CMyDlg::PreTranslateMessage(MSG* pMsg)
{
    if( pMsg->message == WM_KEYDOWN )
    {
        if( pMsg->wParam == VK_RETURN ||  pMsg->wParam == VK_ESCAPE )  
        // AFAIK, Enter or Escape close the dialog. You can add other keys to ignore, for example, VK_SPACE
        {
            ::TranslateMessage(pMsg);
            ::DispatchMessage(pMsg);
            return TRUE;
        }
    }
      
    return CDialog::PreTranslateMessage(pMsg);
}
0
 

Author Comment

by:rosenav
ID: 17843099
Great suggestion AlexFM. Didn't solve my problem but it did show that the dialog isn't recieving RETURN or ESC keys. In fact, nothing on the dialog has the little dotted-line focus highlight painted on it until I specifically click one of the controls. After that point it behaves as expected (doesn't disappear anymore). Is there a reliable way to force a control to have the focus?
0
 

Author Comment

by:rosenav
ID: 17843340
I still don't know why the dialog was disappearing but I was able to make it stop by forcing an arbitrary control in the dialog to have the focus.
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Handling string inputs in C/Linux 23 168
Host to IP 7 73
twoTwo  challenge 35 87
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 92
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

743 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

15 Experts available now in Live!

Get 1:1 Help Now