user186496
user186496

Reputation:

Win32 WM_PAINT and a child window

How to draw inside a child window?

I thought I should create the main window CreateWindow(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN) with some WndProc (without WM_PAINT). On its WM_CREATE I create another window CreateWindow(WS_CHILD | WS_CLIPCHILDREN) with another WndProc2 which reacts to WM_PAINT. However, it seems that the another handler enters an infinite loop. What am I doind wrong?

Please, don't you have an advice or examle?

PS: WS_CLIPCHILDREN doesn't seem to effect this, and both WndProc default to DefWindowProc

The code:


LRESULT CALLBACK Proc2(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   switch(msg)
    {
        case WM_CREATE:
            printf("-------\n");
            return 0;
        case WM_PAINT:
            printf("-");
            return 0;

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

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
...
case WM_CREATE:
    CreateWindowClass(hInstance, Proc2, "Window2");
    w2 = CreateWindowEx(WS_EX_STATICEDGE, "Window2", "Win", WS_CHILD | WS_CLIPCHILDREN, 0, 0, 100, 100, hWnd, NULL, hInstance, NULL);


void createWindowClass(HINSTANCE hInstance, WNDPROC WndProc, LPCSTR lpszClassName)
{
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = 0;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName  = "test";
    wc.lpszClassName = lpszClassName;
    RegisterClassEx(&wc);
}

int WINAPI WinMain
...
createWindowClass(hInstance, WndProc, "MainWindow");
w = CreateWindow("MainWindow", "Main", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);


Upvotes: 1

Views: 4007

Answers (1)

jdigital
jdigital

Reputation: 12266

You should be calling BeginPaint and EndPaint in response to the WM_PAINT message to validate the window. Otherwise, the system thinks that your window has not been painted and so it will send the paint message again (and again). See the Microsoft documentation.

Upvotes: 5

Related Questions