Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Newbie learning MFC, confused about Document/View

Newbie learning MFC, confused about Document/View

Scheduled Pinned Locked Moved C / C++ / MFC
c++questioncsharpjavavisual-studio
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maxmaven
    wrote on last edited by
    #1

    Hi, I'm learning MFC, with prior experience in java, so I have a little bit of understanding about the general concept of a document and a view (I hope). I'm trying to figure out some missing knowledge I don't have about the concept of Document/View in MFC when using a splitter window. I have two questions. The first question, I will explain something I did and ask your advice. I think I did it the "right" way, but I'm not 100%. By the right way, I am meaning, the way that makes the most sense and uses MFC as properly as possible. I know there are lots of ways to do the same thing, I am just hoping I did it the most appropriate way. My second question points out that I am missing something major. I know I did it the wrong way. Could you point me towards the proper way? All this was done in Visual Studio 2005. Question 1: I have a MainFrame and the App Wizard added a window splitter for me. I made the splitter static with one row, two columns. The left pane has a CTreeView subclass. The right pane displays one of two CFormViews, call them info_pane_1 and info_pane_2 (imagine a project settings kind of situation). There are two items in the left CTreeView pane, clicking on the first item causes info_pane_1 to appear in the right pane. Similarly, clicking on the second item causes info_pane_2 to appear in the right pane. I didn't implement any of the fancy splitter stuff myself, I used "CUsefulSplitterWnd" on this website. The only code I needed to add was a message handler, OnTvnSelchanged(...) to my CTreeView subclass. In this function, I figure out which item was clicked on in the tree view, then I get a pointer to the parent frame (MainFrame). I call a new member I added to MainFrm.cpp called ChangeDialog(int which). The variable "which" is either 1 for info_pane_1 or 2 for info_pane_2. The function ChangeDialog(...) in MainFrm.cpp is just: (paraphrasing)

    switch(which)

    case 1:
    m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_1),...)

    or

    case 2:
    m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_2)

    Everything works exactly as I had hoped; Clicking in the tree (in the left pane) dynamically changes what appears in the right pane. Did I do this the right way? Question 2: Here is where I'm confused about how to properly use the Doc/View architecture. What I need to do is simple: The user clicks on the various checkboxes and radio-boxes in info_pane_1, and I have an "Apply" button. When the user clicks "Apply", the state of the

    G 1 Reply Last reply
    0
    • M maxmaven

      Hi, I'm learning MFC, with prior experience in java, so I have a little bit of understanding about the general concept of a document and a view (I hope). I'm trying to figure out some missing knowledge I don't have about the concept of Document/View in MFC when using a splitter window. I have two questions. The first question, I will explain something I did and ask your advice. I think I did it the "right" way, but I'm not 100%. By the right way, I am meaning, the way that makes the most sense and uses MFC as properly as possible. I know there are lots of ways to do the same thing, I am just hoping I did it the most appropriate way. My second question points out that I am missing something major. I know I did it the wrong way. Could you point me towards the proper way? All this was done in Visual Studio 2005. Question 1: I have a MainFrame and the App Wizard added a window splitter for me. I made the splitter static with one row, two columns. The left pane has a CTreeView subclass. The right pane displays one of two CFormViews, call them info_pane_1 and info_pane_2 (imagine a project settings kind of situation). There are two items in the left CTreeView pane, clicking on the first item causes info_pane_1 to appear in the right pane. Similarly, clicking on the second item causes info_pane_2 to appear in the right pane. I didn't implement any of the fancy splitter stuff myself, I used "CUsefulSplitterWnd" on this website. The only code I needed to add was a message handler, OnTvnSelchanged(...) to my CTreeView subclass. In this function, I figure out which item was clicked on in the tree view, then I get a pointer to the parent frame (MainFrame). I call a new member I added to MainFrm.cpp called ChangeDialog(int which). The variable "which" is either 1 for info_pane_1 or 2 for info_pane_2. The function ChangeDialog(...) in MainFrm.cpp is just: (paraphrasing)

      switch(which)

      case 1:
      m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_1),...)

      or

      case 2:
      m_wndSplitter.ReplaceView(0,1,RUNTIME_CLASS(info_pane_2)

      Everything works exactly as I had hoped; Clicking in the tree (in the left pane) dynamically changes what appears in the right pane. Did I do this the right way? Question 2: Here is where I'm confused about how to properly use the Doc/View architecture. What I need to do is simple: The user clicks on the various checkboxes and radio-boxes in info_pane_1, and I have an "Apply" button. When the user clicks "Apply", the state of the

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      maxmaven wrote:

      think I don't have a good understanding of what to do with this Document/View architecture

      Don't feel bad; document/view isn't the easiest thing in the world, which may explain why it's not very popular.

      maxmaven wrote:

      void info_pane_1::OnDraw(CDC *pDC)
      { CCodeBuilderDoc *p_Doc = (CCodeBuilderDoc *) GetDocument();
      ASSERT_VALID(p_Doc);
      state_InsertionSortCheck = p_Doc->state_InsertionSortCheck;
      UpdateData(FALSE);
      }

      Instead of getting the check state from your document and initializing the view in your OnDraw override, you should do it in either your OnInitialUpdate() or OnUpdate() override.

      maxmaven wrote:

      I don't understand why my project/solution has a CMyAppView descended from CView in MyAppView.cpp/.h. I haven't done anything to this class. Is it being used?

      If you aren't referencing it and haven't implemented it, you can probably remove it. As a suggestion, save a copy of your project and its source files in another folder, so that if you need to revert back to its current state, you can.


      Software Zen: delete this;

      Fold With Us![^]

      M 1 Reply Last reply
      0
      • G Gary R Wheeler

        maxmaven wrote:

        think I don't have a good understanding of what to do with this Document/View architecture

        Don't feel bad; document/view isn't the easiest thing in the world, which may explain why it's not very popular.

        maxmaven wrote:

        void info_pane_1::OnDraw(CDC *pDC)
        { CCodeBuilderDoc *p_Doc = (CCodeBuilderDoc *) GetDocument();
        ASSERT_VALID(p_Doc);
        state_InsertionSortCheck = p_Doc->state_InsertionSortCheck;
        UpdateData(FALSE);
        }

        Instead of getting the check state from your document and initializing the view in your OnDraw override, you should do it in either your OnInitialUpdate() or OnUpdate() override.

        maxmaven wrote:

        I don't understand why my project/solution has a CMyAppView descended from CView in MyAppView.cpp/.h. I haven't done anything to this class. Is it being used?

        If you aren't referencing it and haven't implemented it, you can probably remove it. As a suggestion, save a copy of your project and its source files in another folder, so that if you need to revert back to its current state, you can.


        Software Zen: delete this;

        Fold With Us![^]

        M Offline
        M Offline
        maxmaven
        wrote on last edited by
        #3

        This is why I'm confused. So just to recall, I have UsefulSplitterWnd and two child views of the split. The left one is a CTreeView and the right one is a CFormView. Each of the CFormViews has an associated dialog that it displays. Here's what I've tried with these CFormViews. First, I tried overriding OnCreate(...). But this function is never called at any time during execution of my application. Next, I tried overriding OnUpdate(...) and OnInitialUpdate(...). I put breakpoints in these functions and found that they are only executed once, at the start up of the application. They are never called again, even when CUsefulSplitterWnd::replaceview(...) is called. There are basically two important functions that CUsefulSplitterWnd::replaceview(...) calls. They are:

        // Delete existing view
        ((CView *) GetPane(row,col))->DestroyWindow();

        This destroys the window associated with the CFormView. However, in searching the MSDN help docs, it says that this function doesn't destroy the CFormView class object. So my class must be around somewhere (?). The second important function is:

        // Create new view

        context.m_pNewViewClass=pViewClass;
        context.m_pCurrentDoc=pDoc;
        context.m_pNewDocTemplate=NULL;
        context.m_pLastView=NULL;
        context.m_pCurrentFrame=NULL;

        CreateView(row,col,pViewClass,size, &context);

        This should be telling me what function I need to override so that the dialog is initialized to the saved values in my CDocument subclass. But, I tried all the sensible functions. They are either never called in the app, or only called once at app startup. I'm pretty lost. :confused: Max

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups