Summary of the use of redraw functions in MFC for "Turn" VC (collation)

Source: Internet
Author: User

Original URL: http://www.cnblogs.com/x8023z/archive/2008/12/09/mfc33.html

The redraw function is often called when the window is refreshed
MFC provides three functions for window redrawing
InvalidateRect (&rect)
Invalidate ()
UpdateWindow ()
When a window needs to be updated or redrawn, the general system emits two messages WM_PAINT (notifies the client area of changes) and Wm_ncpaint (notifies the non-client area of changes)
The--wm_nvpaint system will take care of itself.
The function that corresponds to the--wm_paint message is OnPaint (), which is the system default function that accepts WM_PAINT messages, but we usually do it in the OnDraw function when redrawing in the program, because the Onpain function is called in the OnDraw function.
CView Default standard Redraw function
void Cview::onpaint ()
{
CPAINTDC DC (this);
ONPREPARDC (&DC);
OnDraw (&DC); Called the OnDraw
}

mentioned above
InvalidateRect (&rect)
Invalidate () Two functions are similar in function, but Invalidate is to invalidate the entire window, forming an invalid rectangle, and InvalidateRect (&rect) invalidates the specified area. Invalidate () Declaration is invalid, waits for WM_PAINT message to redraw, the system automatically sends when no other message is in the queue
UpdateWindow () sends the WM_PAINT immediately, but before it is sent, it calls GetUpdateRect (hwnd,null,true) to see if there is no drawing area, and if not, the message is not sent.RedrawWindow () RedrawWindow () is a dual feature with Invalidate () and UpdateWindow (). The State of the declaration window is not valid and immediately updates the window, standingthat is, call WM_PAINT message processing. Why is the system not sending WM_PAINT messages when calling invalidate? Why do I have to wait until the app message queue is empty to send WM_PAINT messages? This is because the departmentThe drawing operation in the window is treated as a low-priority operation, so as to be pushed backwards as much as possible. But it also helps to improve the efficiency of drawing: two WM_PAINT messagesareas that are invalidated by InvalidateRect and Invaliatergn are added up and updated in one WM_PAINT message, not only to avoidupdates the same region multiple times, and also optimizes the app's update operations. such as this through InvalidateRect and invalidatergn to make the window area invalid, dependent on the system. The mechanism for sending WM_PAINT messages at the right time is actually an asynchronous way of working, that is, there is a delay between the invalid window area and the sending WM_PAINT messagelate; Sometimes this delay is not what we want, and we can of course use SendMessage to send a WM_PAINT message after invalidating the window area to forceredraw now, but instead of using Windows GDI to provide us with more convenient and powerful functions: UpdateWindow and RedrawWindow. UpdateWindow will check the window'sUpdate Region, when it is not empty, sends the WM_PAINT message; RedrawWindow gives us more control: whether to redraw non-client areas and backgrounds, whether to always sendWM_PAINT the message regardless of whether the update region is empty or not.
BeginPaint and WM_PAINT messages are closely related. What if you try not to write beginpaint in the WM_PAINT processing function? The program will reach as much as it enters a dead loop.Amazing CPU usage, you will find that the program is always processing one after another WM_PAINT message. This is because, under normal circumstances, when an app receives a WM_PAINT message, the windowThe update region of the port is non-null (if it is empty, you do not need to send the WM_PAINT message), and one of the BeginPaint functions is to set the update to empty, so that if you do not call BeginPaint, the window's update region is not empty, as mentioned earlier, the system will always send WM_PAINT messages.
BeginPaint and WM_ERASEBKGND messages are also related. When the window's update region is marked as needing to erase the background, BeginPaint sends WM_ERASEBKGNDto redraw the background, and in the return message there is a flag indicating whether the background of the window has been redrawn. When we use InvalidateRect and invalidatergn to get the designated areawhen the domain is added to the update region, you can set whether the zone needs to be erased from the background so that the next beginpaint knows if a WM_ERASEBKGND message needs to be sentup. It is also important to note that BeginPaint can only be used in WM_PAINT processing functions. The following are the functions of the Invalidate function: void Invalidate (BOOL bErase = TRUE);
The function is to invalidate the entire window client area. The invalid client area of a window means that a redraw is required, for example, if a window obscured by another window becomes a foreground window, the part that was obscured is invalid and needs to be redrawn. Windows then places the WM_PAINT message in the application's message queue. MFC provides a WM_PAINT message handler function for the window class Onpaint,onpaint is responsible for redrawing the window. There are some exceptions to the view class, and the OnDraw function is called in the OnPaint function of the view class, and the actual redraw work is done by OnDraw. When the parameter berase is true, the background in the redraw area will be erased, otherwise the background will remain unchanged.

The difference with UpdateWindow () is that the function of UpdateWindow () is to make the window redraw immediately. The window does not redraw immediately after calling functions such as invalidate, because the WM_PAINT message has a low priority, and it needs to be processed after other messages in the message queue have been sent out. Calling the UpdateWindow function causes the WM_PAINT to be sent directly to the target window, causing the window to be redrawn immediately.

Hope to be useful to those in need .... For reference only!! There is something wrong, please give a bit in the comments to point out! Thank you....

Summary of the use of redraw functions in MFC for "Turn" VC (collation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.