Creating Accelerators for Font Attributes

The example in this section shows how to perform the following tasks:

  • Create an accelerator-table resource.

  • Load the accelerator table at run time.

  • Translate accelerators in a message loop.

  • Process WM_COMMAND messages generated by the accelerators.

These tasks are demonstrated in the context of an application that includes a Character menu and corresponding accelerators that allow the user to select attributes of the current font.

The following portion of a resource-definition file defines the Character menu and the associated accelerator table. Note that the menu items show the accelerator keystrokes and that each accelerator has the same identifier as its associated menu item.

#include <windows.h>

#include "acc.h"

MainMenu MENU

BEGIN

POPUP "&Character"

BEGIN

MENUITEM "&Regular\tF5", IDM_REGULAR

MENUITEM "&Bold\tCtrl+B", IDM_BOLD

MENUITEM "&Italic\tCtrl+I", IDM_ITALIC

MENUITEM "&Underline\tCtrl+U", IDM_ULINE

END

END

FontAccel ACCELERATORS

BEGIN

VK_F5, IDM_REGULAR, VIRTKEY

"B", IDM_BOLD, CONTROL, VIRTKEY

"I", IDM_ITALIC, CONTROL, VIRTKEY

"U", IDM_ULINE, CONTROL, VIRTKEY

END

The following sections from the application's source file show how to implement the accelerators.

HWND hwndMain; /* handle of main window */

HANDLE hinstAcc; /* handle of application instance */

int WINAPI WinMain(hinst, hinstPrev, lpCmdLine, nCmdShow)

HINSTANCE hinst;

HINSTANCE hinstPrev;

LPSTR lpCmdLine;

int nCmdShow;

{

MSG msg; /* application messages */

HACCEL haccel; /* handle of accelerator table */

.

. /* Perform the initialization procedure. */

.

/* Create a main window for this application instance. */

hwndMain = CreateWindowEx(0L, "MainWindowClass",

"Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,

CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,

hinst, NULL );

/* If a window cannot be created, return "failure." */

if (!hwndMain)

return FALSE;

/* Make the window visible and update its client area. */

ShowWindow(hwndMain, nCmdShow);

UpdateWindow(hwndMain);

/* Load the accelerator table. */

haccel = LoadAccelerators(hinstAcc, "FontAccel");

if (haccel == NULL)

HandleAccelErr(ERR_LOADING); /* application defined */

/*

* Get and dispatch messages until a WM_QUIT message is

* received.

*/

while (GetMessage(&msg, NULL, NULL, NULL)) {

/* Check for accelerator keystrokes. */

if (!TranslateAccelerator(

hwndMain, /* handle of receiving window */

haccel, /* handle of active accel. table */

&msg)) { /* address of message data */

TranslateMessage(&msg);

DispatchMessage(&msg);

}

}

return msg.wParam;

}

LRESULT APIENTRY MainWndProc(hwndMain, uMsg, wParam, lParam)

HWND hwndMain;

UINT uMsg;

WPARAM wParam;

LPARAM lParam;

{

BYTE fbFontAttrib; /* array of font-attribute flags */

static HMENU hmenu; /* handle of main menu */

switch (uMsg) {

case WM_CREATE:

/*

* Add a check mark to the Regular menu item to

* indicate that it is the default.

*/

hmenu = GetMenu(hwndMain);

CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |

MF_CHECKED);

return 0;

case WM_COMMAND:

switch (LOWORD(wParam)) {

/* Process the accelerator and menu commands. */

case IDM_REGULAR:

case IDM_BOLD:

case IDM_ITALIC:

case IDM_ULINE:

/*

* GetFontAttributes is an application-defined

* function that sets the menu-item check marks

* and returns the user-selected font attributes.

*/

fbFontAttrib = GetFontAttributes(

(BYTE) LOWORD(wParam), hmenu);

/*

* SetFontAttributes is an application-defined

* function that creates a font with the

* user-specified attributes the font with

* the main window's device context.

*/

SetFontAttributes(fbFontAttrib);

break;

default:

break;

}

break;

.

. /* Process other messages. */

.

default:

return DefWindowProc(hwndMain, uMsg, wParam, lParam);

}

return NULL;

}

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
MegaDetailed.Net
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database