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总是少掉一个字符??求大神帮忙啊啊
8 个解决方案
#1
那就GetWindowText(hEdit,strText,len+1);试试?
#2
len+1?
#3
或者gettext直接填MAX_PATH也行
#4
是\0问题?自己多尝试几次。主要是方法有时候是解释的特明白
#5
CString src;
GetWindowText(src);
int len = src.GetLength();
lstrcpy(strText, src.GetBuffer(len));
src.ReleaseBuffer();
#6
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.
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.
#7
字符串后面都会有个结束符'\0'会占一个字节的长度,GetWindowTextLength函数返回的是不包含结束符的长度,所以应该是len+1
#8
已经解决了 谢谢各位的回答
因为是UNICODE 宽字节的 一个字符占两个字节 应该是
GetWindowText(hEdit,strText,len*2);
因为是UNICODE 宽字节的 一个字符占两个字节 应该是
GetWindowText(hEdit,strText,len*2);
#1
那就GetWindowText(hEdit,strText,len+1);试试?
#2
len+1?
#3
或者gettext直接填MAX_PATH也行
#4
是\0问题?自己多尝试几次。主要是方法有时候是解释的特明白
#5
CString src;
GetWindowText(src);
int len = src.GetLength();
lstrcpy(strText, src.GetBuffer(len));
src.ReleaseBuffer();
#6
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.
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.
#7
字符串后面都会有个结束符'\0'会占一个字节的长度,GetWindowTextLength函数返回的是不包含结束符的长度,所以应该是len+1
#8
已经解决了 谢谢各位的回答
因为是UNICODE 宽字节的 一个字符占两个字节 应该是
GetWindowText(hEdit,strText,len*2);
因为是UNICODE 宽字节的 一个字符占两个字节 应该是
GetWindowText(hEdit,strText,len*2);