The introduction of MFC Self-drawing (iii.)

Source: Internet
Author: User

OnPaint is a message handler for the WM_PAINT message, OnDraw is called in OnPaint, and in general, the user's own drawing code should be placed in OnDraw.

OnPaint () is a class member of CWnd and is responsible for responding to WM_PAINT messages. OnDraw () is a member function of the CView, and there is no function to respond to the message. When the view becomes invalid (including size changes, moves, obscured, and so on), Windows sends a WM_PAINT message. The view's OnPaint handler functions by creating the DC object of the CPaintDC class to respond to the message and invoke the OnDraw member function of the view. OnPaint also calls OnDraw, so it is generally drawn in the OnDraw function.


The WM_PAINT message is sent if the UpdateWindow or RedrawWindow member function is called.

In OnPaint, BeginPaint is called to get the display device environment for the client area and to invoke the GDI function to perform the drawing operation. After the drawing operation is complete, EndPaint is called to release the display device environment. And OnDraw is called between BeginPaint and EndPaint.

1) OnPaint is a member function of CWnd in the MFC structure. OnDraw is a member function of CView.
2) OnPaint () calls OnDraw (), OnPrint also calls OnDraw (), so OnDraw () is a common operation for display and printing.


OnPaint is a redraw message handler that is thrown by WM_PAINT messages, and OnDraw is called in OnPaint to draw. OnPaint first constructs an instance of the CPaintDC class, and then an instance of the argument to call the virtual function OnPrepareDC to do some processing before the drawing, than to set the mapping mode, and finally call OnDraw. OnDraw and OnPrepareDC are not message handler functions. So if the onpaint caused by redrawing the message causes OnDraw to be called, such as when drawing in a message handler such as OnLButtonDown, call OnPrepareDC yourself.
As for CPaintDC and CClientDC, it's a different thing. CPaintDC is a device environment class that is passed as a parameter in OnPaint to OnPrepareDC for setting up the device environment. The real and CCLIENTDC are comparable to CWINDOWDC, one of them is to describe the customer area, and one is to describe the entire screen.
You should use OnDraw if you are drawing on CView or a window that derives from the CView class.

What is the difference between OnDraw () and OnPaint ()?
First, let's make it clear that the CView class derives from the CWnd class. OnPaint () is a class member of CWnd and is responsible for responding to WM_PAINT messages. OnDraw () is a member function of CView and does not have the capability to respond to messages. This is why you use VC into the program code when the view class only OnDraw no OnPaint reason. In a dialog-based program, only OnPaint.
Second: We have already spoken on the beginning of the first 3 of "learning MFC with me every day". To draw or display graphics on the screen, you first need to set up a device environment DC. In fact, the DC is a data structure that contains the description of the drawing properties of the output device (not just your 17-inch flat-screen display, but also the output device such as the printer). MFC provides CPAINTDC classes and CWINDWODC classes to respond in real time, while CPAINTDC supports redrawing. When the view becomes invalid (including changes in size, movement, cloaking, and so on), Windows sends WM_PAINT messages to it. The view's OnPaint handler functions by creating the DC object of the CPaintDC class to respond to the message and invoke the OnDraw member function of the view. Usually we do not have to write the overridden OnPaint processing member function.
CView Default standard Redraw function
void Cview::onpaint ()//See VIEWCORE.CPP
{

CPAINTDC DC (this);
OnPrepareDC (&DC);
OnDraw (&DC); Called the OnDraw
}
CView Default Standard OnPrint function
void Cview::onprint (cdc* PDC, cprintinfo*)
{
Assert_valid (PDC);
OnDraw (PDC); Call Draw
}

Since OnPaint is finally going to call OnDraw, we usually draw in the OnDraw function. The following is a typical program.
The drawing code in the view first retrieves a pointer to the document and then makes a draw call through the 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 you know the relationship between the two brothers. So we generally use OnPaint to maintain the client area of the window (for example, our window client area plus a background image), to maintain the client area of the view with OnDraw (for example, we draw in the view with the mouse). Of course you can not follow the above rules, as long as the goal and no problem, how to do it. Add: We can also use the invalidate (), ValidateRgn (), ValidateRect () function to force the redraw window, please refer to MSDN for details.

The user area can be drawn in OnDraw. OnPaint is only redrawn when the window is invalid and does not preserve CCLIENTDC-drawn content.

The two functions have a difference and are related:

1, Difference: OnDraw is a pure virtual function, defined as virtual void OnDraw (cdc* PDC) = 0; OnPaint is a message response function that responds to a wm_panit message and is a window redraw message.

2, Contact: We generally in the visual class in the drawing, often do not directly respond to the WM_PANIT message, but overloaded OnDraw pure virtual function, this is because in the CView class Wm_panit message response function called OnDraw function, If the OnDraw function is not explicitly called in response to the WM_PAINT message in the CMyView class, the OnDraw function is not called when the window is redrawn.

Almost all of the drawings in the application occur in the OnDraw member function of the view and must be overridden in the view class. (Mouse drawing is a special case, which is discussed in interpreting user input through the view.) )


OnDraw rewrite:
Get the data by calling the document member function that you provide.
Displays data by invoking the member function of the device context object passed to OnDraw by the framework.
When the document's data changes in some way, the view must be redrawn to reflect the change. The default OnUpdate implementation invalidates the entire working area of the view. When the view becomes invalid, Windows sends a WM_PAINT message to it. The view's OnPaint handler functions by creating a device context object for the CPaintDC class to respond to the message and invoke the OnDraw member function of the view.

When the window is redrawn without adding WM_PAINT message processing, the message is answered by OnDraw ... When you add WM_PAINT message processing, the WM_PAINT message is posted and the message responds by OnPaint when the window is redrawn. You cannot implicitly invoke OnDraw. Must be explicitly called (CDC *pdc=getdc (); OnDraw (PDC); )..
Implicit invocation: The system calls CView::OnDraw (&PDC) automatically when a message response is made by OnPaint.


Imagine that the window shows the content and the content of the print is similar, so, in general, unified by OnDraw to draw. When the foreground of the window needs to be refreshed, the system will call to OnPaint, and OnPaint typically calls OnDraw () after some initialization of the DC.


OnEraseBkgnd (), which is called by the system when a window background needs to be refreshed. An obvious example is setting the background color of the window (you can put this in OnPaint, but it will cause flicker).
As to how to define the background and prospects, it is specific to the problem of specific analysis, in general, you still very easy to distinguish it.

Indeed, OnPaint () is used to respond to WM_PAINT messages, depending on whether the OnPaint () inside the class calls the OnDraw () virtual function, respectively, with different parameters, based on printing or screen drawing. So in OnDraw () you can differentiate between printing and screen drawing.
In fact, MFC has done a lot of work before and after printing, called a lot of virtual functions, such as onprepareprint () and so on.

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.