C++ SetWindowText not working

C++ SetWindowText not working

本文关键字:working not SetWindowText C++      更新时间:2023-10-16

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

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

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

UPDATE:您更新的代码与原始代码略有不同,但问题仍然存在。edit_1在WndProc中声明,但仅在WM_CREATE回调期间初始化。当WM_COMMAND回调发生时(对WndProc的单独调用),它是未初始化的,所以SetWindowText调用会进入一个随机窗口,或者更可能是不进入任何位置。

我建议您使用一个框架,这样可以很容易地将变量与窗口关联起来。显而易见的选择是MFC或ATL。您可能还需要一些C++方面的辅导,以便了解范围、局部变量与全局变量等。

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