EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+

#Previous question #Index of VC++/WIN32/MFC Questions #Next question


There are ways to add keyboard shortcuts to menus and these are called Accelerators. Accelerators are application resource. It contains one or more mappings of key combination to menu items. Very popular key shortcuts in notepad or any editor applications are -

  • Ctrl+N
  • Ctrl+O
  • Ctrl+S

Accelerators resource editor

Like all other resoures in VC++ project, accelerators are another type of resource and VC++ editor provides an editor for this. A new accelerator can be created using "New Resource" or an existing Accelerator can be edited in Resource tab in the project exploler.

Accelerators resource editor in VC++

Accelerators APIs

LoadAccelerators, TranslateAccelerator, TranslateMessage are most common Win32 APIs used for Accelerators. Lets see the details of these APIs and how to use these. Later we have a sample code where we have utilized these functions.

LoadAccelerators function

Loads the specified accelerator table and returns the handle to accelerator table.

HACCEL WINAPI LoadAccelerators(
 HINSTANCE hInstance,
 LPCTSTR   lpTableName


  • hInstance - A handle to the module whichfile contains the accelerator table to be loaded.
  • lpTableName - The name of the accelerator table as string to be loaded. MAKEINTRESOURCE macro can be used if resource ID is given instead of name.

Return value

handle to the loaded accelerator table on success or NULL in failure.


Accelerator tables loaded from resources are freed automatically when the application terminates.

TranslateAccelerator function

Processes accelerator keys for menu commands. The function translates a WM_KEYDOWN or WM_SYSKEYDOWN message to a WM_COMMAND or WM_SYSCOMMAND message (if there is an entry for the key in the specified accelerator table) and then sends the WM_COMMAND or WM_SYSCOMMAND message directly to the specified window procedure. TranslateAccelerator does not return until the window procedure has processed the message.

int WINAPI TranslateAccelerator(
  HWND   hWnd,
  HACCEL hAccTable,
  LPMSG  lpMsg


  • hWnd - A handle to the window whose messages are to be translated.
  • hAccTable - A handle to the accelerator table loaded with LoadAccelerators().
  • lpMsg -A pointer to an MSG structure that contains message information retrieved from the calling thread's message queue using the GetMessage or PeekMessage function.

Return value

  • If the function succeeds, the return value is nonzero.
  • If the function fails, the return value is zero. To get extended error information, call GetLastError.
  • TranslateMessage function

    Translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.

    BOOL WINAPI TranslateMessage(
       const MSG *lpMsg


    • lpMsg - A pointer to an MSG structure that contains message information retrieved from the calling thread's message queue by using the GetMessage or PeekMessage function.

    Return value

    • If the message is translated (that is, a character message is posted to the thread's message queue), the return value is nonzero.
    • If the message is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP, the return value is nonzero, regardless of the translation.
    • If the message is not translated (that is, a character message is not posted to the thread's message queue), the return value is zero.

    Sample code

    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
      MSG msg;
      HACCEL hAccelTable;
      /* Load the Accelerator table */
    10    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ACC);
    12    while (GetMessage(&msg, NULL, 0, 0)) 
    13    {
    14        /* Check and translate the msg key if present in table */
    15      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    16      {
    17          /* Translate the message and dispatch */
    18        TranslateMessage(&msg);
    19        DispatchMessage(&msg);
    20      }
    21    }
    23    return msg.wParam;
    24  }
    26  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    27  {
    28    int wmId, wmEvent;
    30    switch (message) 
    31    {
    32      case WM_COMMAND:
    33        wmId    = LOWORD(wParam); 
    34        wmEvent = HIWORD(wParam); 
    35        /* Parse the menu selections: */
    36        switch (wmId)
    37        {
    38          case IDM_ABOUT:
    39             DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
    40             break;
    41          case IDM_EXIT:
    42             DestroyWindow(hWnd);
    43             break;
    44          default:
    45             return DefWindowProc(hWnd, message, wParam, lParam);
    46        }
    47        break;
    49      case WM_DESTROY:
    50        PostQuitMessage(0);
    51        break;
    52      default:
    53        return DefWindowProc(hWnd, message, wParam, lParam);
    54     }
    55     return 0;
    56  }

    You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

     Vote 0

    Similar topics related to this section

    #Programming Windows, Fifth Edition (Developer Reference)
    #Programming Applications for Microsoft Windows (Microsoft Programming Series)
    #Programming Windows with MFC, Second Edition
    #Visual C++ Programming
    #Programming Microsoft Visual C++, Fifth Edition

    * #1 webmaster Sun 27 Apr/2014 22:38:26(GMT)  Like 0 Unlike 0

    Dear Users,

    We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.


    Your message goes here:

    My Post:*
    Secutiry Code: ******  *
    Preview this compose before posting this in discussion forum.
    1. Use [Sxx] code to show smiles. Example [S02]
    2. Use [URL ], example [URL http://www.google.com/]
    3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
     [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
     [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
     [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

    Note: Only members are allowed, * fields are mandatory.