Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinAPI - Menu accelerators not working

I'm trying to add accelerators to my menu, when I press 'Ctrl+R' it should send the command ID_VIEW_RESULTS, But it doesn't. It works fine clicking the menu item, but it isn't translating the accelerator, this is what I have:

MyApp.h

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define ID_MAINMENU 101
#define ID_MENUACC  102

#define ID_VIEW_RESULTS 2001

MyApp.rc

#include "MyApp.h"

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

ID_MAINMENU MENU 
BEGIN
    POPUP "&View"
    BEGIN
        MENUITEM "Calculated &Results...\aCtrl+R",  ID_VIEW_RESULTS
    END
END

/////////////////////////////////////////////////////////////////////////////
//
// Accelerator
//

ID_MENUACC ACCELERATORS 
BEGIN
    "^R",       ID_VIEW_RESULTS,        ASCII,  NOINVERT
END

MyApp.cpp

#include "MyApp.h"

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInst);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // Create and show main window, CMainWnd definition is dialog
    // resource template, works fine, irrelevant to problem.
    MainWnd = new CMainWnd();
    MainWnd->Show();

    MSG    Msg;
    HACCEL hAcc;
    hAcc = LoadAccelerators(hInst, MAKEINTRESOURCE(ID_MENUACC));

    while (GetMessage(&Msg, 0, 0, 0)) {
        if (!TranslateAccelerator(Msg.hwnd, hAcc, &Msg)) {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }

    return (int)Msg.wParam;
}

I myself can't see a good reason why this wouldn't work, can anyone point out what I'm doing wrong or give me any suggestions?

like image 512
Brandon Miller Avatar asked Mar 26 '26 20:03

Brandon Miller


1 Answers

Try replacing Msg.hwnd with the HWND member of MainWnd. If that works than Msg.hwnd is not the right window that gets the message (in Tanslate Accelerator parameters).

like image 67
engf-010 Avatar answered Mar 28 '26 23:03

engf-010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!