"MFC" window redraw in MFC

Source: Internet
Author: User
Tags message queue

Windows Redraw in MFC

Excerpt from: http://blog.csdn.net/shuilan0066/article/details/5859057

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 customer that the area has changed) and

WM_NCPAINT (Notification of changes to non-client areas)
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 in the form and function of the samebut invalidate is making the entire window invalid, forming an invalid rectangle, while InvalidateRect (&rect) is
invalidates the specified rangeThe Invalidate () declaration is invalid, waits for the WM_PAINT message to redraw, the system automatically sends a UpdateWindow () when there is no other message in the queue and sends WM_PAINT immediately, but before it is sent, it calls GetUpdateRect (HWnd, Null,true) See if there is no drawing area, if not, the message RedrawWindow () RedrawWindow () is a dual feature with Invalidate () and UpdateWindow ().       The State of the declaration window is invalid and immediately updates the window, calling WM_PAINT message processing immediately. 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 system treats the drawing operations in the window as a low-priority operation, so as to be pushed backwards as much as possible. However, this also helps to improve the efficiency of drawing: Two WM_PAINT messages between the InvalidateRect and Invaliatergn to invalidate the area will be added up, and then in a WM_PAINT message is updated once, It not only avoids updating the same area multiple times, but also optimizes the app's update operations. such as this through InvalidateRect and invalidatergn to invalidate the window area, depending on the system at the right time to send the WM_PAINT message mechanism is actually an asynchronous way of working, that is, in the invalid window area and send Wm_ There is a delay between the paint messages, and sometimes this delay is not what we want, and we can certainly use SendMessage to send a WM_PAINT message after the invalid window area to force immediate redrawing, but instead of using Windows GDI provides us with more convenient and powerful functions: UpdateWindow and RedrawWindow. UpdateWindow checks the window's update region to send the WM_PAINT message when it is not empty; RedrawWindow gives us more control: whether to redraw non-client areas and backgrounds, whether to always send WM_PAINT messages regardless of the update Whether the 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 achieve a staggering CPU usage as it enters a dead loop, and you will find that the program is always processing one WM_PAINT message after another. This is because, in general, when an app receives a WM_PAINT message, the window's update region is non-empty (if it's empty, you don't need to send a WM_PAINT message), and BeginPaint's role is to empty the updateregion.        This way, 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 a WM_ERASEBKGND message to redraw the background, and in its return message there is a flag indicating whether the window background has been redrawn.      When we use InvalidateRect and invalidatergn to add the specified area to the update region, you can set whether the area needs to be erased from the background so that the next beginpaint will know if the message needs to be WM_ERASEBKGND. It is also important to note that BeginPaint can only be used in WM_PAINT processing functions.

"MFC" window redraw in MFC

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.