Win32 LB_GETTEXT returns garbage(Win32 LB_GETTEXT 返回垃圾)
问题描述
我遇到的问题很可能是一个简单的问题,但对我来说仍然是个问题.我在 Win32/C++ 中使用列表框,当从我的列表框中获取选定的文本时,返回的字符串只是垃圾.它是一个结构或类似的句柄?
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 就是这样做的.它只是获取当前列表框中的项目数.
GetItemCount just does that. It just gets the number of items currently in the listbox.
我从列表框中获取的字符串是测试字符串",它返回了 ¨±é» Tzã
The string I was grabbing from the Listbox is "Test String" and it returned ¨±é» Tzã
如有任何帮助,谢谢.
好的,我将范围缩小到我的 GetSelected 函数,因为 GetString 返回了正确的字符串.
Ok, I narrowed it down to my GetSelected function as GetString returns the correct string.
推荐答案
LB_GETSEL 消息不返回所选项目的索引,它返回您在 WPARAM 中传递的 ITEM 的所选状态.
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.
您还有一个严重的错误,如果没有选择任何项目,您将尝试检索索引 -1 处的项目字符串,这显然是错误的.检查这些 SendMessage 调用的返回值将有助于您诊断问题.
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);
或者,您可以使用 LB_GETSELITEMS 来获取所选项目的列表.
Alternatively you can use LB_GETSELITEMS to get a list of the items that are selected.
这篇关于Win32 LB_GETTEXT 返回垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Win32 LB_GETTEXT 返回垃圾
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01