Hot tracking list item selection in a combo box

本秂侑毒 提交于 2019-12-02 08:52:11

问题


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.



来源:https://stackoverflow.com/questions/36287605/hot-tracking-list-item-selection-in-a-combo-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!