Win32 LB_GETTEXT返回垃圾 [英] Win32 LB_GETTEXT returns garbage

查看:140
本文介绍了Win32 LB_GETTEXT返回垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,这很可能是一个简单的问题,但对我来说仍然是一个问题。我在Win32 / C ++中使用Listbox,当从列表框中获取所选文本时,返回的字符串只是垃圾。它是一个结构或类似的句柄?



下面是代码和一个我得到的例子。

  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就是这样。



我从列表框中抓取的字符串是Test String,它返回¨±é»Tzã

任何帮助都是appericated的,谢谢。



好的,我把它缩小到GetSelected函数,GetString返回正确的字符串。

解决方案

LB_GETSEL消息不返回选定项目的索引,它返回选择的ITEM的STATE在WPARAM中。



您也有一个严重的错误,如果没有选择项目,您将尝试检索索引-1的项目的字符串,这是明显错误。



这里是如何获取第一个选定项目的文本的示例;

  //获取框中的项数。 
count = SendMessage(control,LB_GETCOUNT,0,0);

int iSelected = -1;

//遍历这些项并找到第一个选中的项
for(int i = 0; i {
//检查如果这个项目被选中或没有..
if(SendMessage(control,LB_GETSEL,i,0)> 0)
{
//是的,我们只想要第一个选择所以break 。
iSelected = i;
break;
}
}

//获取所选项目的文本
if(iSelected!= -1)
SendMessage(control,LB_GETTEXT, WPARAM)iSelected,(LPARAM)text);

或者,您可以使用LB_GETSELITEMS获取所选项目的列表。


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.

解决方案

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.

这篇关于Win32 LB_GETTEXT返回垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆