How to draw OpenGL content while resizing win32 window
Asked Answered
L

1

8

While resizing win32 window, with OpenGL context, it just shows black on the newly exposed area. I do get WM_PAINT message while resizing, and I do try to render new content, but it seems as if SwapBuffers does nothing, while resizing.

How should window resizes be handled correctly, so that there is no "broken" content while resizing?

Lyell answered 6/7, 2011 at 7:37 Comment(2)
While resizing, or after resizing?Cembalo
related: #3267743Coastal
C
9

This usually happens if you have a background brush configured for your window's class (see the WNDCLASS or WNDCLASSEX structure). If there's a brush, the system will clear the window right after each redraw step, then send the WM_PAINT. In case of V-Synced SwapBuffers your picture may have been overdrawn by the next resizing step before the buffer swap happened, or just right after it, but before that part of the screen was sent to the display device.

Either way, the solution is to set the background brush of the window to NULL. Also tinkering with the WM_ERASEBKGND message handling may give results.

EDIT due to comment

If the content of the last frame stays visible, you probably just don't react to resizing with a redraw. The easiest solution to this is calling the drawing function from the WM_SIZING (or the WM_SIZE, just try both) message handler.

Cafard answered 6/7, 2011 at 7:51 Comment(7)
Thank you for your answer. I do not have background brush set, and I am trying to handle WM_ERASEBKGND. The thing is, the window is not cleared, but the last drawn frame is left to upper-left corner.Lyell
I've tried rendering during WM_SIZE as well, that again does not seem to have an effect. It shouldn't matter if I draw in WM_SIZE or in WM_PAINT as I seem to get both events, WM_SIZE and WM_PAINT, while resizing.Lyell
Which Windows version do you use? If Vista or 7 try what happens if you disable the Aero window compositing system.Cafard
This was a major PEBCAK - I had an unrelated bug. Certain pointer wasn't set, and deferecing it (had value 0xcccccc ..) didn't crash. It exhibited a curious problem, in that it resulted a foobar HDC for SwapBuffers, and the execution of the wndproc exited from that point. So printf("foo\n"); SwapBuffers(foobar); printf("bar\n"); didn't print bar. I guess the magic messageloop used within the window resize catches some type of exceptions? Anyway, marking as answered as you did really help me look around.Lyell
Yep, had to make the brush NULL and I had to redraw & swap buffers during the WM_SIZING message. No tinkering with WM_ERASEBKGND though.Tolentino
Note that if you get rid of the background brush, the window background defaults to a really ugly white which can show during window load and resize.Kaylakayle
As was suggested, playing with WM_ERASEBKGND fixed it. Don't remove the brush and just return 0 when this event occurs and it won't break.Kaylakayle

© 2022 - 2024 — McMap. All rights reserved.