Win32 API Window messages WM_COMMAND


Example

Sent to a window procedure when:

  • the user selects an item from a menu
  • a control sends a notification to its parent window
  • an accelerator keystroke is translated
Message SourceHIWORD(wp)LOWORD(wp)lp
Menu0Menu ID (IDM_*)0
Accelerator1Accel ID (IDM_*)0
Controlnotification codeControl idHWND of control window

For example, in Notepad, when a user clicks "File->Open" a dialog box is displayed to allow the user to open a file. Menu items are processed in the window procedure's WM_CREATE message like this:

LRESULT CALLBACK winproc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp)
{
    switch (wm) {
        case WM_COMMAND:
            switch (LOWORD(wp) {
                case ID_FILE_OPEN:
                    /* show file open dialog */
                    break;
                case ID_FILE_NEW:
                    /* create new instance */
                    break;
            }
            return 0;
   }
   return DefWindowProc(hwnd, wm, wp, lp);
}