The code you presented here is wrong. First off, you need to read a little more of the documentation. Here is a useful link: Painting and Drawing. Basically there are two ways to update a window:
- In response to the
WM_PAINT
message. Use theBeginPaint
andEndPaint
functions to paint the client area properly. This message is sent by the system when a part of the client area is “invalidated” (as a result of resizing, restoring from minimized state, moving a window previously obscured, or programmatically invalidating it.)WM_PAINT
is a low-priority message, received just before the message-queue gets empty. - Specifically drawing a part or the whole client area without having an invalidated region on it. Use
GetDC
andReleaseDC
for this. Useful if you want to make changes immediately visible, when the application (CPU) is busy.
Writing some code to process the WM_PAINT
message is normally almost mandatory, while specifically drawing as well is optional, depending on the requirements of your application.
Never send or post a WM_PAINT
message yourself, instead invalidate a part or the client area – the application will receive a WM_PAINT
message before becoming idle. If you want the painting to occur immediately call UpdateWindow
– this bypasses the message queue.
solved Why should I call `GetDC` and `ReleaseDC` again and again?