Click here to Skip to main content
15,887,376 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an MFC SDI CFormview based app and I just successfully added another view derived from CView, I'm stuck on adding my document data to this new view. I can add text to it in the OnDraw section by doing this: pDC->TextOutW(100, 250, L"WOW!!, this is really confusing!!"); How do I display my data from my document?
Posted

The CView class has a method called GetDocument that returns a pointer to the document.
You can typecast this returned CDocument pointer to a pointer of your derived document class.
If you've created the new view using the wizard, there should be a wrapper method called GetDocument in the view class.
Otherwise, check the form view class on how it is done.
 
Share this answer
 
As already noted by _Superman_, use GetDocument() to get the data from your document class.

OnDraw() of views is rather seldom used. If your view is derived from a Windows control or contains multiple controls, call the control specific update functions like SetWindowText(), SetItem() from the OnUpdate() function of your view. Use OnInitialUpdate() to initiliaze the controls after the view has been created like OnInitDialog() with dialogs.

A simple example:
C++
void CMyView::OnInitialUpdate()
{
    m_editText1.SetLimitText(255);
    CView::OnInitialUpdate();
}
void CMyView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
    m_editText1.SetWindowText(GetDocument()->GetText1());
}


The lParam and pHint parameters can be used to update only specific parts of your view. To trigger an update, use CDocument::UpdateAllViews().
 
Share this answer
 
Comments
DrBones69 30-Jan-12 22:28pm    
I'm using this http://msdn.microsoft.com/en-us/library/s199bks0.aspx method to add my additional view to my project. It works, but when I create a class that uses CView as the base I only get OnDraw created by the wizard and in the OnDraw is the "CDocument* pDoc = GetDocument();" inside. Where do I go from here to display one item in my document?
Jochen Arndt 31-Jan-12 3:15am    
It depends on your data and how you want it to be drawn. To get the item data, make the data member of your documant class public, or add a GetItem() function. Then you can use GetDocument() from your view to access the item data. If you are using common windows controls inside your view, let OnDraw() as it is and add the two virtual update functions to your view class using the Overrides tab in the Properties window.
DrBones69 4-Feb-12 21:42pm    
May I add some of my code to illustrate what I have setup? There are about 200 lines of code. This is not the complete app, but it may help you to explain to me the correct path to take.
Jochen Arndt 5-Feb-12 5:00am    
200 lines are too much. Especially for comments.

Please tell us about the kind of data (text, numbers, graphics) that should be shown, and how they should be shown. Based on this, the view class has to be designed.
DrBones69 5-Feb-12 10:42am    
I'm trying to create a view that I can print. I read numerous articles that tell me I can not print from the CFormView class, so I created a new CView class within my project and a switching routine to display my document and I would like to print this information. It(Document) consists of text and numbers. Maybe I'm going about this all wrong? I also have never used a CView before, I'm really good with the dialog based apps now. I'm still learning...
Microsoft had this[^] outdated article on printing from any class other than CView. With a few minor tweeks, I'm printing my data. :-)

-DrB
 
Share this answer
 
v2
The "view" is designed for custom drawing. So, you receive a CDC* and you draw on the View.

The "document" is where you put/get the information. But, of course, you must adapt it to your needs.
It has two virtual methods: OnNewDocument and Serialize that you must override to make it work the way you want.

If you like a screen with controls (not custom drawn) I reccomend you use a CFormView.
 
Share this answer
 
v2
Comments
DrBones69 31-Jan-12 22:02pm    
I'm already using CFormView, this questions is for an additional view. See question above.
ErnestoNet 1-Feb-12 7:17am    
Sorry, English is not my natural language. I meant that you may need another CFormView (instead of a CView).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900