set focus on view

Posted on 1999-07-29
Medium Priority
Last Modified: 2013-11-20
I have a sdi split window app.

From a function in one view I would like to put the focus back on the other view.

I asked this Q before, and I was told I could use:
from Anywhere in my code.

Or do the following:
  // code above

the problem I have is how do I use SetActivePane from within a cview class. Do I need to get the CSplitterWnd object from CMainFrame? How?

And if I was using the second method how do I get pview2 so that it corresponds to the view on screen.

I think both probs are along similar lines, and probably indicate some lack of understanding in c++.

If someone could please explain to me how I should be trying to so this I would be very grateful.
Question by:lukis
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 23

Accepted Solution

naveenkohli earned 400 total points
ID: 1311950
Hi Lukis,
Here is sample code which switches views in splitter windows. It appeared in one of the MFC books..

Hope it helps ..

void EkSwitchViewInSplitter(      CSplitterWnd* pSplitter,
                                                int row, int col,
                                                CRuntimeClass* pViewClass )
      ASSERT_VALID( pSplitter );
      ASSERT( pViewClass != NULL );

      ASSERT( pViewClass->
                        IsDerivedFrom( RUNTIME_CLASS( CView ) ) );

      // 1 - Find the view to be replaced
      CWnd* pPaneWnd = pSplitter->GetPane( row, col );
      if( !pPaneWnd->IsKindOf( RUNTIME_CLASS( CView ) ) )
            TRACE2(      "Unable to switch: pane (%d,%d) is not a view\n",
                        row, col );

      CView* pCurrentView = static_cast<CView*>( pPaneWnd );
      ASSERT_VALID( pCurrentView );
      ASSERT_KINDOF( CView, pCurrentView );

      if( pCurrentView->IsKindOf( pViewClass ) )
            // No need to switch for same view class

      // 2 - Store current view position and activation state
      CRect rcView;
      pCurrentView->GetWindowRect( &rcView );

      CView* pActiveView = pSplitter->
      BOOL bSaveActive = ( pActiveView == NULL )
                                    || ( pActiveView == pCurrentView );

      // 3 - Find the associated document
      CDocument* pDoc = pCurrentView->GetDocument();
      ASSERT_VALID( pDoc );

      // 4 - Make sure the document won't self-destruct
      // when current view is destroyed
      BOOL bSaveAutoDelete = pDoc->m_bAutoDelete;
      pDoc->m_bAutoDelete = FALSE;

      // 5 - Destroy the current view

      // 6 - Restore document to initial state
      pDoc->m_bAutoDelete = bSaveAutoDelete;

      // 7 - Initialize creation context used by CreateView()
      CCreateContext context;
      context.m_pNewDocTemplate = NULL;
      context.m_pLastView = NULL;
      context.m_pCurrentFrame = NULL;

      context.m_pNewViewClass = pViewClass;
      context.m_pCurrentDoc = pDoc;

      // 8 - Create the new view
      pSplitter->CreateView(      row, col, pViewClass,
                                          rcView.Size(), &context );

      CView* pNewView = static_cast<CView*>
                                    ( pSplitter->GetPane( row, col ) );
      ASSERT_VALID( pNewView );
      ASSERT_KINDOF( CView, pNewView );

      // 9 - Position the new view like the old one and
      // activate it if needed
      pSplitter->ScreenToClient( &rcView );
      pNewView->MoveWindow( &rcView, TRUE );
      if( bSaveActive )
            pSplitter->GetParentFrame()->SetActiveView( pNewView );
      // 10 - Send WM_INITIALUPDATE to the view
      pNewView->GetParentFrame()->InitialUpdateFrame( pDoc, TRUE );


Author Comment

ID: 1311951
but doesn't this require me to already have the correct csplitterwnd object to pass to the function?
How do I get this object if it is in the CmainFrame object?
LVL 23

Expert Comment

ID: 1311952
This is a utility function which you can use from anywhere. Since the splitter window variable is in mainframe therefore it is easy to get to it cause we can get the pointer to mainframe from anywhere. Declare the CSplitterWnd memeber variable as public in CMainFrmae class and then whereever u need it, get the pointer to mainframe and then use it.

CMainFrame *pFame = static_cast<CMainFrame *>(AfxGetMainWnd ());
ASSERT (pFrame != NULL);

// Then we have access to spliter window variable

Author Comment

ID: 1311953
Thanks. I really appreciate the help.

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question