将WM_CHANGEUISTATE发送到窗口应用程序后,当使用鼠标访问菜单时,系统菜单上不显示助记符

时间:2017-11-16 10:28:27

标签: c++ c windows winapi

下面的代码使用参数向窗口过程本身发送WM_CHANGEUISTATE:

LOWORD(wParam)= UIS_CLEAR

HIWORD(wParam)= UISF_HIDEACCEL

lParam = 0x00000000

当用鼠标左键单击窗口客户区时。

根据this blog Raymond Chen,当使用鼠标访问菜单时,这应该显示系统菜单中的助记符。以下段落摘自本文:

  

清除标记会显示相应的指示符。例如,如果你   UISF_HIDEFOCUS有一个UIS_CLEAR,这意味着你要显示   重点指标。

在我的情况下,我有一个UIS_CLEAR用于UISF_HIDEACCEL,这意味着我想显示菜单加速器。

如果您运行下面的代码并在应用客户端区域单击鼠标左键,则应在系统菜单中显示加速器,即使使用鼠标访问此菜单也是如此。但是这种情况不会发生,即如果通过左键单击窗口的图标激活系统菜单,或者右键单击窗口的标题栏,系统菜单中的助记符将不会显示。我错过了什么?

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pszCmdLine, int nCmdShow)
{
    WNDCLASSEX  wndclassx;
    wchar_t szAppName[] = L"WM_CHANGEUISTATE";

    wndclassx.cbSize = sizeof(WNDCLASSEX);
    wndclassx.style = CS_HREDRAW | CS_VREDRAW;
    wndclassx.lpfnWndProc = WndProc;
    wndclassx.cbClsExtra = 0;
    wndclassx.cbWndExtra = 0;
    wndclassx.hInstance = hInstance;
    wndclassx.hIcon = 0;
    wndclassx.hCursor = LoadCursor(0, IDC_ARROW);
    wndclassx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wndclassx.lpszClassName = szAppName;
    wndclassx.lpszMenuName = nullptr;
    wndclassx.hIconSm = 0;

    if (!RegisterClassEx(&wndclassx)) return 0;

    HWND hWnd = CreateWindow(szAppName, szAppName, WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
                             CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInstance, 0);

    ShowWindow(hWnd, SW_MAXIMIZE);
    UpdateWindow(hWnd);

    MSG msg;
    while (GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
        case WM_LBUTTONUP:
        {
            BOOL b;

            // Note that in my system (Windows 10) I have:
            //
            // Control Panel > Ease of Access > Ease of Access Center > Make the keyboard easier
            //
            // and the option "Underline keyboard shortcuts and access keys" unmarked (the default). Therefore, the value
            // returned in b below will be 0x00000000 (FALSE).

            SystemParametersInfo(SPI_GETKEYBOARDCUES, 0, &b, 0);

            // If b = FALSE, the SendMessage() below should underline the accelerators in the System menu, when this menu is
            // accessed with the mouse. But that doesn't work. Why?

            if( !b ) SendMessage(hwnd, WM_CHANGEUISTATE, MAKEWPARAM(UIS_CLEAR, UISF_HIDEACCEL), NULL);
        }
        break;

        // Setting a break in the return below, one can see that WM_CHANGEUISTATE message is being sent to the window and
        // passed to DefWindowProc().

        case WM_CHANGEUISTATE:
        return DefWindowProc(hwnd, message, wParam, lParam);


        case WM_DESTROY:
        PostQuitMessage(0);
        break;

        default:
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

这似乎是Windows中的错误/疏忽。 DefWindowProc在显示系统菜单之前未发送WM_QUERYUISTATE。菜单实现直接检查最后一个输入事件,如果它来自键盘则显示下划线。

我尝试发送并发布WM_CHANGEUISTATEWM_UPDATEUISTATE以回复WM_ENTERMENULOOPWM_INITMENUPOPUPWM_NCRBUTTONDOWNWM_SYSCOMMAND而没有任何运气。< / p>

我能够想出的唯一解决方法是通过将HTSYSMENU更改为SC_MOUSEMENU来破解SC_KEYMENU /图标菜单:

case WM_SYSCOMMAND:
  if ((wParam & 0xFFF0) == SC_MOUSEMENU)
  {
    return SendMessage(hwnd, WM_SYSCOMMAND, SC_KEYMENU, ' ');
  }
  return DefWindowProc(hwnd, message, wParam, lParam);

SC_KEYMENUDefWindowProc中有特殊处理,强制下划线(如果适用)。

这不会处理右键单击图标,标题或任务栏...