Win16 API   /   サブメニュー

ホーム    Windows API Primer

サブメニュー

MF_POPUP に MF_POPUP を追加してサブメニューを作ることができます。

submenu.c


#include <windows.h>

#define CURRY   1
#define HAMBURG 2
#define COFFEE  3
#define JUICE   4

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void    AddMenu         (HWND);

int     PASCAL   WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
{
    WNDCLASS wc;
    HWND     hwnd;
    MSG      msg;
    
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = NULL;
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "SUBMENU";
    
    RegisterClass(&wc);
    
    hwnd             = CreateWindow("SUBMENU",
                                    "SubMenu",
                                    WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                                    CW_USEDEFAULT, CW_USEDEFAULT, 300, 200,
                                    NULL, NULL, hInstance, NULL);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        DispatchMessage(&msg);
    }
    
    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HWND label;
           RECT rect;
    switch (msg)
    {
    case WM_CREATE:
        AddMenu(hwnd);
        GetClientRect(hwnd, &rect);
        label = CreateWindow("STATIC",
                             "",
                             WS_CHILD | WS_VISIBLE | SS_CENTER,
                             (rect.right  - 60) / 2,
                             (rect.bottom - 20) / 2,
                             60, 20, hwnd, NULL, NULL, NULL);
        return 0;
    case WM_SIZE:
        GetClientRect(hwnd, &rect);
        SetWindowPos (label, HWND_TOP,
                      (rect.right  - 60) / 2,
                      (rect.bottom - 20) / 2,
                      0, 0, SWP_NOSIZE);
        return 0;
    case WM_COMMAND:
        switch (wParam)
        {
        case CURRY:
            SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Curry");
            break;
        case HAMBURG:
            SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Hamburg");
            break;
        case COFFEE:
            SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Coffee");
            break;
        case JUICE:
            SendMessage(label, WM_SETTEXT, 0, (LPARAM)"Juice");
            break;
        }
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    
    return DefWindowProc(hwnd, msg, wParam, lParam);
}

void AddMenu(HWND hwnd)
{
    HMENU menubar   = CreateMenu();
    HMENU menumenu  = CreateMenu();
    HMENU menufood  = CreateMenu();
    HMENU menudrink = CreateMenu();
    
    AppendMenu(menumenu,  MF_POPUP, (UINT)menufood,  "Food");
    AppendMenu(menumenu,  MF_POPUP, (UINT)menudrink, "Drink");
    AppendMenu(menubar,   MF_POPUP, (UINT)menumenu,  "Menu");
    AppendMenu(menufood,  MF_STRING, CURRY,          "Curry");
    AppendMenu(menufood,  MF_STRING, HAMBURG,        "Hamburg");
    AppendMenu(menudrink, MF_STRING, COFFEE,         "Coffee");
    AppendMenu(menudrink, MF_STRING, JUICE,          "Juice");
    
    SetMenu   (hwnd, menubar);
}

    


実行結果

メニュー項目をクリックすると、そのメニューの名前がラベルに表示されます。



14663 visits
Posted: Jul. 21, 2020
Update: Jul. 21, 2020

ホーム        Windows API Primer     目次