What is the difference between ondraw and onpaint?

Source: Internet
Author: User

What is the difference between ondraw and onpaint? (Transfer)


Onpaint is the message processing function of the wm_paint message. ondraw is called in onpaint. Generally, your own drawing code should be placed in ondraw.

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 does not respond to messages. When the view becomes invalid (including changing the size, moving, overwriting, etc.), Windows sends the wm_paint message. 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. ondraw must also be called at the end of onpaint, so it is generally drawn in the ondraw function.


The wm_paint message is sent when the updatewindow or redrawwindow member function is called.

In onpaint, beginpaint is called to obtain the display device environment of the customer zone, and the GDI function is called to perform the Drawing operation. After the Drawing operation is complete, endpaint is called to release the display device environment. Ondraw is called between beginpaint and endpaint.

1) In the MFC structure, onpaint is a member function of cwnd. ondraw is a member function of cview.
2) onpaint () calls ondraw () and onprint will also call ondraw (). Therefore, ondraw () is a common operation for displaying and printing.

Onpaint is the re-painting message processing function triggered by the wm_paint message. onpaint calls ondraw for plotting. Onpaint first constructs a cpaintdc class instance, and then calls the virtual function onpreparedc as a parameter for some processing before the painting, such as setting the ing mode, and finally calls ondraw. Ondraw and onpreparedc are not message processing functions. Therefore, when ondraw is called not because of the onpaint caused by the re-painting message, for example, when drawing in onlbuttondown and other message processing functions, you must call onpreparedc by yourself.
As for cpaintdc and cclientdc, cpaintdc is a device environment class. In onpaint, it is passed as a parameter to onpreparedc for setting the device environment. Cwindowdc is comparable to cclientdc. One is to describe the customer region and the other is to describe the entire screen.
Ondraw should be used for drawing windows derived from the cview or cview class.

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 only ondraw does not have onpaint in the View class when you use VC program code. In a dialog box-based program, only onpaint is used.
Secondly, we have already said in the beginning of article 3, "Learning MFC every day. To draw or display a graph on the screen, you must first establish a device environment DC. In fact, DC is a data structure that contains a description of the drawing properties of an output device (not just a 17-inch pure screen display, but an output device such as a printer. 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 processing function of this view responds to this message and calls the view's
Ondraw member function. Generally, we do not need to write the override onpaint to process the member function.
/// The default standard redraw function of cview
Void cview: onpaint () // see viewcore. cpp.
{

Cpaintdc DC (this );
Onpreparedc (& DC );
Ondraw (& DC); // ondraw called
}
/// Default standard onprint function of cview
Void cview: onprint (CDC * PDC, cprintinfo *)
{
Assert_valid (PDC );
Ondraw (PDC); // call draw
}

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 ();
Getclientrect (& rect); // returns a cstring crect rect;
PDC-> settextalign (ta_baseline | ta_center );
PDC-> textout (rect. Right/2, rect. Bottom/2, S, S. getlength ());
}
Finally: now we understand the relationship between them. 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 msdn.

You can draw user areas in ondraw. Onpaint only redraws when the window is invalid and does not retain the content drawn by cclientdc.

The two functions are different and related:

1. Difference: ondraw is a pure virtual function, defined as virtual void ondraw (CDC * PDC) = 0; while onpaint is a message response function that responds to wm_panit messages, it is also a window re-painting message.

2. Contact: We generally reload the ondraw pure virtual function instead of directly responding to the wm_panit message when constructing the class, this is because the ondraw function is called in the wm_panit message response function of the cview class. If the wm_paint message is returned in the cmyview class and the ondraw function is not explicitly called, the ondraw function is not called during window re-painting.

Almost all plotting in an application occurs in the ondraw member function of the view. This member function must be rewritten in the View class. (Mouse drawing is a special case, which is discussed in the interpretation of user input through views .)


Ondraw rewrite:
You can call the document member function provided by you to obtain data.
Call the member function of the device context object passed to ondraw to display data.
When the document data is changed in some way, the view must be repainted to reflect the change. The default onupdate implementation invalidates the entire Workspace of the view. When the view becomes invalid, Windows sends the wm_paint message to it. The onpaint processing function of this view responds to the message by creating a device context object of the cpaintdc class and calls the ondraw member function of the view.

When the wm_paint message is not added and the window is re-painted, ondraw will respond to the message... when a wm_paint message is added for processing and the window is re-painted, the wm_paint message is shipped and onpaint is used to respond to the message. in this case, ondraw cannot be called implicitly. must be explicitly called (CDC * PDC = getdc (); ondraw (PDC );)..
Implicit call: When onpaint calls a message, the system automatically calls cview: ondraw (& PDC ).


Imagine that the content displayed in the window is similar to the printed content. Therefore, in general, ondraw is used for painting. When the window foreground needs to be refreshed, the system will call onpaint, while onpaint generally calls ondraw () after initialization of DC ().


Onerasebkgnd () is called by the system when the window background needs to be refreshed. An obvious example is to set the background color of the window (you can put this in onpaint, but it will cause flickering ).
As for how to define the background and prospects, we need to analyze specific issues. In general, it is easy for you to differentiate.

Indeed, onpaint () is used to respond to the wm_paint message. The ondraw () virtual function is called with different parameters based on whether the onpaint () class is printed or on the screen. So in ondraw (), you can differentiate printing from screen painting.
In fact, MFC has done a lot of work before and after printing, calling a lot of virtual functions, such as onprepareprint.

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.