关于 m_pszAppName

  本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/renjwjx/archive/2009/04/14/4073076.aspx

 

关于如何修改AfxMessageBox的标题,不看后悔的 收藏
刚开始一筹莫展,后来从网上发现有好几种方式。但是这些方法有错误的。

所以,网上的东西,我们也不能一股脑的接受。

方法一:

一,新建一个基于对话框的工程Ex0811a。
二,增加一个按钮,其响应函数为:
void CEx0811aDlg::OnButton1()
{
         AfxMessageBox("欢迎你!");
}
三,利用类向导重写CEx0811aApp::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 ;
}

方法二:

如果在线程里面跑,那么直接修改AfxGetApp()->m_pszAppName

当然这种方法没有保存原来的名字 

上面的程序乍一看没什么问题,但是,运行起来有时会有问题。

查看msdn,发现上面有一句容易忽略的话。看来大家都忽略了。我也是程序出错才思考的。

If you assign a value to m_pszAppName, it must be dynamically allocated on the heap. The CWinApp destructor calls free( ) with this pointer. You many want to use the _tcsdup( ) run-time library function to do the allocating. Also, free the memory associated with the current pointer before assigning a new value. For example:

Example:

//First free the string allocated by MFC at CWinApp startup.
//The string is allocated before InitInstance is called.
free((void*)m_pszAppName);
//Change the name of the application file.
//The CWinApp destructor will free the memory.
m_pszAppName=_tcsdup(_T("d:\\somedir\\myapp.exe"));

上面的意思是说,不能直接赋值,应该要先free(m_pszAppName),然后申请空间,然后赋值。

注:修改对话框的标题直接

setwindowtext("新标题");

 


 

posted @ 2009-12-02 15:37  ~笑  阅读(678)  评论(0编辑  收藏  举报