dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: How to get a View handle?

  1. #1
    Join Date
    Apr 2001
    Posts
    1,029

    How to get a View handle?

    Hello,

    I have an SDI Doc/View application and I need to get access to the CFormView derived class to update some data in the view. Whats the best way to get a handle to the view class from outside that class?

    Thanks!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,175

    Re: How to get a View handle?

    From anywhere inside your MFC app, you can call AfxGetMainWnd to get the main frame window. Next call the CFrameWnd methods GetActiveDocument or GetActiveView to get the doc or the view.

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: How to get a View handle?

    Actually, the best way (since this is a Doc/View architecture) is to store the data in your document. The view should be responsible for displaying that data to the user however needed.

    Now when you need to change the data, change it in the document and then call the document class UpdateAllViews(). This will call the OnUpdate for each view, and the view can then redraw/update itself from the new document data. (Of course, you have to write the code in the OnUpdate handler to do whatever is needed.)

    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: How to get a View handle?

    Hello,

    There are many methods to do this.

    Andreas Masur, in his FAQ MFC Doc/View: How to obtain a pointer to various objects gives a good compilation on how to get various objects from any part of the application. MSDN also gives good advice to get document and view from any where in the application in their article How To Get Current CDocument or CView from Anywhere. Another method is to create a variable of application class and initialise the variable with the view / document pointer in the respective constructor.

    Regards,
    Pravin.
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

  5. #5
    Join Date
    Apr 2001
    Posts
    1,029

    Re: How to get a View handle?

    Hello,

    I tried the following code:

    Code:
    CDisplayView* view = dynamic_cast<CDisplayView*>(((CFrameWnd*)AfxGetMainWnd())->GetActiveView());
    but it gives me an error for DisplayView.h:

    error C2065: 'IDD_DISPLAY_FORM' : undeclared identifier


    Any idea why I get this error?

    Thanks!

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,175

    Re: How to get a View handle?

    The IDD_DISPLAY_FORM is a resource id to a dialog or a formview. I believe you just need to include resource.h in the file you are calling GetActiveView.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)