Update views in MFC to track events in the. NET Framework

Source: Internet
Author: User

This article supporting source code

How do I update a view in MFC?

How do I track events in the. NET Framework?

I intend to update all the child windows in the MDI program through the timer events in CMainFrame. Views are used to display many charts. You can only update the currently active window with the following code:

GetActiveWindow()->GetActiveView()->GetDocument()

Is there another way to get all the child windows or all the documents from the Cmdiframe class?

Makarand

Your situation is not uncommon. Many programs that collect real-time data need to update the screen regularly. Even if your program is not collecting real-time data, you need to update the view as the user's actions change the document. The basic idea of the Doc/view model (including all the Object/view models) in MFC is the separation of data from display. When a user or actual event changes the underlying object, data, or document, it is passed to the display mechanism by immediate updating of the event through some views.

For the same document if there are several views, MFC already has a mechanism to update all views one-step. This function is CDocument::UpdateAllViews, which calls cview::onupdate for each view of the open document. You can pass an application-specific "hint" describing which update operation to perform. For example, if you know that only the title of a document has changed, you can define an enumeration value Changed_title and pass it as a hint code. If your document contains pictures and text, you can define enumeration values Changed_text and Changed_graphics. The purpose of these hint codes is to improve performance. By telling the view what has changed, you can more intelligently redraw only those areas of the screen that really need to be refreshed, avoiding potentially time-consuming drawing operations or screen flashes.

UpdateAllViews Update all the views associated with a document, but how do you update all the documents? There is no such function as Updatealldocuments in MFC, so you need to enumerate all the documents yourself. This requires the implementation of a circular operation on document templates and related documents, as follows:

for (/* each CDocTemplate in app */) {
for (/* each CDocument in CDocTemplate */) {
// do something
}
}

Since enumerating documents is so useful, I've written a very small class cdocenumerator, hiding all the templates and location mechanisms in MFC. In fact, this class is what I wrote back in September 1995--Oh, it's almost ten years ago. The code looks like Figure 1. Using Cdocenumerator, you can easily enumerate all open documents in your program.

CDocEnumerator it;
CDocument* pdoc;
while ((pdoc=it.Next())!=NULL) {
// do something
}

What could be easier than that? To illustrate the use of this class in the actual example, I wrote a small program updview, which simulates the real-time data acquisition program. Each document in the Updview counts the number of seconds it opens. Figure 2 shows the Updview in the work. If you download, build, and run Updview, you can see the number of seconds that each view updates the display document open per second. In Figure 2, the document named File2.dat has two views, all of which display the same underlying document. Each document maintains its own number of time since it was opened (data), and the view is only displayed (performance). In your own program, Updview works through the main frame timer setup. This timer handling event uses Cdocenumerator to tell each document to collect more data, as shown below:

void CMainFrame::OnTimer(UINT_PTR nIDEvent) {
CDocEnumerator it;
CDocument* pdoc;
while ((pdoc=it.Next())!=NULL) {
((CMyDoc*)pdoc)->CollectMoreData();
}
}

Figure 2 Updview in operation

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.