Win32 LB_GETTEXT returns garbage

混江龙づ霸主 提交于 2020-01-13 02:43:07

问题


I have a problem which is most likely a simple problem, but neverthe less still a problem for me. I am using the Listbox in Win32 / C++ and when getting the selected text from my listbox the string returned is just garbage. It is a handle to a struct or similar?

Below is the code and an example of what I get.

std::string Listbox::GetSelected() {
int index = -1;
int count = 0;

count = SendMessage(control, LB_GETSELCOUNT, 0, 0);

if(count > 0) {
    index = SendMessage(control, LB_GETSEL, 0, 0);
}

return GetString(index);
}


std::string Listbox::GetString(int index) {
int count = 0;
int length = 0;
char * text;

if(index >= 0) {
    count = GetItemCount();

    if(index < count) {
        length = SendMessage(control, LB_GETTEXTLEN, (WPARAM)index, 0);
        text = new char[length + 1];

        SendMessage(control, LB_GETTEXT, (WPARAM)index, (LPARAM)text);
    }
}
std::string s(text);
delete[] text;

return s;
}

GetItemCount just does that. It just gets the number of items currently in the listbox.

The string I was grabbing from the Listbox is "Test String" and it returned ¨±é» Tzã

Any help is appericated, thanks.

Ok, I narrowed it down to my GetSelected function as GetString returns the correct string.


回答1:


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.



来源:https://stackoverflow.com/questions/739095/win32-lb-gettext-returns-garbage

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