Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
As my subject says, when i call GetWindowText with the string to fill, it comes back with nothing copied into it. I also tried directly sending the message to the edit control and also got nothing. Here is my code. any ideas?

C++
#include <windows.h>
#include <stdio.h>

#pragma comment (lib, "comctl32.lib")
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK printout(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
HWND mainwindow;
HWND editbox;
HWND sendbutton;
HWND printbox;
HFONT hFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);

#define PO_ADDTEXT (WM_USER + 1)

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
	WNDCLASSEX print;
    MSG Msg;

    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH) COLOR_BACKGROUND + 1;
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = "clientstuffs";
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    print.cbSize        = sizeof(WNDCLASSEX);
    print.style         = 0;
    print.lpfnWndProc   = printout;
    print.cbClsExtra    = 0;
    print.cbWndExtra    = 0;
    print.hInstance     = hInstance;
    print.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    print.hCursor       = LoadCursor(NULL, IDC_ARROW);
    print.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(255,255,255));
    print.lpszMenuName  = NULL;
    print.lpszClassName = "printbox";
    print.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

	if(!RegisterClassEx(&print))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    mainwindow = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        "clientstuffs",
        "The title of my window",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 600, 500,
        NULL, NULL, hInstance, NULL);

	editbox = CreateWindowEx(0, "EDIT", "",
                      WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
                      40, 410, 420, 30,
                      mainwindow,
                      (HMENU)5, hInstance, NULL);
	SendMessage(editbox, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));

	
	printbox = CreateWindowEx(0, "printbox", "",
					   WS_VISIBLE | WS_CHILD | WS_BORDER | WS_VSCROLL,
					   40, 40, 420, 360,
					   mainwindow,
					   NULL, hInstance, NULL);

	sendbutton = CreateWindowEx( 0, "BUTTON", "Send",
					WS_VISIBLE | WS_CHILD | BS_CENTER | BS_VCENTER,
					470, 410, 70, 30,
					mainwindow,
					(HMENU)5, hInstance, NULL);
	SendMessage(sendbutton, WM_SETFONT, (WPARAM)hFont, MAKELPARAM(TRUE, 0));

    if(mainwindow == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(mainwindow, nCmdShow);
    UpdateWindow(mainwindow);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static LPTSTR textmessage = "";
	static char buffer[256];

    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;

		case WM_KEYDOWN:
			{
				switch (wParam)
				{
					case VK_RETURN:
					{	
						SendMessage(sendbutton, BM_CLICK, 0, 0);
					}
				}
				break;
			}

		case WM_COMMAND:
			{
				switch(HIWORD(wParam))
				{
					case BN_CLICKED:
					{
						SendMessage(editbox, WM_GETTEXT, (WPARAM) 256, (LPARAM) textmessage);
						//GetWindowText(editbox, textmessage, 256);
						sprintf(buffer,"number of chacacters copied : %i", strlen(textmessage));
						MessageBox(hwnd, buffer, "yerp", MB_OK);
						SetWindowText(editbox, "");
						SendMessage(printbox, PO_ADDTEXT, (WPARAM) textmessage , 0);
					}
				}
				break;
			}
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK printout(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	static PAINTSTRUCT paint;
	static RECT textbox = {20, 20, 373, 200};
	static RECT clientrect;
	static char prompt[50000] = "this is a testing string filled with a bunch of insignifigant words where i fill the string to make it as large as possible so that it makes testing the string in multiple lines possible";
	static char linebreak[3] = "\n\n";
	switch(msg)
	{
		case WM_CLOSE:
		{
                        DestroyWindow(hwnd);
			break;
		}
                case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
		case WM_PAINT:
		{
			hdc = BeginPaint(hwnd, &paint);

				SelectObject(hdc, hFont);
				DrawText(hdc, prompt, strlen(prompt), &textbox, DT_CALCRECT | DT_WORDBREAK);
				DrawText(hdc, prompt, strlen(prompt), &textbox, DT_WORDBREAK);

			EndPaint(hwnd, &paint);
			break;
		}
		case PO_ADDTEXT:
		{
			strcat(prompt, linebreak);
			strcat(prompt, (char*) wParam);
			GetClientRect(printbox, &clientrect);
			InvalidateRect(printbox, &clientrect, true);
			break;
		}
		default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}
Posted
Updated 28-Aug-11 12:21pm
v2
Comments
FatalCatharsis 28-Aug-11 18:43pm    
for the record, don't even worry about the printout control, all i'm trying to do here is get the text that is entered into the edit control saved in a string.
Sergey Alexandrovich Kryukov 28-Aug-11 19:26pm    
There is no GetWindowText in your sample. What's the problem?
--SA

Your problem is with the statement:
C++
static LPTSTR textmessage = "";

LPTSTR is an pointer which you incorrectly assign an empty string and do not allocate any memory for. When using the WM_GETEXT[^] message you need to supply the buffer into which the text is to be placed.

Replace it with the following and it should work.
C++
static char textmessage[256];

Also note that the SendMessage will return the number of characters placed into the textmessage buffer. You could use this number instead of using the strlen call.
 
Share this answer
 
Comments
FatalCatharsis 28-Aug-11 23:17pm    
wow, that was really stupid of me, worked like a charm. thank you very much.
I don't see how this code can work. There is no GetWindowText calls. As to the sending WM_GETTEXT directly, look at what you are doing: you initialize the string textmessage to empty string and trying to get a text into it. It looks like you wanted to use buffer instead.

There could be one deeper problem, but I don't know if you face it or not, because your sample does not really match your question. If by any change you call GetWindowText and later GetWindowText, it won't work if you do it immediately or in the same event handler. If you do so, you don't give a chance for your main application message loop to process messages, so the text is not really set when you call GetWindowText. Should you really do it (which I doubt), it would need a call to some function called "DoEvents" in some hi-level libraries, which does not exist in C++.

See this discussion: http://forums.devx.com/archive/index.php/t-91311.html[^].

—SA
 
Share this answer
 
Comments
FatalCatharsis 28-Aug-11 23:25pm    
yeh, the getwindowtext was commented out while i tried the direct message send, but neither worked. no i didn't mean to use buffer, that was for messagebox specifically, i was just using an LPTSTR cause that's what the function called for, and when i didn't intialize to a value, it caused some errors. i didn't know to try andre's suggestion cause i didn't know a char * could pass as an LPTSTR. Anyway thanks for your assistance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900