C++ Win32 Window Text

Started by
6 comments, last by fastcall22 13 years, 12 months ago
I have created a window:
nWindow = CreateWindow(L"Edit", L"print(\"Hello, World!\")",
	WS_BORDER | WS_CHILD | WS_VISIBLE | ES_MULTILINE | WS_VSCROLL | WS_HSCROLL, 0, 30, width, height-30, hWnd,
	NULL, NULL, NULL);
When a user clicks a button I want to get all the text that the user has typed into this textbox. Do you know how?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Advertisement
TCHAR Buffer[/* insert length of buffer */];int Length = GetWindowText(nWindow, Buffer, sizeof(Buffer)/sizeof(Buffer[0]));


[edit]
MSDN - GetWindowText Function

The code above would need to be handled in the WM_COMMAND message sent by the button press, where nWindow is the handle to your textbox.

(also changed WCHAR to TCHAR)

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

In your WinProc add this code:

...case WM_COMMAND:if(static_cast<HWND>(lParam) == hWndButton && LOWORD(wParam) == BN_CLICKED){    TCHAR buf[MAX_INPUT_LENGTH];    GetWindowText(nWindow, buf, sizeof(buf)/sizeof(buf[0]));    // Use the text here..}break;...


Edit: Ninja'd :(
That didn't work. Can someone give me some example code that takes the ALL of the text in the editbox and put it in a file, named "Test.lua". Everything was working fine until I had to start converting between const char and wchar_t, then its like my brain melted and I am stuck.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
I'm having some flashbacks to another thread started by you - C++ Errors

The GetWindowText is what you want. Store the data in a TCHAR array (as shown in both posts above) and use wcstombs or WideCharToMultiByte to convert the returned text to a CHAR array. To be safe, you'll want the CHAR array to be longer than the TCHAR array, in case you have any UTF-16 characters than need to map to something larger than a single byte.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Thanks, I'm new to Win32 programming, and I am trying to understand it better.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
One more thing... I have this code:

	wchar_t Buffer[100];	HWND nWind = GetSyntaxWindow(); // Returns the TextBox	GetWindowText(nWind, Buffer, sizeof(Buffer)/sizeof(Buffer[0]));	std::ofstream File;	File.open("Test.lua");	File << Buffer;	File.close();


It should be writing:

print("Hello, World!")


To the file, but instead, it is writing:

0021EFE0


That looks like it's memory location so I tried:

File << *Buffer;


An this is what it wrote to the File:

0


Do you know how I can make it write:

print("Hello, World!")


??????

If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
You need to use a wide character stream (wofstream) for wchar_t strings:

#include <iostream>#include <fstream>using namespace std;int main() {	const wchar_t wideMessage[] = L"This is a wide message.";	const char narrowMessage[] = "This is a narrow message.";	ofstream narrowStream( "./narrow.txt" );	narrowStream << narrowMessage << endl 		<< wideMessage << endl;	wofstream wideStream( "./wide.txt" );	wideStream << narrowMessage << endl		<< wideMessage << endl;}


narrow.txt:
This is a narrow message.002FFB34


wide.txt:
This is a narrow message.This is a wide message.

This topic is closed to new replies.

Advertisement