ayes l
ayes l

Reputation: 53

How to add mouse double click to ListBox

To this code: https://www.dreamincode.net/forums/topic/163804-microsoft-working-with-listboxes-part-i/

It is displaying list and looping nicely.

Sadly my guru never finished his code. So plan would be adding double click detection on a name. How?

        case WM_COMMAND:
        {

            return 0;
        }

Something like this? 11 is this child window where names are.

        case WM_COMMAND:
        {
            if (LOWORD(wparam) == 11) {
                if ((message) == LBN_DBLCLK) {
                    cout << "double click" << endl;
                }
            }
            return 0;
        }

Doesn't work

Upvotes: 3

Views: 660

Answers (2)

Zeus
Zeus

Reputation: 3880

First, according to the documentation:

Parameters

wParam

The LOWORD contains the identifier of the list box. The HIWORD specifies the notification code.

lParam

Handle to the list box.

Remarks

This notification code is sent only by a list box that has the LBS_NOTIFY style.

So in the first step you need to add this style and use HIWORD (wParam) to determine whether to double-click the list.

Then if you need to get the elements of the list, you should not send LB_GETCURSEL to window_handle, but should send it to This->listbox_handle, which is the window handle of the listbox. Then you can get it by sending LB_GETTEXT Text content.

Here is the code:

case WM_COMMAND:
{
    if (HIWORD(wparam) == LBN_DBLCLK) {
        TCHAR temp[100]{};
        int index = SendMessageW(This->listbox_handle, LB_GETCURSEL, 0, 0L);
        SendMessageW(This->listbox_handle, LB_GETTEXT, index, (LPARAM)temp);
        MessageBox(window_handle, temp, L"test", 0);
    }
    return 0;
}

And it works for me:

enter image description here

Upvotes: 2

catnip
catnip

Reputation: 25388

Try replacing:

if ((message) == LBN_DBLCLK)

with:

if (HIWORD (wParam) == LBN_DBLCLK)

Documentation here.

Upvotes: 1

Related Questions