用sendmessage给listbox LB_ADDSTRING失败

还没淹死的鱼 2017-05-08 09:42:16
vs2010创建的windows应用程序,没用mfc,创建一个listbox后,在程序中其它地方sendmessage

case WM_CREATE:

g_hListBox = CreateWindow(L"LISTBOX",
NULL,
WS_CHILD | WS_VSCROLL | WS_TABSTOP | LBS_STANDARD | LBS_OWNERDRAWFIXED | LBS_HASSTRINGS | WS_BORDER | WS_VISIBLE,
20, 300, 700, 300,
hWnd, (HMENU)IDC_LISTBOX_LOG,
((LPCREATESTRUCT)lParam)->hInstance,
NULL);

。。。。。。
iIndex = SendMessage(g_hListBox, LB_ADDSTRING, 0, (LPARAM)L"test...");
SendMessage(g_hListBox, LB_SETCARETINDEX, iIndex, 0);

为什么失败呢?是不是跟没用dialogbox有关?
...全文
421 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
还没淹死的鱼 2017-05-17
  • 打赏
  • 举报
回复
虽然成功了都每次插入都是第一行,i = SendMessage(hList, LB_ADDSTRING, 0, (LPARAM)tbuf); 每次都返回0. 我想追加到最后一行,msnd文档里也是这么说的。 why?
赵4老师 2017-05-09
  • 打赏
  • 举报
回复
楼主的头像比较呆萌。
还没淹死的鱼 2017-05-08
  • 打赏
  • 举报
回复
shit, 查到原因了, 一旦设置了LBS_OWNERDRAWFIXED风格,必须重写DrawItem和MeasureItem函数,同理设置了LBS_SORT风格,也必须重写Co mpareItem函数,他们都是虚函数。 就是它干的! 从旧工程拷贝的代码,被坑了。。。。。。
赵4老师 2017-05-08
  • 打赏
  • 举报
回复
LB_ADDSTRING An application sends an LB_ADDSTRING message to add a string to a list box. If the list box does not have the LBS_SORT style, the string is added to the end of the list. Otherwise, the string is inserted into the list and the list is sorted. LB_ADDSTRING wParam = 0; // not used; must be zero lParam = (LPARAM) (LPCTSTR) lpsz; // address of string to add Parameters lpsz Value of lParam. Pointer to the null-terminated string that is to be added. If you create the list box with an owner-drawn style but without the LBS_HASSTRINGS style, the value of the lpsz parameter is stored as item data instead of the string it would otherwise point to. You can send the LB_GETITEMDATA and LB_SETITEMDATA messages to retrieve or modify the item data. Return Values The return value is the zero-based index of the string in the list box. If an error occurs, the return value is LB_ERR. If there is insufficient space to store the new string, the return value is LB_ERRSPACE. Remarks If you create an owner-drawn list box with the LBS_SORT style but not the LBS_HASSTRINGS style, the system sends the WM_COMPAREITEM message one or more times to the owner of the list box to place the new item properly in the list box. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. See Also List Boxes Overview, List Box Messages, LB_DELETESTRING, LB_INSERTSTRING, LB_SELECTSTRING, WM_COMPAREITEM LB_SETCARETINDEX An application sends an LB_SETCARETINDEX message to set the focus rectangle to the item at the specified index in a multiple-selection list box. If the item is not visible, it is scrolled into view. LB_SETCARETINDEX wParam = (WPARAM) index; // item index lParam = MAKELPARAM(fScroll, 0); // flag for scrolling item Parameters index Value of wParam. Specifies the zero-based index of the list box item that is to receive the focus rectangle. Windows 95 and Windows 98: The wParam parameter is limited to 16-bit values. This means list boxes cannot contain more than 32,767 items. Although the number of items is restricted, the total size in bytes of the items in a list box is limited only by available memory. fScroll Value of lParam. If this value is FALSE, the item is scrolled until it is fully visible; if it is TRUE, the item is scrolled until it is at least partially visible. Return Values If an error occurs, the return value is LB_ERR. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winuser.h. See Also List Boxes Overview, List Box Messages, LB_GETCARETINDEX _T("test...")
还没淹死的鱼 2017-05-08
  • 打赏
  • 举报
回复
引用 1 楼 schlafenhamster 的回复:
iIndex = SendMessage(g_hListBox, LB_ADDSTRING, 0, (LPARAM)L"test..."); iIndex==LB_ERR ??? 还是 iIndex=SendMessage(g_hListBox, LB_SETCARETINDEX, iIndex, 0); iIndex==LB_ERR ???
都是返回0, 没出现过LB_ERR
还没淹死的鱼 2017-05-08
  • 打赏
  • 举报
回复
引用 2 楼 zgl7903 的回复:
1 试试用 TEXT 2 单步调试看看 g_hListBox 是否有效 iIndex = SendMessage(g_hListBox, LB_ADDSTRING, 0, (LPARAM)TEXT("test..."));
试了, g_hListBox是有效的, 用TEXT, 还是不行。 奇怪的是,在界面上好像加上去了,就是文本没显示,第一次iIndex = 0, 第二次为1, 。。。。。。
zgl7903 2017-05-08
  • 打赏
  • 举报
回复
1 试试用 TEXT 2 单步调试看看 g_hListBox 是否有效 iIndex = SendMessage(g_hListBox, LB_ADDSTRING, 0, (LPARAM)TEXT("test..."));
schlafenhamster 2017-05-08
  • 打赏
  • 举报
回复
iIndex = SendMessage(g_hListBox, LB_ADDSTRING, 0, (LPARAM)L"test..."); iIndex==LB_ERR ??? 还是 iIndex=SendMessage(g_hListBox, LB_SETCARETINDEX, iIndex, 0); iIndex==LB_ERR ???
Eleven 2017-05-08
  • 打赏
  • 举报
回复

16,470

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧