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 -
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.
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.
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.
handle to the loaded accelerator table on success or NULL in failure.
Accelerator tables loaded from resources are freed automatically when the application terminates.
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.
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.
- 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.
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...
Learn on Youtube