组合框中的热跟踪列表项选择

2022-01-02 00:00:00 winapi combobox mfc

我有一个组合框,我需要在用户更改选择时拦截选择的更改,只需将鼠标悬停而无需点击.这是用于显示有关用户悬停的项目的补充信息.

I have a combo box and I need to intercept the changement of the selection while the user changes the selection by just hovering with the mouse without clicking. This is for displaying complementary information about the item the user is hovering over.

CBN_SELCHANGE 不会完成这项工作,因为只有当用户实际上通过单击组合框项目之一更改了选择或当向上/向下键被按下.

CBN_SELCHANGE won't do the job, because this message gets fired only when the user has actually changed the selection by clicking on one of the combo box items or when the up/down keys are pressed.

当用户只是将鼠标悬停在组合框上时,显然不会触发任何消息.

Apparently no message is fired while the user is just hovering over the the combobox.

插图

例如:我需要知道用户何时将鼠标从条目 2 移动到条目 33.

E.g: I need to know when the user moves the mouse from the entry 2 to the entry 33.

推荐答案

这是基于 你提到的c#文章:

LRESULT CALLBACK ComboProc(HWND hwnd, UINT msg, WPARAM wParam, 
    LPARAM lParam, UINT_PTR uIdSubClass, DWORD_PTR)
{
    if (msg == WM_CTLCOLORLISTBOX)
    {
        COMBOBOXINFO ci = { sizeof(COMBOBOXINFO) };
        GetComboBoxInfo(hwnd, &ci);
        if (HWND(lParam) == ci.hwndList)
        {
            int pos = SendMessage(ci.hwndList, LB_GETCURSEL, 0, 0);
            OutputDebugStringA(std::to_string(pos).c_str());
            OutputDebugStringA("
");
        }
    }

    if (msg == WM_NCDESTROY)
    {
        RemoveWindowSubclass(hwnd, ComboProc, uIdSubClass);
    }

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

...
SetWindowSubclass(hComboBox, ComboProc, 0, 0);

这是在 Windows 10 上测试过的.

This was tested on Windows 10.

这只能报告下拉列表中的悬停选择,不能改变选择.

This can only report the hover selection in drop down list, it can't change the selection.

相关文章