Win32 LB_GETTEXT returns garbage

大城市里の小女人 提交于 2019-12-04 08:43:49

The LB_GETSEL message does not return the index of a selected item, it returns the selected STATE of the ITEM you pass in WPARAM.

You also have a serious bug where if no items are selected you will attempt to retrieve the string of the item at index -1, which is clearly wrong. Checking the return values of these SendMessage calls would have helped you diagnose the problem.

Here's an example of how to get the text of the first selected item;

// get the number of items in the box.
count = SendMessage(control, LB_GETCOUNT, 0, 0);

int iSelected = -1;

// go through the items and find the first selected one
for (int i = 0; i < count; i++)
{
  // check if this item is selected or not..
  if (SendMessage(control, LB_GETSEL, i, 0) > 0)
  {
    // yes, we only want the first selected so break.
    iSelected = i;
    break;
  }
}

// get the text of the selected item
if (iSelected != -1)
  SendMessage(control, LB_GETTEXT, (WPARAM)iSelected , (LPARAM)text);

Alternatively you can use LB_GETSELITEMS to get a list of the items that are selected.

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