【MFC】如何修改多文档视图的标签

新建工程同之前的几篇博客

新建一个调用菜单,并实现其内容

以下代码演示创建时设置标题,并保存到子框架中

复制代码
#include "MFCApplication9Doc.h"
#include "MFCApplication9View.h"
void CMainFrame::On32771()
{
	CMFCApplication9Doc* pDoc;
	POSITION pos = theApp.m_pDocTemplate->GetFirstDocPosition();
	int i = 1;
	while (pos != NULL)
	{
		pDoc = (CMFCApplication9Doc*)(theApp.m_pDocTemplate->GetNextDoc(pos));
		if (pDoc == NULL) break;

		POSITION pos1 = pDoc->GetFirstViewPosition();
		if (pos1 != NULL)
		{
			while (TRUE)
			{
				CMFCApplication9View* pView = (CMFCApplication9View*)pDoc->GetNextView(pos1);
				if(pView==NULL) break;
				CFrameWnd* pFrame = pView->GetParentFrame();
				CString strTitle;
				strTitle.Format(_T("第%d个页面"), i++);
				pFrame->SetWindowText(strTitle);
				pFrame->SetTitle(strTitle);
			}
		}
	}
}

为了在切换窗口时保持标题,需要修改子框架的以下函数

复制代码
void CChildFrame::OnUpdateFrameTitle(BOOL bAddToTitle)
{
	CString str = GetTitle();
	SetWindowText(str);

	// CMDIChildWndEx::OnUpdateFrameTitle(bAddToTitle);
}

注意不要调用父类的函数

最后效果如下图:

源代码见文章绑定的资源

相关推荐
anew___4 小时前
蜂鸣器播放音乐——让 Arduino 唱出旋律
c++·stm32·单片机·嵌入式硬件·c
stolentime6 小时前
洛谷P10515 转圈题解
c++·算法·数学建模·贪心算法
tdtsmt7 小时前
穿戴硬件量产工艺实录:天地通电子智能手表主板 SMT 贴片案
c++
m0_7345717610 小时前
深入理解C++ 析构函数<一>基本概念
开发语言·c++
t-think11 小时前
C++ string 类(上)—— string类初识与常用接口详解
开发语言·c++
cpp_learners11 小时前
C++ 实现责任链模式(Chain of Responsibility):从一堆 if-else 到可插拔的处理管道
开发语言·c++·责任链模式
郝学胜_神的一滴11 小时前
C++11 工程级应用 12:编译期类型魔法,干掉重复与臃肿的代码
c++·visual studio
David猪大卫13 小时前
【C++修炼】哈希表的实现
数据结构·c++·笔记·学习·算法·哈希算法·散列表
广州山泉婚姻13 小时前
列表初始化:C++11全新初始化体系
c++·人工智能
莫生灬灬13 小时前
灵码 LingBuilder:用中文写代码,确定性生成真实 C++ 工程(开源)
c++·开源·mfc