在关闭和启动之间保持分离条的位置 [英] Maintaining splitter bar position between shutdown and startup

查看:41
本文介绍了在关闭和启动之间保持分离条的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写MFC SDI Explorer样式的应用程序.我需要保持用户可能已设置的分隔条的位置,以便当用户关闭应用程序然后再次启动它时,分隔条处于原位置.

我已经四处搜寻,并且发现很难找到有关如何执行此操作的任何信息.分隔条已经出现在我的应用程序中,但是在CMyApp初始化代码中没有对它的引用.

解决方案

检查本文,
CSSplitter:具有保存/恢复其位置功能的拆分器.


分隔条已经出现在我的应用程序中,但是在CMyApp初始化代码中没有对它的引用.

拆分器窗口将在SDI的CFrameWnd派生类(通常为CMainFrame类)中创建,而不在CWinApp类中创建.
在OnCreate函数中将创建拆分器窗口,在OnCreateClient函数中将初始化拆分器窗口的视图.请让我知道,正如我在MFC中所知道的那样,有时即使您可以使用某些东西,也不一定是正确的,以后可能会引起问题

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


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.

解决方案

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.


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


这篇关于在关闭和启动之间保持分离条的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆