gpt4 book ai didi

c++ - WinAPI - C++ - 如何完全删除/清除窗口中的所有内容?

转载 作者:行者123 更新时间:2023-11-30 03:52:17 26 4
gpt4 key购买 nike

我正在为我的应用程序使用 C++ 中的 WinAPI。我通过处理 WM_PAINT 消息绘制了一些文本并在窗口中绘制了位图。我还通过处理 WM_CREATE 消息,通过 CreateWindow 函数在窗口中放置了一些按钮控件。我花了几个小时研究这个但我没有运气 - 我怎样才能完全清除窗口及其所有内容(我放入其中的内容),所以我只剩下一个具有相同背景标题的空窗口等等,这样我就可以在上面放更多的按钮和东西。

这是我的代码

#include <windows.h>
#include <string>
#include "mainheader.h"

const char g_szClassName[] = "myWindowClass";
HBITMAP bitmap;

//Window Procedure #Brain of the Window
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_CREATE:{
CreateWindow(TEXT("button"), TEXT("Play"),
WS_VISIBLE | WS_CHILD,
100, 227, 80, 25,
hwnd, (HMENU) 1, NULL, NULL);

CreateWindow(TEXT("button"), TEXT("Stats"),
WS_VISIBLE | WS_CHILD,
200, 227, 80, 25,
hwnd, (HMENU) 2, NULL, NULL);

CreateWindow(TEXT("button"), TEXT("Quit"),
WS_VISIBLE | WS_CHILD,
300, 227, 80, 25,
hwnd, (HMENU) 3, NULL, NULL);

bitmap = (HBITMAP) LoadImage(NULL, TEXT("menuIcon.bmp"), IMAGE_BITMAP, 130, 130, LR_LOADFROMFILE);
break;
}

case WM_COMMAND:{
switch(LOWORD(wParam)){
case 1:{
break;
}

case 2:{
break;
}

case 3:{
PostQuitMessage(0);
}
}

break;
}

case WM_PAINT:{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
HDC hMemDC = CreateCompatibleDC(hdc);

::SelectObject(hMemDC, bitmap);
BitBlt(hdc, 170, 65, 200, 400, hMemDC, 0, 0, SRCCOPY);
::DeleteDC(hMemDC);

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

std::string text = "Welcome to Tic Tac Toe v2.0!";
std::string text2 = "Made By: Jaden Peterson (2015)";
std::string text3 = "Please choose an option below:";

TextOut(hdc, 150, 20, text.c_str(), text.length());
TextOut(hdc, 141, 45, text2.c_str(), text2.length());
TextOut(hdc, 145, 200, text3.c_str(), text3.length());
EndPaint(hwnd, &ps);
break;
}

case WM_CLOSE:{
DestroyWindow(hwnd);
break;
}

case WM_DESTROY:{
PostQuitMessage(0);
break;
}

default:{
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}

return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = CreateSolidBrush(RGB(0, 0, 0));
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));

if(!RegisterClassEx(&wc)){
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

//Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Tic Tac Toe v2.0",
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 300,
NULL, NULL, hInstance, NULL);

if(hwnd == NULL){
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

//Message Loop #Heart of the Window
while(GetMessage(&Msg, NULL, 0, 0) > 0){
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

return Msg.wParam;
}

最佳答案

删除所有子窗口很简单。执行循环并使用 EnumChildWindows 枚举所有子窗口.对于每个返回的句柄,调用 DestroyWindow .

首先声明回调函数:

BOOL CALLBACK DestoryChildCallback(
HWND hwnd,
LPARAM lParam
)
{
if (hwnd != NULL) {
DestroyWindow(hwnd)
}

return TRUE;
}

来自您的代码:

EnumChildWindows(hwnd /* parent hwnd*/, DestoryChildCallback, NULL);

关于c++ - WinAPI - C++ - 如何完全删除/清除窗口中的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com