C ++ SetWindowText无法正常工作

时间:2012-11-28 20:39:37

标签: c++ winapi

我正在尝试更改文本框中的文本,但代码似乎不起作用。 我正在使用SetWindowText(hwnd,lpcstr);并且不知道问题可能是什么

尝试了我想到的一切

#include <iostream>
#include <string>
#include <fstream>
#include <windows.h>
#include <tchar.h>

using namespace std;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
enum { ID_LABEL = 1,ID_LONG_TEXT,ID_EDIT,ID_EDIT_1,ID_BUTTON };

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
// Creo la finestra

static TCHAR szWindowClass[] = _T("Draw");
static TCHAR szTitle[] = _T("Draw");

WNDCLASSEX wcex;

wcex.cbSize      = sizeof(WNDCLASSEX);
wcex.style       = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra  = 0;
wcex.cbWndExtra  = 0;
wcex.hInstance   = hInstance;
wcex.hIcon       = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor     = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName   = NULL;
wcex.lpszClassName  = szWindowClass;
wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

if(!RegisterClassEx(&wcex))
{
    MessageBox(NULL,
               _T("Call to RegisterClassEx failed!"),
               _T("Win32 Guided Tour"),
               NULL);

    return 1;
}

HWND hWnd = CreateWindow(
    szWindowClass,
    szTitle,
    WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
    CW_USEDEFAULT, CW_USEDEFAULT,
    450, 600,
    NULL,
    NULL,
    hInstance,
    NULL);

if(!hWnd)
{
    MessageBox(NULL,
               _T("Call to CreateWindow failed!"),
               _T("Win32 Guided Tour"),
               NULL);

    return 1;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

MSG msg;

while(GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
TCHAR primo[] = _T("Inserisci la lunghezza della parola da indovinare:");
TCHAR secondo[] = _T("Inserisci le lettere (una dopo l'altra senza ne spazi ne invii):");
HDC hdc;
//HFONT hFont;
HWND button;
HWND edit;
HWND edit_1;
HWND long_edit;
HINSTANCE g_hInst;

switch(uMsg)
{
    case WM_CREATE:
    {
        edit = CreateWindow("Edit", "1h2f3d4",
                             WS_BORDER |WS_CHILD | WS_VISIBLE,
                             5, 30, 23, 20, hWnd, (HMENU) ID_EDIT, NULL, NULL);

        edit_1 = CreateWindow("Edit", "sdgf",
                              WS_BORDER | WS_CHILD | WS_VISIBLE,
                              5, 85, 250, 20, hWnd, (HMENU) ID_EDIT_1, NULL, NULL);

        // Creo il pulsante

        button = CreateWindow("Button","Calcola",
                              BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
                              260, 85, 150, 20,hWnd,(HMENU) ID_BUTTON,NULL,NULL);   

        long_edit = CreateWindow("Edit", "kjvh",
                                 WS_BORDER | WS_CHILD | WS_VISIBLE,
                                 5, 125, 250, 300, hWnd, (HMENU) ID_LONG_TEXT, NULL, NULL);
    }

    case WM_PAINT:
    {
        PAINTSTRUCT ps;

        hdc = BeginPaint(hWnd, &ps);

        SetTextColor(hdc, RGB(0,0,0));
        SetBkColor(hdc, RGB(255,255,255));

        TextOut(hdc, 5, 5, primo, sizeof(primo));
        TextOut(hdc, 5, 60, secondo, sizeof(secondo));

        EndPaint(hWnd, &ps);

        break;
    }
    case WM_COMMAND:
    {
        if(edit_1 == NULL)
        {
            MessageBox(hWnd, "Errore", "Errore", uMsg);
        }

        if(LOWORD(wParam) == ID_BUTTON)
        {
            MessageBox(hWnd, "sgf", "ghgfrore", uMsg);
            SetWindowText(edit_1, "lool");
        }
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
        break;
}

return 0;
}

我发布了我的所有代码,你可以亲自看看,但我没有发现任何问题

1 个答案:

答案 0 :(得分:4)

看起来您在多个范围内声明了edit_1。您在创建按钮时声明了一个本地按钮,但请注意它位于与引用不同的大括号块中。因此,您可能有一个名为edit_1的单独变量未初始化。

更新:您更新的代码与原始代码略有不同,但问题仍然存在。 edit_1在WndProc中声明,但它仅在WM_CREATE回调期间初始化。当WM_COMMAND回调发生时(单独调用WndProc),它会被取消初始化,因此您的SetWindowText电话会转到随机窗口,或者更有可能无处可见。

我建议你使用一个框架,这样可以很容易地保持与Window相关的变量。明显的选择是MFC或ATL。您可能还需要对C ++进行一些辅导,以便了解范围,本地变量和全局变量等。

如果不使用框架,你可以通过使变量全局化来使这些代码工作,但是这不会扩展到比非常简单的程序更大的任何东西。