MFC window Redraw

Source: Internet
Author: User
Tags message queue

What is the difference between Invalidate () and UpdateAllViews ()

Invalidate () is to have the program repaint the window.

UpdateAllViews () is in the Doc/view structure,

When the data of a view changes,

Notify all views to change accordingly.

It has nothing to do with redrawing.

Invalidate () is to make the window invalid so that the system sends a WM_PAINT message to it so that the OnPaint of the program is called to redraw the client area.

and UpdateAllViews () is the link between the document and the view, called from which it will make the program related to this document all the Updateview of the view is called as to whether redrawing and how the painting is determined by the updateview of each view.

Invalidate () is a member function of CWnd, unrelated to Doc-view;

UpdateAllViews is the member function of CDocument, which embodies doc-view spirit.

If only the current window is redrawn with this->invalidate ();

If you are notified that all the Windows associated with the current document are redrawn with GetDocument ()->updateallviews () (in view) or This->updateallviews () (in doc).

The Invalidate () function generates a WM_PAINT message and feeds into the Windows message queue, which is the window that produces the repaint.

Instead, UpdateAllViews does not enter the Windows message queue, producing a redraw directly

Summary of Invalidate function

InvalidateRect only adds redraw area, only takes effect the next time wm_paint.

The parameter true in the InvalidateRect function indicates that the system overwrites the selected area once with the background color before you draw it, the default background color is white, and you can change the background color by setting the brush.

Invalidate () After: (MFC, by the way)

OnPaint ()->onpreparedc ()->ondraw ()

So just refresh the drawing statements in the OnPaint () and OnDraw () functions. Other places have no effect.

Invalidate marks an invalid area that needs to be redrawn, and does not mean that the function is redrawn immediately after it is called. Similar to PostMessage (WM_PAINT), it needs to be processed to WM_PAINT message when it is actually redrawn. The program does not have the opportunity to process the WM_PAINT message after you invalidate that there are other statements being executed, but the message processing is performed when the function is finished.

Invalidate just put a WM_PAINT message in the queue, do nothing else, so only when the current function returns, enter the message loop, take out the WM_PAINT, only execute PAINT, so no matter where invalidate put, is the last.

InvalidateRect (hwnd,&rect,true); sends a WM_PAINT message to the HWnd form, forcing the customer area to redraw,

Rect is the area that you specify to refresh, the customer area outside this area is not redrawn, which prevents a local change in the customer area, causing the entire customer area to redraw and cause flicker, and if the last argument is true, the WM_ERASEBKGND message is also sent to the form, causing the background to redraw, Before the customer area is redrawn, of course.

UpdateWindow only sends a WM_PAINT message to the form, GetUpdateRect (hwnd,null,true) to see if there is no customer area to draw before sending it, and if not, does not send WM_PAINT

If you want to flush an invalid zone immediately, you can call UpdateWindow after calling InvalidateRect, and if any part of the client area is not valid, UpdateWindow will cause Windows to use WM_ The paint message invokes the window procedure (does not call the window procedure if the entire client area is valid). This WM_PAINT message does not enter the message queue and is called directly by Windows to the window procedure. When the window procedure finishes flushing and exits immediately, Windows controls the return to the statement after the UpdateWindow call in the program. (Windows Programming version 5th P98)

UpdateData () by the way, this function is not for the refresh interface.

UpdateData (); When the parameter is false, the data for the variable bound by the control on the interface is directed to the controls, and the import direction is the opposite when the parameter is true.

void updateallviews ( cview* psender, LPARAM lHint = 0L , cobject* phint = NULL );

NULL If all views is to be updated.

Remarks

Call this function after the document has been modified. You should call the "this function" after the member function. This function informs each view attached to the document, except for the view specified by Psender, that the docu ment has been modified. You typically call this function from your view class after the user has changed the document through a view.

Parameter: Psender A pointer to the view that modifies the document. If all views are updated, NULL is returned.
LHint contains the modified information.
Phint The object pointer that contains the modification information.
Description
This function is called after the document has been modified. Should be called after SetModifiedFlag is called. In addition to the view specified by Psender, the function notifies the message that the individual viewport documents connected to the document have been modified. This function is typically called after a user changes a document through a view.
In addition to sending views, this function invokes cview::onupdate for each document view and passes Lhint and phint values, using these parameters to pass the document's modifications to the view. You can use Lhint to encode information, (or) define a CObject derived class to save the modification information, and use Phint to pass a class object. To optimize the view update, the Cview::onupdate member function can be overridden in a CView derived class.

Call Invalidate in the Doc class to redraw the MFC window!

Get the view pointer, and then use invalidate to redraw the MFC window.

The code is as follows:

Cuabapp *papp= (Cuabapp *) AfxGetApp ();

CMainFrame *pmainframe= (CMainFrame *) papp->m_pmainwnd;

CChildFrame *pframe= (CChildFrame *) pmainframe->getactiveframe ();

CView *pview= (CView *) Pframe->getactiveview ();

Pview->invalidate ();

Method Two:

POSITION pos = getfirstviewposition ();

cview* PView = GetNextView (POS);

Pview->invalidate ();

The difference between OnDraw and OnPaint functions

Cwnd::onpaint () is a member function of the CWnd class and is the response function of the WM_PAINT message. When you call Cwnd::updatewindow (), Cwnd::redrawwindow (), or the window is overwritten by another window, WM_PAINT messages are generated when events such as resizing are changed. You can use it in a CWnd-derived subclass.

CView::OnDraw () is a pure virtual function of the CView class. You have to use it, you must reload it.

Cview::onpaint () will call it.

OnPaint is called when the WM_PAINT message is received, OnPaint calls the OnDraw function, and since OnDraw is a virtual function, you can rewrite it yourself, so it's implemented, different plotting purposes

The OnDraw is used for CView, while OnPaint is used for CDialog

Onpait responds to the WM_PAINT message, and in CView, it then calls OnDraw

CPAINTDC response to WM_PAINT messages, drawing is done automatically

CLIENTDC Real-time rendering

In general, you do not see any difference in the two functions because, in general, their task is to repaint the window.

In CView and its subclasses, as the Wave Knife says, OnDraw is called by OnPaint, which means that you can invoke invalidate functions as in the window class, resulting in OnDraw calls (of course, invalidate is the first to cause OnPaint).

The difference between OnDraw and OnPaint is that it can draw to the screen, as well as any other standard output device, such as a printer

MFC window Redraw

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.