CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2001
    Location
    PA
    Posts
    183

    Two views in splitterwindow, same base class

    Hello,

    I was woondering how can I use the same class for 2 different views. I have a SDI application with splitter window and I want to use the same class called "CMyFormView" for both the views. Here is a creation code

    Code:
    .
    ...
    ......
    ........
    ......
    .....
    
    	
    if(!m_wndSplitter2.CreateView(0, 0,RUNTIME_CLASS(CMyFormView), CSize(0, 400), pContext))
    		return false;
    
        if (!m_wndSplitter2.CreateView(1,0 ,RUNTIME_CLASS(CMyFormView), CSize(0, 0), pContext))
    
    
    ......

    In the CFormView class, I have member variable called iSwitch to keep track of which CFormView object I am dealing with. I need to initialize this variable appropriately during the creation of the CFormView objects. However, how can I initialize this variable during the construction of the view object in mainframe class as shown above?

    You might say that I should create another class derived from CMyFormView and no more functionality to it. But I want to avoid creating another class if possible. Any suggestions? Thanks

    m m

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    I am not familiar with MFC internal, but how do you go about handling message if there are two MFC views and one class in the project?

    Kuphryn

  3. #3
    Join Date
    Jul 2001
    Location
    PA
    Posts
    183

    I figured it out

    How can I have not known this? I posted the question and then after an hour or so realized that the answer is straight forward. The simple solution is to use static variables. And that is the answer to your question too.

    In the code above, I am creating two views based on the same class called "CMyFormView". If you look at the creation code above in the mainframe class, you will see that the view in the top window is created before the view in the bottom window. Therefore, when the bottom view is created, the top view has already been created.

    Add following member variables to the CFormView class

    Code:
    static int formInstance;   
    int iSwitch;
    In the constructor of CFormView class add the following:

    Code:
    CMyFormView::CMyFormView()
    	: CFormView(CMyFormView::IDD)
    {
    	formInstance++;
    	iSwitch = formInstance;
    
    //{{AFX_DATA_INIT(CMyFormView)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT    
    						
    }
    In the destructor of the CFormView class add the following:

    Code:
    CMyFormView::~CMyFormView()
    {
    
    	formInstance-- ;
    }

    Somewhere in your FormView.cpp class you need to add:

    Code:
    int CFormView:: formInstance = 0;

    Therefore, since FormInstance is a static variable and belongs to the class, it will be set to 1 when the top view is created and incremented to 2 when the bottom view is created.

    Handle messages like this:

    Code:
    CFormView :: MasHandler( )
    
    {   
                  if(iSwitch == 1)
                                            // Do this
                 if(iSwitch == 2)
                                           // Do something else
    }

    Hope that helps. It took me a while to realize that I can use static member variables since I do not need to use them often. I know I answered my own question. Hope I answered yours too.

    Sincerely
    m m

  4. #4
    Join Date
    Feb 2002
    Posts
    5,757
    Correct. I wanted to know how do you destinguish messages from, for example, the top view from bottom view? There is one class and two MFC objects. If main frame sends a message to the top view, how will the class know which view main frame is targetting?

    Are you using some kind of indicator in the wParam or lParam?

    Kuphryn

  5. #5
    Join Date
    Jul 2001
    Location
    PA
    Posts
    183

    mssange from mainframe to formview??

    could you give me a few lines of code to show what kind of messages are you asking about? Thanks. I'll get back to you.

    Sincerely
    m m
    Are you too gullible? I can cure you! Send $1,000 to...

  6. #6
    Join Date
    May 2002
    Location
    Sydney, Australia
    Posts
    4

    Splitter windows, and which view?...

    In terms of which view receives a message, part of the rationale of the Doc/View architecture is that you shouldn't have to worry about that. All a view should need to care about is keeping in sync with its document, handling the messages it receives, and displaying the document in its particular way. The framework determines for you which view in a splitter receives a message.

    It seems to me that if your views need to know which pane they're in (and will display differently depending on the answer?), then strictly speaking they're different views. It's a bit of a hack to keep it to one class which works two different ways ... though if your solution works, maybe it's a good hack!

    By the way, have you looked at the "Scribble" sample application in the VC++ documentation? It demonstrates using a splitter window with the same CView in each part, and talks about how to do it in SDI apps.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured