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

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

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

我以后研究出来再说。

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

相关推荐
鸿芯微控科技2 天前
MFC关断后还有流量怎么办?零流量、阀门泄漏、压差与Python分析
c++·python·mfc·质量流量控制器·关断泄漏·零流量测试
鸿芯微控科技3 天前
MFC量程怎么选?最小流量、满量程、调节比与误差计算
c++·python·mfc·流量控制·设备选型·质量流量控制器
TunerT_TQ11 天前
Valhalla 静态工程审阅 #021|华为MindSpore 源码证据驱动评测【大厂开源基础设施特辑】
c++·开源·github·mfc·#大模型开源·#华为开源·#深度学习
蒙奇·D·路飞-12 天前
OpenClaw:开源的《合金弹头》精神续作与终极本地部署指南
c++·开源·mfc
TunerT_TQ12 天前
Valhalla 静态工程审阅 #018|MMCV 源码证据驱动评测【大厂开源基础设施特辑】
c++·开源·mfc·#计算机视觉·#商汤科技·#openmmlab·#静态源码审计
鸿芯微控科技13 天前
MFC质量流量控制器流量不准怎么排查?设定值、反馈值与参考流量对比,附Python代码
c++·python·mfc
鸿芯微控科技13 天前
MFC气体转换系数怎么用?标定气体、换算方向与Python批量计算
c++·python·mfc
小小龙学IT18 天前
C++ Placement New 与显式析构:手动对象生命周期管理的艺术
c++·windows·mfc
蕓晨1 个月前
MFC实现画图菜单小程序
c++·mfc
浩浩测试一下1 个月前
windows 加壳工具代码实现02 - 加壳界面实现MFC
windows·mfc·免杀·加壳器·pe应用