1. The OnPaint () function is the response function of the window redraw message wm_paint, which generates WM_ERASEBKGND messages and WM_PAINT messages when the window is redrawn, and WM_ERASEBKGND is generated before WM_PAINT, so when the window is redrawn, The OnEraseBkgnd () erase window is called before the OnPaint drawing window is called. If you paint the window in OnPaint (), you'll notice that the window will flash when the window is redrawn, because the OnEraseBkgnd () function uses the default brush (usually gray-white) to erase the window. There are three ways to solve this problem:
① the work of drawing windows in OnEraseBkgnd (), commenting out the code for erasing windows in OnEraseBkgnd ().
② the work of drawing windows in OnPaint, commenting out the code for erasing windows in OnEraseBkgnd ().
③ the work of drawing windows in OnPaint, setting the background brush blank when creating a window.
Since the WM_PAINT message needs to be processed after other messages in the message queue are sent, and the work of the drawing window is implemented in OnEraseBkgnd (), any small changes to the window element will call OnEraseBkgnd (), OnPaint () OnEraseBkgnd () may have been called several times before the call, so the best way to do this is to do only simple, time-consuming drawing work in OnEraseBkgnd (), while complex drawing work takes place in OnPaint ().
When the window is redrawn, it does not necessarily produce a WM_ERASEBKGND message, and the Invalidate () and InvalidateRect () redraw the window with the parameter berase to specify whether to produce a WM_ERASEBKGND message to erase the background.
2. cwnd::invalidate () invalidates the entire client area, and the client area is not valid sends a WM_PAINT message to redraw the entire customer area. The window may not be redrawn immediately after calling invalidate because it waits until other messages in the message queue are sent to be processed. Function Prototypes:
void Invalidate (BOOL bErase = TRUE);
The parameter determines whether to send the WM_ERASEBKGND message before the WM_PAINT message to erase the window background, Berase is true, the background in the redraw area will be erased, otherwise the background will remain intact.
Cwnd::invalidaterect () with The invalidate function is the same, but you can specify the area to redraw, the function prototype:
BOOL InvalidateRect (const RECT *lprect, bool bErase = TRUE);
Cwnd::updatewindow () redraws the client area by sending a WM_PAINT message and does not redraw if there is no area to draw. If there is an area that can be drawn , UpdateWindow () immediately redraws the window, and the WM_PAINT message it sends does not go into the message queue but is directly called to redraw the window procedure. So it is generally called invalidate () and then called UpdateWindow () to make the window redraw immediately .
void UpdateWindow ();
Cwnd::redrawwindow () is also used to redraw the window, which is more flexible, and its flags parameter can be a combination of the following values: Rdw_invalidate invalidates the area, Rdw_updatenow immediately redraws the window, Rdw_erase the background in the redraw area will be erased. So RedrawWindow () can achieve
The sum of the UpdateWindow functions. Function Prototypes:
BOOL RedrawWindow ( lpcrect lprectupdate = null, crgn* prgnupdate = null,
WM_PAINT and WM_ERASEBKGND messages