Sign in to follow this  
Matthew Shockley

C++ Win32 Window Text

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!")


??????

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this