c ++键盘加速功能不起作用

时间:2014-04-11 14:21:39

标签: c++ c winapi resource-files

我一直在训练如何制作键盘加速键,但是当我尝试它时,它不起作用.. 这是代码:

资源文件::

    #include "resource.h"

    IDM_MAINMENU MENU
    BEGIN
        POPUP "&File"
        BEGIN
            POPUP "&Program"
                BEGIN

                MENUITEM "LED O&N", IDM_PROGRAM_LED_ON
                MENUITEM "LED O&FF", IDM_PROGRAM_LED_OFF
                MENUITEM "LED &BLINK", IDM_PROGRAM_LED_BLINK

                END
            MENUITEM SEPARATOR
            MENUITEM "E&xit", IDM_FILE_EXIT
        END
        POPUP "&Help"
        BEGIN
            MENUITEM "&Info", IDM_INFO
        END

    END


    app_icon   ICON "untitled.ico"

    IDA_ACCEL_TABLE ACCELERATORS
    BEGIN
       "x", IDA_CTRL_X, CONTROL, VIRTKEY
    END

标题文件:

        #define  app_icon              1
        #define  IDM_MAINMENU          2
        #define  IDM_FILE_EXIT         3
        #define  IDA_ACCEL_TABLE       4
        #define  IDD_DIALOG            5
        #define  IDA_CTRL_X            6
        #define  IDM_PROGRAM_LED_OFF   7
        #define  IDM_PROGRAM_LED_BLINK 8
        #define  IDM_PROGRAM_LED_ON    9
        #define  IDM_INFO              10

主.exe文件::

        #include <windows.h>
        #include "resource.h"
        #include "define.h"

        LRESULT CALLBACK WinProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

        int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd){


        window win;
        MSG msg;
        HMENU hMenu;
        hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDM_MAINMENU));
        HACCEL hAccel;
        hAccel = LoadAccelerators(hInst, MAKEINTRESOURCE(IDA_ACCEL_TABLE));

        win.cbClsExtra = NULL;                             //Additional parameters
        win.cbWndExtra = NULL;                            //Additional parameters
        win.hbrBackground = (HBRUSH)COLOR_WINDOW ;                        //Sets background color for the window
        win.hCursor = LoadCursor(NULL, IDC_ARROW);      //The cursor that will appear in the window
        win.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(app_icon));                              //Icon for the window
        win.hInstance = hInst;                        //Handle to the application instance
        win.lpszClassName = "Window Class";          //The unique name of the window class
        win.lpszMenuName = NULL;                    //Used for menus
        win.style = NULL;                          //The look and feel of the window
        win.lpfnWndProc=(WNDPROC)WinProc;         //The callback procedure (more on that later)

        RegisterClass(&win);
        HWND hwnd = CreateWindow("Window Class", "Atmel Microcontroller Programmer", WS_OVERLAPPEDWINDOW, 550, 300, 350, 260, NULL, hMenu, hInst, NULL);
        ShowWindow(hwnd, SW_SHOWNORMAL);

        while(GetMessage(&msg, NULL, 0, 0)){


            if(!TranslateAccelerator(hwnd, hAccel, &msg)){
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }

        }

        return 0;

        }

        LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){

        switch (message){

        case WM_CREATE:{

            CreateWindow("BUTTON", "LED on", WS_VISIBLE | WS_CHILD | WS_BORDER, 20, 20, 70, 50, hWnd, (HMENU)1, NULL, NULL);
            CreateWindow("BUTTON", "LED off", WS_CHILD | WS_VISIBLE | WS_BORDER, 125, 80, 70, 50, hWnd, (HMENU)2, NULL, NULL);
            CreateWindow("BUTTON", "LED blink", WS_CHILD | WS_VISIBLE | WS_BORDER, 240, 140, 70, 50, hWnd, (HMENU)3, NULL, NULL);
            break;

        }

        case WM_COMMAND:{

        if(wParam == 1){

            system("avrdude");


        }

        if(wParam == 2){

            system("avrdude");


        }

        if(wParam == 3){

            system("avrdude");


        }

        if (wParam == IDA_CTRL_X){

            MessageBeep(message);

        }

            break;
        }

        case WM_DESTROY:{

            PostQuitMessage(0);
            return 0;
            break;

        }

        default:{

            return DefWindowProc(hWnd, message, wParam, lParam);

        }
        }


        }

这是我一直在使用的所有文件,但是还有一个头文件,我已经定义了一些小东西,并添加了注释以便我记住,但它是如此之大,我定义的最重要的事情是消息为MessageBox和窗口为WNDCLASS

我知道要努力阅读所有这些,但我真的需要帮助,非常感谢你们。 = d

修改

并且是的,你问我试图从计算机上制作一个程序来控制我将用微控制器制作的电路但是我还没有完成十六进制程序或电路这是我开始的。

1 个答案:

答案 0 :(得分:1)

我相信你的错误就在这里:

if (wParam == IDA_CTRL_X){
    MessageBeep(message);
}

WM_COMMAND的消息来源是加速器时,wParam的高位字是1,低位字是加速器标识符。如果它是菜单,则高位字是0,低位字是菜单标识符。 (见reference。)

所以你需要做的是:

switch (message)
{
    case WM_COMMAND:
       wmId    = LOWORD(wParam);
       wmEvent = HIWORD(wParam);

       switch(wmId)
       {
         case IDA_CTRL_X:
            MessageBeep(message);
            break; 
       }
    break;
}