Using Accelerators with an MFC Modeless Dialog Box

Last reviewed: October 10, 1997
Article ID: Q117500
1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 4.00
WINDOWS             | WINDOWS NT
kbprg

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and
          1.52
        - Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 2.1, and 4.0
    

SUMMARY

To use accelerators with a modeless dialog box, override the function "PreTranslateMessage()" in your derived CDialog class.

MORE INFORMATION

To use accelerators with your modeless dialog box, perform the following steps:

  1. Create a modeless dialog box. For additional information, please see the following article in the Microsoft Knowledge Base:

          ARTICLE-ID: Q103788
    
          TITLE     : Creating a Modeless Dialog Box with MFC Libraries
    
    

  2. Insert this sample code into the files listed above the code:

       // .H file with your derived CDialog class
       class CModeless : public CDialog
       {
          .
        public:
          virtual BOOL PreTranslateMessage(MSG*);
          .
       };
    
       //.CPP file
       BOOL CModeless::PreTranslateMessage(MSG* pMsg)
       {
          HACCEL hAccel =
          ((CMainFrame*)AfxGetApp()->m_pMainWnd)->GetAccelTable();
          if(!(hAccel &&
               ::TranslateAccelerator(AfxGetApp()->m_pMainWnd, hAccel, pMsg)))
               return CDialog::PreTranslateMessage(pMsg);
          else
             return TRUE;
       }
    
       // MAINFRM.H file, where CMainFrame is the main window class
       HACCEL CMainFrame::GetAccelTable() { return m_hAccelTable; }
    
    

  3. Create the accelerators with App Studio, or with Resource View from the Project Workspace in Visual C++ 32-bit Edition, version 4.0. The accelerators should be in the IDR_MAINFRAME accelerator table. They should also have the same ID as the controls or menu items with which they are associated.


Additional reference words: kbinf 1.00 1.50 1.51 1.52 2.00 2.10 2.50 2.51
2.52 3.00 3.10 4.00
KBCategory: kbprg
KBSubcategory: MfcUI
Keywords : MfcUI kbprg
Technology : kbMfc
Version : 1.00 1.50 1.51 1.52 | 1.00 2.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 10, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.