PRB: Modal Dialog Box Prevents Calls to PreTranslateMessageLast reviewed: July 31, 1997Article ID: Q126874 |
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00
WINDOWS | WINDOWS NTPUBLIC | kbprg kbprb kbcode The information in this article applies to:
NOTE: This article contains references to CWinApp member functions. In the 32-bit version of MFC, the bulk of the code for these functions is actually implemented in the CWinThread class from which CWinApp is derived.
SYMPTOMSPreTranslateMessage is not called when a modal dialog box has been invoked. A modal dialog box has its own message loop, which has no interaction with the application's main message loop. This prevents CWinApp::PumpMessage from being called. PumpMessage is the function that calls PreTranslateMessage.
CAUSEAn application is begun by a call to WinMain. MFC's implementation of WinMain calls CWinApp::Run, which has the application's main message loop. The message loop in Run calls PumpMessage to process messages. PumpMessage retrieves the messages from the application's message queue using the GetMessage API. PumpMessage then calls CWinApp::PreTranslateMessage, which will call PreTranslateMessage for the appropriate CWnd. For details on MFC's message routing mechanism, see MFC TechNote #21 and examine the CWinApp::PreTranslateMessage function in the MFC source code that can be found in APPCORE.CPP. In the 32-bit version of MFC, this code is in CWinThread::PreTranslateMessage and can be found in THRDCORE.CPP. Here is a picture of this process:
------------------------------------- | CWinApp::PumpMessage | ------------------------------------- | | ------------------------------------- | CWinApp::PreTranslateMessage | ------------------------------------- | | ------------------------------------- | CWnd:: PreTranslateMessage | -------------------------------------When a modal dialog box has been invoked, the above sequence is no longer used. A modal dialog box uses the Dialog Manager (the code built into Windows for implementing dialog boxes) to retrieve messages from the application's message queue and process them. In other words, the Dialog Manager takes control of all message processing during the existance of a modal dialog box. Here's a picture of this process:
------------------------------------- | CWinApp::PumpMessage | ------------------------------------- | | ------------------------------------- | CDialog::DoModal | ------------------------------------- | | ------------------------------------- | Dialog Manager's Message Loop | -------------------------------------The PumpMessage has dispatched the message that invoked the dialog box in the first place. PumpMessage will not be called again until the Dialog Manager exits its message loop -- when the modal dialog box has been dismissed. However, a modeless dialog box uses the normal message processing sequence because it uses the application's message loop, not the Dialog Manager's message loop.
RESOLUTIONPreTranslateMessage is generally overidden to check for certain messages and do some specialized or additional processing before these messages are translated and dispatched. Because PreTranslateMessage will not be called while a modal dialog box exists, another technique must be used to achieve this processing. The additional message processing can be achieved by using one of the following two methods:
// The OnInitDialog to initialize m_hwndDialog //CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); ((CMyApp *)AfxGetApp())->m_hwndDialog=m_hWnd;}
// When the dialog is destroyed restore m_hwnDialog to NULL // void CMyDialog::PostNcDestroy(){ CDialog::PostNcDestroy(); ((CMyApp *)AfxGetApp())->m_hwndDialog=NULL;}
// The CWinApp object's constructor to initialize m_hwndDialogCMyApp::CMyApp() { m_hwndDialog=NULL;}
STATUSThis behavior is by design.
REFERENCESBooks Online: MFC TechNote #21. "Meandering Through the Maze of MFC Message and Command Routing" by Paul DiLascia in the July 1995 issue of "Microsoft Systems Journal" (Volume 10, Number 7).
|
Additional reference words: 1.00 1.50 2.00 2.10 2.50 2.51 3.00 3.10 4.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |