Reputation: 138
How do I get the contents of a textbox in C++?
Upvotes: 3
Views: 1928
Reputation: 1
//unicode std::string or std::wstring
typedef std::basic_string<TCHAR> unicode_string;
unicode_string GetWinString(HWND h)
{
int len = ::GetWindowTextLength(h);
if (len)
{
std::vector<TCHAR> tmp(len + 1,_T('\0'));
::GetWindowText(h,&tmp[0],len + 1);
return &tmp[0];
}
return _T("");
}
Upvotes: 0
Reputation: 11
Correction to last post:
//unicode std::string or std::wstring
typedef std::basic_string<TCHAR> unicode_string;
unicode_string GetWinString(HWND h)
{
int len = ::GetWindowTextLength(h);
if (len)
{
std::vector<TCHAR> tmp(len + 1,_T('\0'));
::GetWindowText(h,&tmp[0],len + 1);
return &tmp[0];
}
return _T("");
}
Upvotes: 1
Reputation: 347196
Use the Win32 API GetWindowText passing in the text box's window handle.
If you want to get the text from another process use WM_GETTEXT instead with SendMessage.
Upvotes: 7