Here, invalidate () can replace redrawwindow ().

Source: Internet
Author: User

In the second part of the wtl clock program, when the timer throws the wm_timer message, the processor ontimer does the following work:

Lresult ontimer (uint_ptr nidevent) <br/>{< br/> if (1! = Nidevent) <br/> setmsghandled (false); <br/> else <br/>{< br/> getlocaltime (& m_stlasttime ); // Save the current time <br/> redrawwindow (); // redraw the window <br/>}< br/> return 0; <br/>}

The onpaint processor used to update the time is triggered by the wm_paint message. Obviously, wm_paint is thrown by redrawwindow.

If postmessage (wm_paint) is directly used to replace redrwawindow (), the system compiles and runs the program and finds that the time displayed in the window is not updated every 1sec as expected. Why? The wm_paint message is thrown manually. It should be triggered by the onpaint function to process the message.

F12: view the redrawwindow () Code as follows:

Bool redrawwindow (<br/> _ in_opt _ lpcrect lprectupdate = NULL, <br/> _ in_opt _ hrgn hrgnupdate = NULL, <br/> _ in _ uint flags = rdw_invalidate | rdw_updatenow | rdw_erase) Throw () <br/>{< br/> atlassert (: iswindow (m_hwnd )); <br/> return: redrawwindow (m_hwnd, lprectupdate, hrgnupdate, flags); <br/>}

Obviously, it is a simple encapsulation of the Platform SDK function redrawwindow. By viewing msdn, it is easy to understand that the redrawwindow function has done two things:

  1. Perform "invalidate" on the window, that is, to declare that the region to be updated is "expired"
  2. Throw messages such as wm_ncpaint, wm_erasebkgnd, and wm_paint.

Well, there are enough redrawwindows to understand. We found that in the wtl clock program, we only need the wm_paint message, but the experiment proves that postmessage (wm_paint) does not work, and it cannot replace the redrawwindow function. The only thing we miss is the window "invalidate" (expiration Declaration ). Search for invalidate in msdn and get a cwindowimpl: invalidate () member function. The wtlclockview class inherits from cwindowimpl, so you can call invalidate () directly within it.

For more information, see the description of invalidate (): "invalidates the entire client area. passes null for the rect parameter to the invalidaterect Win32 function ", which means that the function will declare that the entire customer zone has expired. In this example, view is the customer zone. In addition, it is an encapsulation of Win32 functions. Check the description of invalidaterect and link it to invalidating.
The first sentence of the client area is: "The system is not the only source ofwm_paint messages. theinvalidaterect
Orinvalidatergn function can indirectly generate wm_paint messages for your windows. "It tells us that the system is not the only source of the wm_paint message. The invalidaterect or invalidatergn function can also generate a wm_paint message for your window. It seems that invalidate () has completely met the requirements of this program: declares that the client area (view area in this example) is "expired" and throws the wm_paint message we need, this will drive onpaint () to update the time. Therefore, the ontimer () function can be modified:

Ontimer (uint_ptr nidevent) <br/>{< br/> if (1! = Nidevent) <br/> setmsghandled (false); <br/> else <br/>{< br/> getlocaltime (& m_stlasttime); <br/> invalidate (); <br/> // postmessage (wm_ncpaint); <br/> // postmessage (wm_erasebkgnd); <br/> // postmessage (wm_paint ); <br/> // redrawwindow (); <br/>}< br/> return 0; <br/>}

You do not need to write postmessage (wm_paint. Compile and run, and the result is as needed.

 

P.s:

In Windows, users are prohibited from throwing wm_paint messages. wm_paint messages should be triggered by functions such as invalidaterect. As long as an invalid region exists in the customer zone, the Message Queue contains a wm_paint message, but the priority of the message is very low. It is captured by the getmessage function only when no other message exists. Multiple wm_paint messages are merged into one by combining invalid areas. The API function redrawwindow is much more powerful than the invalidate function. The two functions are very different, but in this example they are used to make the entire customer area invalid for re-painting. The difference is that the redrawwindow in this example is drawn immediately, and it is uncertain when the wm_paint caused by invalidate is processed. (2013-06-22)

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.