Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing an MFC SDI Explorer style app. I need to maintain the position of the splitter bar that the user may have set so that when the user shuts the application down and then starts it up again, the spliiter bar is in the position that the usre left it.

I have hunted around and am finding it very hard to find any info on how to do this. The splitter bar already appears in my application, but there is no reference to it in the CMyApp initialisation code.
Posted

Check this article,
CSSplitter: a splitter with the ability to save/restore its position.


The splitter bar already appears in my application, but there is no reference to it in the CMyApp initialisation code.

The splitter window will be created in your CFrameWnd derived class in SDI(normally CMainFrame class) and not in your CWinApp class.
In OnCreate function the splitter window will be created and in OnCreateClient function the views of splitter window will be initialised.
 
Share this answer
 
Comments
Jackie Lloyd 11-Aug-11 8:48am    
many thanks - this helped me solve this as detailed below
I got this to work as follows - if anyone can see something wrong with this, please let me know, as I know with MFC that sometimes even if you get something to work, it is not necessarilly right an could cause problems later

BOOL CMainFrame::DestroyWindow()
{
    int currentLeftWidth;   //current width of left view in pixels
    int minLeftWidth;       //minimum width of left view in pixels
    int currentRightWidth;  //current width of right view in pixels
    int minRightWidth;      //minimum width of right view in pixels

    try
    {
        //get the sizes of the left and right panes in pixels
        m_wndSplitter.GetColumnInfo( 0, currentLeftWidth, minLeftWidth );
        m_wndSplitter.GetColumnInfo( 1, currentRightWidth, minRightWidth );

        //save the position of the splitter bar to the registry before CWnd 
        //does cleanup
       AfxGetApp()->WriteProfileInt(_T("BWE"), _T("LeftPaneWidth"),    currentLeftWidth);

AfxGetApp()->WriteProfileInt(_T("BWE"), _T("RightPaneWidth"), currentRightWidth);
	}
	catch ( ... )
	{
		AfxMessageBox(_T("Could not save splitter bar position to registry"));
	}

	//ask CWnd to do rest of cleanup
	CWnd::DestroyWindow();
	
	return true;
}


BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
	CCreateContext* pContext)
{
	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;

	//GetProfileInt gets the pane sizes from their registry location
	if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CLeftView), CSize(AfxGetApp()->GetProfileInt(_T("BWE"), _T("LeftPaneWidth"), 50), 100), pContext) ||
		!m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CBWEView), CSize(AfxGetApp()->GetProfileInt(_T("BWE"), _T("RightPaneWidth"), 50), 100), pContext))
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}

	return TRUE;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900