) When is ondraw called in VC ++?

Source: Internet
Author: User

Http://blog.csdn.net/lary2360433/article/details/6405737

Q: Why does the image or text I drew in the view disappear when the window changes? Ondraw () and onpaint () both solve the above problem. What is the difference?
Because when your window changes, an invalid area will be generated, and this invalid area needs to be re-painted. Generally, Windows sends two messages, wm_paint (notifying customers that the zone has changed) and wm_ncpaint (notifying non-customers that the zone has changed ). The non-customer area re-painting system is done by ourselves, and the customer area re-painting needs to be done by ourselves. This requires ondraw () or onpaint () to redraw the window.
What is the difference between ondraw () and onpaint?
First, we need to clarify that the cview class is derived from the cwnd class. Onpaint () is a member of the cwnd class and is responsible for responding to the wm_paint message. Ondraw () is a member function of cview and has no message response function. This is why you use VCProgramCodeOnly ondraw does not have onpaint in the View class.
Second, to draw or display a picture on the screen, you must first establish a device environment DC. In fact, DC (device contextDevice context) is a structure that defines a series of graphical objects and their related properties, that is, a set of properties of the device environment. These devices include printing devices. Display devices, output devices, etc,
These graphic objects include: paint brush (used to draw a straight line), brush (used for drawing and filling), bitmap (used for Screen Copy or scrolling), color palette (used to define available color sets ), cropping Area (for cropping and other operations), path (for drawing and drawing operations ).

MFC provides the cpaintdc class and cwindwodc class for real-time response, while cpaintdc supports repainting.
When the view becomes invalid (including size change, movement, overwriting, etc.), Windows sends the wm_paint message to it. The onpaint handler function of this view responds to the message by creating a DC object of the cpaintdc class and calls the ondraw member function of the view. Generally, we do not need to write the override onpaint to process the member function.

 

 
/// Default standard redraw function void cview: onpaint () {cpaintdc DC (this); onprepardc (& DC); ondraw (& DC ); // ondraw called}

 

 


Since ondraw is also called at the end of onpaint, we generally draw it in the ondraw function. The following is a typical program
/// The drawing code in the view first retrieves the pointer to the document, and then calls the drawing through DC.

 
Void cmyview: ondraw (CDC * PDC) {cmydoc * pdoc = getdocument (); cstring S = pdoc-> getdata (); // returns a cstring crect rect; getclientrect (& rect); PDC-> settextalign (ta_baseline | ta_center); PDC-> textout (rect. right/2, rect. bottom/2, S, S. getlength ());}

 

 


Therefore, we generally use onpaint to maintain the customer area of the window (for example, adding a background image to the customer area of our window ), use ondraw to maintain the customer area of the view (for example, drawing in the view with the mouse ). Of course, you may not follow the above rules, as long as you achieve the purpose and there is no problem, how to do it all.
Supplement: we can also use the invalidate (), validatergn (), validaterect () function to force the re-draw window. For details, see the msdn

 

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.