Dialog Box PreTranslateMessage

   When Dialog boxes in MFC are created using the resource editor, they will have two default Command Buttons. One with caption as OK and other one with CANCEL. When a class is created, both these buttons will be created with default handlers. When any one of them is clicked, they will close the dialog. Just removing/renaming these buttons will be enough to change their behavior.

   There are two key press events which produce a similar result. By default when a dialog is created, the ENTER and ESCAPE keys are considered as shortcuts for the OK and CANCEL buttons. When any of these keys are pressed, the dialog box will be closed.

  To avoid this, the default behavior of the dialog should be modified. The incoming ENTER and ESCAPE key down messages are to be nullified, inside the PreTranslateMessage function. The following is the sample code required for this purpose.

     BOOL DialogName::PreTranslateMessage(MSG* pMsg)
     {
          // TODO: Add your specialized code here and/or call the base class
          if(pMsg->message==WM_KEYDOWN)
          {
              if(pMsg->wParam==VK_RETURN || pMsg->wParam==VK_ESCAPE)
                  pMsg->wParam=NULL ;
          }

          return CDialog::PreTranslateMessage(pMsg);
       }

   If this code is used in PreTranslateMessage, all the ENTER and ESCAPE messages will be nullified and the dialog will not be closed when these keys are pressed.
 

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > c > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction