【MFC】多工具栏如何保存状态

MFC中的工具栏本来只有一个,如果想增加几个工具栏是比较简单,但现在一个重要的问题是,状态无法保存,导致每次打开,工具栏就会出现问题,要么偏移位置要么显示不出。

经过研究,发现是MFC框架中的一个bug,就是在保存工具栏时,将两个工具栏都保存到注册表的同一个路径中了。

MFC的具体源代码如下:

以下是加载工具栏了,

复制代码
//-----------------------------------------------------
    // Load all toolbars, menubar and docking control bars:
    //-----------------------------------------------------
    for (POSITION posTlb = afxAllToolBars.GetHeadPosition(); posTlb != NULL;)
    {
        CMFCToolBar* pToolBar = (CMFCToolBar*) afxAllToolBars.GetNext(posTlb);
        ENSURE(pToolBar != NULL);

        if (CWnd::FromHandlePermanent(pToolBar->m_hWnd) != NULL)
        {
            ASSERT_VALID(pToolBar);

            if (!m_bLoadSaveFrameBarsOnly || pToolBar->GetTopLevelFrame() == pFrameImpl->m_pFrame)
            {
                if (!pToolBar->IsFloating())
                {
                    pToolBar->LoadState(strSection);
                    if (pToolBar->IsResourceChanged())
                    {
                        bResetImages = TRUE;
                    }
                }
            }
        }
    }

以下是加载工具栏的具体实现

复制代码
BOOL CMFCToolBar::LoadState(LPCTSTR lpszProfileName, int nIndex, UINT uiID)
{
CString strProfileName = ::AFXGetRegPath(AFX_MFC_TOOLBAR_PROFILE, lpszProfileName);
BOOL bResult = FALSE;

if (nIndex == -1)
{
    nIndex = GetDlgCtrlID();
}

CString strSection;
if (uiID == (UINT) -1)
{
    strSection.Format(AFX_REG_SECTION_FMT, (LPCTSTR)strProfileName, nIndex);
}
else
{
    strSection.Format(AFX_REG_SECTION_FMT_EX, (LPCTSTR)strProfileName, nIndex, uiID);
}

LoadState的3个参数其中后两个都是用默认参数,导致最后调用的路径生成的为strSection.Format(AFX_REG_SECTION_FMT, (LPCTSTR)strProfileName, nIndex);

这个值是相同的。最后注册表中就是这样:

加载都是这样,可以想到保存也是类似的。

修改方法,就是在框架加载或保存后,对这两个工具条的状态再次进行加载和保存。

实现的具体代码在APP中,如下:

复制代码
void CMFCApplication8App::LoadCustomState()
{
	CString strSection = GetRegSectionPath();
	CMainFrame* pFrm = (CMainFrame*)AfxGetMainWnd();
	pFrm->m_wndToolBar.LoadState(strSection, -1,
		theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME);
	pFrm->m_wndToolBar1.LoadState(strSection, -1, IDR_TOOLBAR1);
}

void CMFCApplication8App::SaveCustomState()
{
	CString strSection = GetRegSectionPath();

	((CMainFrame*)m_pMainWnd)->m_wndToolBar.SaveState(strSection, -1,
		theApp.m_bHiColorIcons ? IDR_MAINFRAME_256 : IDR_MAINFRAME);
	((CMainFrame*)m_pMainWnd)->m_wndToolBar1.SaveState(strSection, -1, IDR_TOOLBAR1);
}

具体代码见文章绑定的资源

但现在还存在一个问题,就是在视图中没有出现增加的工具栏

我以后研究出来再说。

但这个视图的自定义中有了:

相关推荐
R-G-B18 小时前
【03】MFC入门到精通——MFC 添加控件 设置属性 按钮 文本框
c++·mfc·mfc添加控件
chilavert31819 小时前
技术演进中的开发沉思-28 MFC系列:关于C++
开发语言·c++·mfc
R-G-B4 天前
【MFC】Combobox下拉框中4个选项,运行后点击下拉框选项不能全部展示出来,只能显示2个选项,需要垂直滚动条滚动显示其余选项
c++·mfc
Vitta_U6 天前
MFC的List Control自适应主界面大小
c++·list·mfc
mxpan19 天前
VC++ 与 Golang 的协作:实现 HTTP 文件传输服务
c++·go·mfc
斗转星移7721 天前
MFC中使用CRichEditCtrl控件让文本框中的内容部分加粗
c++·mfc·cricheditctrl·richedit2
lzb_kkk22 天前
【MFC】编辑框、下拉框、列表控件
c语言·开发语言·c++·mfc·1024程序员节
蚂蚁取经22 天前
MFC动态链接库相关知识
c++·mfc
lzb_kkk24 天前
【MFC】树控件的使用详解
开发语言·c++·windows·mfc·1024程序员节
水饺编程1 个月前
MFC 第一章概述
c语言·c++·windows·mfc