Win32键盘加速器不工作C ++

时间:2014-03-15 17:46:20

标签: c++ windows winapi handle

我一直在尝试在Win32应用程序中创建键盘快捷键。这是一个F5刷新快捷方式。

我一直关注此示例:http://msdn.microsoft.com/en-us/library/windows/desktop/ms646337%28v=vs.85%29.aspx#wm_command

我已将其包含在" menu.rc"文件:

IDR_MYACC ACCELERATORS
BEGIN
    VK_F5, ID_EDIT_REFRESH, VIRTKEY
END

这是在" main.cpp"文件:

HWND   hwnd;
HANDLE hinstAcc;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("CPUTemp");
    MSG        msg;
    WNDCLASSEX wc;
    BOOL       bRet;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON));
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_MYICON), 
                                        IMAGE_ICON, 16, 16, 0);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // use default colour as window background
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MYMENU);
    wc.lpszClassName = szAppName;

    if (!RegisterClassEx(&wc)) {
        MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, szAppName, "CPU Temperature", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 150, NULL, NULL, hInstance, NULL);

    ShowWindow(GetConsoleWindow(), 0);
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    HACCEL haccel = LoadAccelerators(hinstAcc, MAKEINTRESOURCE(IDR_MYACC));

    if (haccel == NULL) {
        MessageBox(NULL, "Accelerator Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // get and dispatch messages until a WM_QUIT message is received
    while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    {
        if (bRet == -1) {
            MessageBox(NULL, "Message Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
            return 0;
        } else {
            // Check for accelerator keystrokes
            if (!TranslateAccelerator( 
                hwnd,   // handle to receiving window 
                haccel, // handle to active accelerator table 
                &msg))  // message data 
            {
                TranslateMessage(&msg); // translate virtual-key messages into character messages
                DispatchMessage(&msg);  // send message to window procedure 
            }
        }
    }

    return msg.wParam; // the program return-value is 0 - value that PostQuitMessage() gave
}

编译时出现问题 - 我收到错误消息"无效转换来自' HANDLE又名无效'至于' HINSTANCE'对于这一行:

HACCEL haccel = LoadAccelerators(hinstAcc, MAKEINTRESOURCE(IDR_MYACC));

但如果我尝试更换" hinstAcc"使用" hInstance",它编译并运行,但加速器最终为NULL。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

编译问题:

使用hInstance,或者更好,使用GetModuleHandle(0)并放弃愚蠢,冗长和非标准WinMain(使用标准main代替

更一般地说,只需查阅有关Windows API函数参数的文档

e.g。谷歌LoadAccelerators,点击MSDN文档的链接。


RE

  

“如果我尝试更换" hinstAcc"使用" hInstance",它编译并运行,但加速器最终为NULL。“

我无法在提供的代码中看到任何原因。

也许您忘了链接资源。