ConsoleMenuControl

Go to Home Page

Sets the range for which menu id's will be reported by MENU_EVENTs in the consoles input stream

Syntax

HMENU WINAPI ConsoleMenuControl (
    HANDLE hConOut,
    DWORD cmdIdLow,
    DWORD cmdIdHigh
)

Parameters

hConOut
Handle to a console screen buffer
cmdIdLow
The lowest menu id to report
cmdIdHigh
The highest menu id to report

Return Value

Handle to the console's window menu

Remarks

The range of id's is inclusive of both the low and high.

Using the returned menu handle, an application can add its own items to the window menu. Notifications of user selections of any user items are made via MENU_EVENT records retrived by ReadConsoleInput

Internally the id range is checked in a WM_SYSCOMMAND handler, before any default command window handling of the message. This allows the id range to include and therefore be notified of the standard SC_ * commands associated with WM_SYSCOMMAND. Note however, that there is no way to allow the command window's default processing to take place. The application must mimic any behaviour it takes over (e.g. if the range includes SC_KEYMENU or SC_MOUSEMENU the app is responsible for presenting the menu, etc)

If the specified range excludes the above commands, an application will always receive a notification when the menu is opened (MenuEvent.dwCommandId=WM_INITMENU) and when the menu is closed (MenuEvent.dwCommandId=WM_MENUSELECT).

Example

#include <windows.h>
#include <stdio.h>

typedef HMENU (WINAPI*pfnControl)(HANDLE, DWORD, DWORD);

static const DWORD CLOSE_MENU_ID = 500;

int main()
{
    // get the function
    HMODULE hKernel = GetModuleHandle(TEXT("kernel32.dll"));
    pfnControl consoleMenuControl = (pfnControl)GetProcAddress(hKernel, "ConsoleMenuControl");
    // call it
    HMENU hMenu = consoleMenuControl(GetStdHandle(STD_OUTPUT_HANDLE), CLOSE_MENU_ID, CLOSE_MENU_ID);
    INPUT_RECORD input = {0};
    HANDLE hConin = GetStdHandle(STD_INPUT_HANDLE);
    DWORD numEvents = 0;
    AppendMenu(hMenu, MF_STRING, CLOSE_MENU_ID, TEXT("Exit Program"));
    puts("Reading events, select the menu item to close");
    while(ReadConsoleInput(hConin, &input, 1, &numEvents))
    {
        if(input.EventType == MENU_EVENT)
        {
            printf("Got menu message, id = %lu\n", input.Event.MenuEvent.dwCommandId);
            if(input.Event.MenuEvent.dwCommandId == CLOSE_MENU_ID)
            {
                puts("Closing...");
                break;
            }
        }
    }    
    return 0;
}