0
votes

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?

This is the code i wrote.

#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;
}

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;

}

2

2 Answers

1
votes

Drawing outside of processing of the WM_PAINT message is absolutely OK:

Painting and Drawing

You should call ReleaseDC() rather than DeleteDC() to return a HDC you got by calling GetDC().

Also, resources you select into a HDC must be selected out before releasing or destroying it.

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.

0
votes

Do all your drawing in WM_PAINT, not WM_LBUTTONDOWN, WM_MOUSEMOVE, and WM_RBUTTONDOWN. Save what you want to draw in WM_LBUTTONDOWN, WM_MOUSEMOVE, and WM_RBUTTONDOWN and call Invalidate() to send a WM_PAINT message to draw them. Your drawing will be up all the time.