GetWindowText获取的字符少了一个?????

呆神 2012-07-31 09:17:17
我的是win32窗口中,WM_CREATE中创建一个edit:
hEdit=CreateWindowEx(NULL, TEXT("EDIT"), NULL, WS_CHILD|WS_VISIBLE, 0, 0, 200, 100, hWnd, NULL, NULL, NULL);
在编辑框中输入内容,获取编辑框的内容:
TCHAR strText[MAX_PATH];
int len=GetWindowTextLength(hEdit);
GetWindowText(hEdit,strText,len);
获取的长度len没有错,但是内容strText总是少掉一个字符??求大神帮忙啊啊
...全文
251 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
呆神 2012-08-07
  • 打赏
  • 举报
回复
已经解决了 谢谢各位的回答
因为是UNICODE 宽字节的 一个字符占两个字节 应该是
GetWindowText(hEdit,strText,len*2);
小马喝水 2012-07-31
  • 打赏
  • 举报
回复
字符串后面都会有个结束符'\0'会占一个字节的长度,GetWindowTextLength函数返回的是不包含结束符的长度,所以应该是len+1
Gloveing 2012-07-31
  • 打赏
  • 举报
回复
int GetWindowText(
HWND hWnd, // handle to window or control
LPTSTR lpString, // text buffer
int nMaxCount // maximum number of characters to copy
);
Parameters
hWnd
[in] Handle to the window or control containing the text.
lpString
[out] Pointer to the buffer that will receive the text.
nMaxCount
[in] Specifies the maximum number of characters to copy to the buffer, including the NULL character. If the text exceeds this limit, it is truncated.
dog357 2012-07-31
  • 打赏
  • 举报
回复

CString src;
GetWindowText(src);
int len = src.GetLength();
lstrcpy(strText, src.GetBuffer(len));
src.ReleaseBuffer();
傻X 2012-07-31
  • 打赏
  • 举报
回复
是\0问题?自己多尝试几次。主要是方法有时候是解释的特明白
这个娜戒海了 2012-07-31
  • 打赏
  • 举报
回复
或者gettext直接填MAX_PATH也行
这个娜戒海了 2012-07-31
  • 打赏
  • 举报
回复
len+1?
meiguakee 2012-07-31
  • 打赏
  • 举报
回复
那就GetWindowText(hEdit,strText,len+1);试试?

15,978

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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