编码实现修改应用程序标题

本文介绍如何在MFC应用程序中自定义对话框及消息框的标题,通过修改CWinApp的m_pszAppName成员变量实现统一标题更改。提供两种方法:一是在构造函数中直接修改;二是重写DoMessageBox函数临时改变标题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.问题背景

   使用afxmessagebox弹出信息框,此函数没有设置标题的参数,默认标题为创建的项目名称。

   注:因使用的环境不允许使用messagebox.so....寻找之路。。。


2.初识 m_pszAppName

  MSDN: The application name can come from the parameter passed to the CWinApp constructor, or, if not specified, to the resource string with the ID ofAFX_IDS_APP_TITLE. If the application name is not found in the resource, it comes from the program's .EXE filename.

  根据MSDN的描述,我们只要改掉这个值就可以了,如何修改。

3.实践操作

  我想实现在CDlg_Test.cpp中所有的标题都设为“网络安全”。

  a)构造函数时修改

       //First free the string allocated by MFC at CWinApp startup.
      //The string is allocated before InitInstance is called.

       free((void*)AfxGetApp()->m_pszAppName);

       //Change the name of the application file.
      //The CWinApp destructor will free the memory.

       AfxGetApp()->m_pszAppName = _tcsdup("网络安全");

       ps:此时使用afxmessage(),标题就变为改之后的。

  b)重写DoMessageBox--此种方式笔者没有亲自实验:)

    int CEx0811aApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt) 
    {
          LPCTSTR pOldAppName = m_pszAppName;
          m_pszAppName = "新标题";// 其实afxmessgebox的标题就是这个变量决定的
          int iRet = CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
          m_pszAppName = pOldAppName ;
          return iRet ;
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值