如何在Win32 API中了解所选菜单项

时间:2017-04-20 00:26:39

标签: c++ windows user-interface winapi win32gui

在窗口API中,我有一个弹出菜单,其中包含3个项目" Line"," Circle"和"退出"分别

我的程序是让用户选择要绘制的形状然后获取点,参数(即线的起点和终点,......)。这是我到目前为止编写的代码的一部分。

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
    WPARAM wParam, LPARAM lParam) {

    HMENU hMenu;
    POINT point;
    HDC hdc;
    hdc = GetDC(hwnd);
    static int x1, y1,x2,y2,count = 0;
    switch (msg) {

    case WM_LBUTTONDOWN:
        count++;
        if (count == 1)
        {
            x1 = LOWORD(lParam);
            y1 = HIWORD(lParam);
        }
        else
        {
            x2 = LOWORD(lParam);
            y2 = HIWORD(lParam);

            // I think the problem goes here, it never execute else part 
            //even if global_ID ==2, Am I missing something?

            if (global_ID == 1)//Line
            {DirectMethod(hdc, x1, y1, x2, y2, RGB(0, 0, 0));}
            else if (global_ID == 2)//Circle
            {Ellipse(hdc, x1, y1, x2, y2);}
            count = 0;
        }
    case WM_COMMAND:

        switch (LOWORD(wParam)) {
        case IDM_FILE_LINE:
            global_ID = 1;
            break;
        case IDM_FILE_CIRCLE:
            global_ID = 2;//Global Variable
            break;

        case IDM_FILE_QUIT:

            SendMessage(hwnd, WM_CLOSE, 0, 0);
            break;
        }

        break;

    case WM_RBUTTONUP:

        point.x = LOWORD(lParam);
        point.y = HIWORD(lParam);

        hMenu = CreatePopupMenu();
        ClientToScreen(hwnd, &point);

        AppendMenuW(hMenu, MF_STRING, IDM_FILE_LINE, L"&line");
        AppendMenuW(hMenu, MF_STRING, IDM_FILE_CIRCLE, L"&Circle");
        AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
        AppendMenuW(hMenu, MF_STRING, IDM_FILE_QUIT, L"&Quit");

        TrackPopupMenu(hMenu, TPM_RIGHTBUTTON, point.x, point.y, 0, hwnd, NULL);
        DestroyMenu(hMenu);
        break;

    case WM_DESTROY:

        PostQuitMessage(0);
        break;
    }

    return DefWindowProcW(hwnd, msg, wParam, lParam);
}

我想做的是,基于所选的菜单项(Line,Circle,...),我执行特定的代码片段,这也取决于从用户那里获得鼠标点击(WM_LBUTTONDOWN)。

例如: 如果用户选择" Line",我应该拿两点来绘制该行。

1 个答案:

答案 0 :(得分:0)

您的代码中存在逻辑漏洞:

  1. 即使你不应该点击鼠标,也是如此。当您的select * from dataclass group by name ORDER BY score desc limit 3 处理程序设置WM_COMMAND时,您还没有将global_ID重置为0,因此后续点击可能会跳过count / x1的分配由于y1可能已经count

  2. > 0不是1或2时,您的WM_LBUTTONDOWN处理程序根本不应该做任何事情。

  3. 在致电global_ID / global_ID后,您没有重置DirectMethod(),因此Ellipse()会不断计算点击次数并在每次点击时执行图纸。

  4. 您的WM_LBUTTONDOWN缺少必需的WM_LBUTTONDOWN语句,因此每条break条消息都将落入WM_LBUTTONDOWN代码。

  5. 请改为尝试:

    WM_COMMAND