Hot tracking list item selection in a combo box

2020-04-27 02:16发布

问题:

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 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.

Illustration

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

回答1:

This is c++ subclass based on c# article which you mentioned:

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("\n");
        }
    }

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

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

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

This was tested on Windows 10.

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