le_alex
le_alex

Reputation: 63

How to focus on a certain view in a MFC CSplitter

I'm trying to create an hierarchic window that contains 3 views using CMDIChildWnd, 2 CSplitterWnd's and 3 CFormView's:

  1. A form view that contains a static control
  2. A form to display the main window that I use to view a PDF document
  3. A side panel for some actions related to the main view

The main splitter (containing the MainView and TaskPane) is initialized with 1 row and 2 columns. The second splitter contains 2 rows and 1 column, containing the Tabs and the main splitter.

This image describes how it should be built: Visual description

My problem is that the MainView has no focus and therefore, the tool bar buttons are not enabled for zooming, saving as, etc...

When I'm not using the Banner Splitter, it works fine. I tried the setActivePane() setFocus() setActiveWindow() Here is how I create it in the CChildFrame::OnCreateClient()

if (m_BannerSplitter->CreateStatic(this, 2, 1))
{
    m_BannerSplitter->CreateView(0,
                                0,
                                RUNTIME_CLASS(CBannerView),
                                CSize(r.Width(),28),
                                pContext);
    m_splitter->CreateStatic(m_BannerSplitter,
                                1,
                                2,
                                WS_CHILD | WS_VISIBLE | WM_SHOWWINDOW, 
                                m_BannerSplitter->IdFromRowCol(1,0));

}
else
{
    m_splitter->CreateStatic(this, 1, 2);
}

m_splitter->CreateView(0,
                        0,
                        RUNTIME_CLASS(CMainView), 
                        CSize(r.Width()-m_splitter->m_iRightTabFullWidth-14,1),
                        pContext);
m_splitter->CreateView(0,
                        1,
                        RUNTIME_CLASS(CTasksView),
                        CSize(m_splitter->m_iRightTabFullWidth, 1),
                        pContext);

m_splitter->SetColumnInfo(1,
                            m_splitter->m_iRightTabFullWidth, 
                            m_splitter->m_iRightTabFullWidth);

Upvotes: 2

Views: 954

Answers (2)

le_alex
le_alex

Reputation: 63

I have found the solution and it was pretty simply. The object contains the splitters is a CMDIChildWnd that has SetActiveView(CView * view) function.

So I tried to get the view using:

CView *mainView = (CView *)m_splitter->GetDlgItem(m_splitter->IdFromRowCol(0, 0)); 

and than used from the CChildFrame::OnCreateClient() event

SetActiveView(mainView);

Upvotes: 0

xMRi
xMRi

Reputation: 15375

The problem is that the command routing always checks the active view followed by the document.

If all commands should be handled by the appropriate view, no matter what view has the focus just implement your own command routing.

So overwrite CMainFrame::OnCmdMsg.

BOOL CMainFrame::OnCmdMsg(UINT nID,int nCode,void* pExtra,AFX_CMDHANDLERINFO* pHandlerInfo)
{
  // Do the standard routing (View, Frame, Application)
  if (__super::OnCmdMsg(nID,nCode,pExtra,pHandlerInfo))
    return TRUE;

  // If not handled ask all other views to handle the command
  return m_pBannerView->OnCmdMsg(nID,nCode,pExtra,pHandlerInfo) ||
         m_pMainView->OnCmdMsg(nID,nCode,pExtra,pHandlerInfo) ||
         m_pTaskView->OnCmdMsg(nID,nCode,pExtra,pHandlerInfo);
}        

My code will just offer every command to every available view after it was not handled by the normal routing.

Upvotes: 0

Related Questions