最小化/最大化后,MFC窗口图形消失了 [英] MFC window graphic is gone after minimizing/maximizing
问题描述
我正在尝试使用MFC创建Windows窗口和其中的图形,但是在最小化或最大化窗口后,图形消失了.有没有一种方法可以使图形在最小化或最大化后仍然可以显示?
I am trying to create a Windows window and a graphic inside it using MFC, but after minimizing or maximizing the window the graphic is gone. Is there a way so that the graphic will still be up after minimizing or maximizing?
这是我写的代码.
#include"Header.h"
#include <iostream>
using namespace std;
int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE,
LPSTR lpstr, int ncmdshow)
{
WNDCLASSEX wnd;
wnd.cbClsExtra = 0;
wnd.cbWndExtra = 0;
wnd.cbSize = sizeof(WNDCLASSEX);
wnd.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wnd.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wnd.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wnd.hInstance = hinstance;
wnd.lpfnWndProc = Mywndproc;
wnd.lpszClassName = TEXT("mywn");
wnd.lpszMenuName = NULL;
wnd.style = CS_HREDRAW | CS_VREDRAW;
RegisterClassEx(&wnd);
HWND hwnd = CreateWindow(TEXT("mywn"),
TEXT("mywn2"),
WS_OVERLAPPEDWINDOW,
50, 50, 300, 500,
NULL,
NULL,
hinstance,
0);
ShowWindow(hwnd, SW_SHOWNORMAL);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LALLULT CALLBACK Mywndproc(HWND hwnd,UINT imessage,WPARAM wparam,LPARAM lparam) {
LRESULT CALLBACK Mywndproc(HWND hwnd, UINT imessage, WPARAM wparam, LPARAM lparam) {
HDC hdc;
int x = LOWORD(lparam);
int y = HIWORD(lparam);
int cmd = LOWORD(wparam);
HBRUSH hbrush = CreateSolidBrush(RGB(125, 60, 250));
HBRUSH hbrush1 = CreateSolidBrush(RGB(255, 0, 0));
HPEN hpen = CreatePen(PS_DASH, 30, RGB(0, 50, 256));
PAINTSTRUCT ps;
switch (imessage)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
hdc = GetDC(hwnd);
TextOut(hdc, x, y, TEXT("Name"), strlen("Name"));
DeleteDC(hdc);
break;
case WM_MOUSEMOVE:
hdc = GetDC(hwnd);
if (cmd == MK_LBUTTON) {
SelectObject(hdc, hbrush1);
Ellipse(hdc, x, y, x + 100, y + 120);
}
else if (cmd == MK_RBUTTON) {
TextOut(hdc, x, y, TEXT("Erevan"), strlen("Erevan"));
}
DeleteDC(hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
TextOut(hdc, 100, 100, TEXT("Text"), strlen("Text"));
EndPaint(hwnd, &ps);
break;
case WM_RBUTTONDOWN:
hdc = GetDC(hwnd);
SelectObject(hdc, hbrush1);
Ellipse(hdc, x, y, x + 100, y + 120);
EndPaint(hwnd, &ps);
DeleteDC(hdc);
break;
default:
return DefWindowProc(hwnd, imessage, wparam, lparam);
}
return 0;
}
推荐答案
在WM_PAINT
消息的处理之外画图绝对是可以的:
Drawing outside of processing of the WM_PAINT
message is absolutely OK:
您应该调用ReleaseDC()
而不是DeleteDC()
来返回通过调用GetDC()
获得的HDC
.
You should call ReleaseDC()
rather than DeleteDC()
to return a HDC
you got by calling GetDC()
.
此外,在释放或销毁HDC
之前,必须先选择您选择的资源.
Also, resources you select into a HDC
must be selected out before releasing or destroying it.
但是,由于窗口的移动,调整大小,取消隐藏等原因,无效"部分或全部客户区的结果确实可能会收到WM_PAINT
消息.因此,为响应WM_PAINT
消息,您应该执行完整的重新绘制,即要显示的所有项目.
However, a WM_PAINT
message may indeed be received as a result of "invalidating" a part or all of the client area, due to moving, resizing, unhiding etc the window. So in response to a WM_PAINT
message you should perform a full repaint, ie all the items you want to be displayed.
这篇关于最小化/最大化后,MFC窗口图形消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!