• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

set focus on view

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:
CSplitterWnd::GetActivePain()
from Anywhere in my code.

Or do the following:
CView1::function()
{
  // code above
   pview2->SetFocus();
}

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.
thanks.
0
lukis
Asked:
lukis
  • 2
  • 2
1 Solution
 
naveenkohliCommented:
Hi Lukis,
Here is sample code which switches views in splitter windows. It appeared in one of the MFC books..

Hope it helps ..

Cheers!
-------------------------------
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 );
            return;
      }

      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
            return;
      }

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

      CView* pActiveView = pSplitter->
                                    GetParentFrame()->GetActiveView();
      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
      pCurrentView->DestroyWindow();

      // 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 );
}

0
 
lukisAuthor Commented:
thanks,
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?
0
 
naveenkohliCommented:
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
//pFRame->m_wnSpliterWnd;
0
 
lukisAuthor Commented:
Thanks. I really appreciate the help.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now