GetWindowText()从我的编辑控件中检索不到任何内容 [英] GetWindowText() retrieving nothing from my edit control

查看:81
本文介绍了GetWindowText()从我的编辑控件中检索不到任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我的主题所说,当我用要填充的字符串调用GetWindowText时,它返回时没有复制任何内容.我也尝试过直接将消息发送到编辑控件,但一无所获.这是我的代码.有什么想法吗?

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?

#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;
}

推荐答案

您的问题出在以下语句上:
Your problem is with the statement:
static LPTSTR textmessage = "";


LPTSTR是一个指针,您错误地分配了一个空字符串,并且没有为其分配任何内存.使用 WM_GETEXT [


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.

static char textmessage[256];


另请注意,SendMessage将返回放置在textmessage缓冲区中的字符数.您可以使用此号码代替使用strlen调用.


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.


我看不出这段代码是如何工作的.没有GetWindowText调用.对于直接发送WM_GETTEXT,请查看您在做什么:将字符串textmessage初始化为空字符串,然后尝试输入文本.看来您想改用buffer.

可能存在一个更深层次的问题,但我不知道您是否面对它,因为您的样本与您的问题并不完全匹配.如果通过任何更改调用了GetWindowText和更高版本的GetWindowText,则立即或在同一事件处理程序中执行此操作均无效.如果这样做,则不会给您的主应用程序消息循环提供处理消息的机会,因此在调用GetWindowText时并不会真正设置文本.如果您确实要这样做(我对此表示怀疑),则需要在某些高级库中调用某个名为"DoEvents"的函数,而C ++中不存在此函数.

请参阅以下讨论:
http://forums.devx.com/archive/index.php/t- 91311.html [^ ].

—SA
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


这篇关于GetWindowText()从我的编辑控件中检索不到任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆