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

注意不要调用父类的函数

最后效果如下图:

源代码见文章绑定的资源

相关推荐
希望_睿智22 分钟前
实战设计模式之中介者模式
c++·设计模式·架构
博语小屋2 小时前
转义字符.
c语言·c++
Lhan.zzZ2 小时前
Qt跨线程网络通信:QSocketNotifier警告及解决
开发语言·c++·qt
Aevget2 小时前
QtitanDocking 如何重塑制造业桌面应用?多视图协同与专业界面布局实践
c++·qt·界面控件·ui开发·qtitandocking
-森屿安年-2 小时前
STL中 Map 和 Set 的模拟实现
开发语言·c++
历程里程碑2 小时前
双指针巧解LeetCode接雨水难题
java·开发语言·数据结构·c++·python·flask·排序算法
ALex_zry2 小时前
C++ 中多继承与虚函数表的内存布局解析
java·开发语言·c++
w-w0w-w2 小时前
C++构造函数与析构函数详解
c++
saber_andlibert3 小时前
【C++转GO】初阶知识
开发语言·c++·golang
Tandy12356_3 小时前
手写TCP/IP协议栈——实现ping响应不可达
c语言·网络·c++·网络协议·tcp/ip·计算机网络