0

我正在尝试更改文本框中的文本,但代码似乎不起作用。我正在使用 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;
}

我已经发布了我所有的代码,您可以自己查看,但我没有发现任何问题

4

1 回答 1

4

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

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

我建议您使用一个框架,该框架可以轻松地将变量与窗口相关联。显而易见的选择是 MFC 或 ATL。为了理解作用域、局部变量和全局变量等,您可能还需要一些 C++ 辅导。

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

于 2012-11-28T22:00:08.587 回答