If some basic knowledge is not identified in time, it is easy to have a big problem.
Wm_paintYesWindowsAn important message and Application in the window systemProgramYou can plot the message in a window by processing the message.
1. When will the system send Wm_paint Message?
The system sends messages at multiple times. Wm_paint Message: When a window is created for the first time, when the window size is changed, when the window is removed from the back of another window, when the window is maximized or minimized, and so on, these actions are In system management, the application only passively receives the message and draws the message in the message processing function. In most cases, the application also needs to be able to actively trigger the Drawing operation in the window, for example, when the data displayed in the window changes Invalidaterect And Invalidatergn Function. Invalidaterect And Invalidatergn Add the specified area to Update Region When the application's message queue has no other messages Update Region When it is not null, the system will automatically generate Wm_paint Message.
Why is the system not called? Invalidate Send at a time Wm_paint What about messages? Why do I have to wait until the application message queue is empty? Wm_paint What about messages? This is because the system regards the painting operation in the window as a low-priority operation May be pushed back. However, this will also help improve the rendering efficiency: Two Wm_paint Messages pass through Invalidaterect And Invaliatergn The invalid region will be accumulated, and then Wm_paint Messages are obtained once. Update not only avoids repeated updates to the same region, but also optimizes the update operation of the application. Such Invalidaterect And Invalidatergn To make the window area invalid, depending on the system to send at the right time Wm_paint Message Machine It is actually an asynchronous way of working, that is, in the Invalid Window area and sending Wm_paint There is a delay between messages. Sometimes this delay is not what we want, and we can use it in the Invalid Window area. Sendmessage Send one Wm_paint Message to force re-draw immediately, but it is better to use Windows GDI Provides us with more convenient and powerful functions: Updatewindow And Redrawwindow . Updatewindow Will check the window Update Region , Sent only when it is not empty Wm_paint Message; Redrawwindow It gives us more control: whether to redraw non-customer areas and backgrounds, and whether to always send Wm_paint Message regardless Update Region Whether it is empty or not.
2. beginpaint
Beginpaint And Wm_paint Messages are closely related. Try Wm_paint Do not write in the processing function Beginpaint What will happen? The program will be as amazing as it enters an endless loop CPU Usage, you will find that the program is always Processing One Wm_paint Message. This is because, in general, when the application receives Wm_paint Message Update Region Both are non-empty (If empty, you do not need to send Wm_paint Message ), Beginpaint Is Update Region Leave it blank so that if you do not call Beginpaint , Window Update Region It will not be blank. As mentioned above, the system will keep sending Wm_paint Message.
Beginpaint And Wm_erasebkgnd Messages are also related. When Update Region Marked as the background to be erased, Beginpaint Will send Wm_erasebkgnd Message to re-draw the background, and a flag in the returned information indicates whether the background of the window has been re-painted. When we use Invalidaterect And Invalidatergn To add the Specified Region Update Region You can set whether the background of the region needs to be erased, so that the next Beginpaint You will know whether to send Wm_erasebkgnd Message.
Note that,BeginpaintOnlyWm_paintUsed in processing functions.