Tworzenie akceleratorów dla atrybutów czcionki

W przykładzie w tej sekcji przedstawiono sposób do wykonywania następujących zadań:

Te zadania są wykazane w kontekście aplikacji, która zawiera menu znak i odpowiadające im akceleratorów, umożliwiające użytkownikowi zaznacz atrybuty of the current font.

Następujące części pliku definicji zasobów definiuje menu znak i tabeli skojarzone akceleratora. Należy zauważyć, że elementy menu Pokaż klawisze skrótu i że każda akceleratora ma ten sam identyfikator jako jej element menu skojarzone.

# include lt;windows.h > # include "acc.h" MainMenu MENU {POPUP "& znaków" {MENUITEM "& Regular\tF5", IDM_REGULAR MENUITEM "& Bold\tCtrl + B", IDM_BOLD MENUITEM "& Italic\tCtrl + I", IDM_ITALIC MENUITEM "& Underline\tCtrl + U", IDM_ULINE}
} Akceleratory FontAccel {VK_F5, IDM_REGULAR, VIRTKEY IDM_BOLD, kontroli, "B", VIRTKEY "I", IDM_ITALIC, CONTROL, VIRTKEY "U", IDM_ULINE, kontroli, VIRTKEY} 

 

W poniższych sekcjach od pliku źródłowego aplikacji przedstawiono sposób implementacji akceleratory.

HWND hwndMain;      // handle to main window 
HANDLE hinstAcc;    // handle to application instance 
 
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow) 
{ 
    MSG msg;            // application messages 
    HACCEL haccel;      // handle to 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 to receiving window 
                haccel,        // handle to active accelerator table 
                &msg))         // address of message data 
        {
            TranslateMessage(&msg); 
            DispatchMessage(&msg); 
        } 
    } 
    return msg.wParam; 
} 
 
LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam) 
{ 
    BYTE fbFontAttrib;        // array of font-attribute flags 
    static HMENU hmenu;       // handle to 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; 
} 
 

Index