WINapi. Can't erase background of window with ellipse

Viewed 374

I'm just trying to draw an ellipse:

case WM_PAINT:
        hdc = BeginPaint(parentWindow, &ps);
        Ellipse(hdc, x, y, width, height);
        EndPaint(parentWindow, &ps);

, and then erase it with drawing a new ellipse with some new parameters every second using timer:

case WM_CREATE:
        SetTimer(hWnd, 1, 1000, NULL);
        break;
case WM_TIMER:
        x += 5;
        InvalidateRect(hWnd, NULL, TRUE);
        break;

But ellipses are not erased and layered:

Ellipses

However, i tried to trace WM_ERASEBKGND and it really is sent every InvalidateRect.

Full code:

#include <Windows.h>
#include <windowsx.h>
#include <tchar.h>
#include <iostream>


TCHAR szWindowClass[] = TEXT("CreateThreadWindow");
TCHAR szAppName[] = TEXT("CreateThreadExample");

BOOL InitWindow(HINSTANCE, int);
ATOM MyRegisterClass(HINSTANCE);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

HWND parentWindow;
MSG msg;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MyRegisterClass(hInstance);
    if (!InitWindow(hInstance, nCmdShow))
        return FALSE;
    BOOL bRet;
    while ((bRet = GetMessage(&msg, (HWND)NULL, 0, 0)) != 0)
    {
        if (bRet == -1)
            return FALSE;
        else
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return (int)msg.wParam;
}

ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASS wndClass;
    memset(&wndClass, 0, sizeof(wndClass));
    wndClass.lpfnWndProc = WndProc;
    wndClass.hInstance = hInstance;
    wndClass.lpszMenuName = NULL;
    wndClass.lpszClassName = szWindowClass;
    return RegisterClass(&wndClass);
}

BOOL InitWindow(HINSTANCE hInstance, int nCmdShow)
{
    parentWindow = CreateWindow(szWindowClass, szAppName, WS_OVERLAPPEDWINDOW,
        300, 0, 600, 600, NULL, NULL, hInstance, NULL);
    ShowWindow(parentWindow, nCmdShow);
    return TRUE;
}



LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    static int x = 0, y = 0, width = 200, height = 100;
    switch (message) {
    case WM_ERASEBKGND:
        _RPT1(0, "%s\n", "erase");
        break;
    case WM_PAINT:
        hdc = BeginPaint(hWnd, &ps);
        Ellipse(hdc, x, y, width, height);
        EndPaint(hWnd, &ps);
        break;
    case WM_CREATE:
        SetTimer(hWnd, 1, 1000, NULL);
        break;
    case WM_TIMER:
        x += 5;
        InvalidateRect(hWnd, NULL, TRUE);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    default:
        return DefWindowProc(hWnd, message, wparam, lparam);
    }
}
1 Answers
Related