WinAPI - C++ - 如何完全 wipe/clear window 中的所有内容?
WinAPI - C++ - How to completeley wipe/clear everything in window?
我正在为我的应用程序使用 C++ 中的 WinAPI。我通过处理 WM_PAINT 消息绘制了一些文本并在 window 中绘制了位图。我还通过处理 WM_CREATE 消息,通过 CreateWindow 函数在 window 中放置了一些按钮控件。我花了几个小时研究这个,但我没有运气 - 我怎样才能完全清除 window 及其所有内容(我放入其中的内容),所以我只剩下一个空的 window 具有相同的背景标题等,这样我就可以在上面放更多的按钮和东西。
这是我的代码
#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;
}
删除所有子 windows 很简单。做一个循环并使用 EnumChildWindows . For each returned handle, call DestroyWindow.
枚举所有子 windows
首先声明回调函数:
BOOL CALLBACK DestoryChildCallback(
HWND hwnd,
LPARAM lParam
)
{
if (hwnd != NULL) {
DestroyWindow(hwnd)
}
return TRUE;
}
来自您的代码:
EnumChildWindows(hwnd /* parent hwnd*/, DestoryChildCallback, NULL);
我正在为我的应用程序使用 C++ 中的 WinAPI。我通过处理 WM_PAINT 消息绘制了一些文本并在 window 中绘制了位图。我还通过处理 WM_CREATE 消息,通过 CreateWindow 函数在 window 中放置了一些按钮控件。我花了几个小时研究这个,但我没有运气 - 我怎样才能完全清除 window 及其所有内容(我放入其中的内容),所以我只剩下一个空的 window 具有相同的背景标题等,这样我就可以在上面放更多的按钮和东西。
这是我的代码
#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;
}
删除所有子 windows 很简单。做一个循环并使用 EnumChildWindows . For each returned handle, call DestroyWindow.
枚举所有子 windows首先声明回调函数:
BOOL CALLBACK DestoryChildCallback(
HWND hwnd,
LPARAM lParam
)
{
if (hwnd != NULL) {
DestroyWindow(hwnd)
}
return TRUE;
}
来自您的代码:
EnumChildWindows(hwnd /* parent hwnd*/, DestoryChildCallback, NULL);