此代码显示一个带有文本标签的窗口:“请输入一个数字”和一个按钮。
当您单击按钮时,它应该将文本替换为“ TEXT ”。它可以工作,但它会在第一个文本之上写入/打印新文本。所以它重叠。
我希望更改文本字符串而不是覆盖第一个文本字符串,但我不知道如何,因为我是 Windows 应用程序开发的新手。
请帮帮我。
整个来源是:
#include <windows.h>
#include <iostream>
using namespace std;
enum { ID_LABEL = 1,ID_BUTTON0};
static HWND static_label, button0;
HDC hdc;
HBRUSH NewBrush;
HINSTANCE g_hInst;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
LPCTSTR className = TEXT("myClass");
WNDCLASSEX wc;
wc.cbSize = sizeof(wc);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbWndExtra = 0;
wc.cbClsExtra = 0;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("myClass");
wc.hbrBackground = (HBRUSH)(CreateSolidBrush(RGB(48, 38, 88)));
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, TEXT("ERROR! FAILED TO REGISTER CLASS!"), TEXT("FATAL ERROR!"), MB_IConerror | MB_OK);
return 1;
}
HWND hwnd = CreateWindowEx(0, TEXT("myClass"), TEXT("WINDOW TITLE"), WS_OVERLAPPEDWINDOW, 450, 100, 500 + 7, 500 + 33 , NULL, NULL, hInstance, NULL);
if(!hwnd)
{
MessageBox(NULL, TEXT("ERROR! FAILED TO CREATE WINDOW!"), TEXT("FATAL ERROR!"), MB_IConerror | MB_OK);
return true;
}
ShowWindow(hwnd, nShowCmd);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
{
}
case WM_CTLCOLORSTATIC:
{
SetBkMode((HDC) wParam, TRANSPARENT);
return (LONG) GetStockObject(NULL_BRUSH);
}
break;
case WM_CREATE:
{
static_label = CreateWindow(L"Static",L"Please Enter A Number",WS_CHILD | WS_VISIBLE,35,15,175,25,hwnd,0, g_hInst,0);
button0 = CreateWindow(L"Button",L"OK",BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE ,80,220,35,35,hwnd,(HMENU)ID_BUTTON0,g_hInst,0);
}
break;
case WM_COMMAND: //Command from Child windows and menus are under this message
switch(wParam) //the ID is wParam
{
case ID_BUTTON0: //check for our button ID
{
SetWindowText(static_label,L"TEXT");
break;
}
}//switch.
break;
case WM_DESTROY:
PostQuitMessage(0);
break; // pass to DefWindowProc(...) as well
case WM_CLOSE:
DestroyWindow(hwnd);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}