如何回答关于列表框项目中右键的鼠标消息?

时间:2015-04-27 13:22:46

标签: c winapi listbox right-click

如果我使用win32 api,如何回应WM_RBUTTONUP?

case WM_RBUTTONUP:
{
    break;
}
case WM_COMMAND:
    wmId = LOWORD(wParam);
    wmEvent = HIWORD(wParam);
    switch (wmId) {
    case IDE_FRIEND:
    {
        switch (wmEvent) {
        case WM_RBUTTONUP:
        {
            POINT pt;
            GetCursorPos(&pt);
            TrackPopupMenu(g_hMenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, nullptr);
            break;
        }
        default:
            return DefWindowProc(hWnd, msg, wParam, lParam);
        }
        break;
    }

当我点击鼠标右键时,它不会输入'case WM_RBUTTONUP'

'IDE_FRIEND'与列表框相关联。

1 个答案:

答案 0 :(得分:0)

右键单击列表框将为父窗口生成上下文消息。你应该能够像这样抓住它:

case WM_CONTEXTMENU:
{
    if (hListBox == (HWND)wParam)
    {
        int x = LOWORD(lParam);
        int y = HIWORD(lParam);
        TrackPopupMenu(g_hMenu, TPM_RIGHTBUTTON, x, y, 0, hWnd, nullptr);
    }
    break;
}

case WM_RBUTTONUP:
{
    //main window's right-click message
    break;
}

case WM_COMMAND:
{
    //...
}

编辑: 用子类

做同样的事情
#include "CommCtrl.h" //may need to add this
#pragma comment(lib, "comctl32.lib")

LRESULT CALLBACK ListBoxProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp,
                                 UINT_PTR, DWORD_PTR)
{
    switch (msg){

    case WM_RBUTTONUP:
    {
        //add listbox menu here
        break;
    }

    case WM_NCDESTROY://safely remove subclass
    {
        RemoveWindowSubclass(hwnd, ListBoxProc, 0);
        return DefSubclassProc(hwnd, msg, wp, lp);
    }

    }

    return DefSubclassProc(hwnd, msg, wp, lp);
}

LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
    //...
    case WM_CREATE:
        ListBox = CreateWindow(L"listbox", ... )
        SetWindowSubclass(ListBox, ListBoxProc, 0, 0);
    //...
}