MFC修改AfxMessageBox的标题

[MFC编程]如何修改AfxMessageBox的标题? 

应用程序的exe的名称可以随意更改,但是如果程序内部弹出一个系统对话框,这时候就会发现对话框的标题和应用程序的名称不一致了。怎么办呢?
系统内部弹出的对话框一般调用的是AfxMessageBox()函数,那就检查一下这个函数吧。一路跟下去,最后发现到了CWinApp类中。
int CWinApp::ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
// disable windows for modal dialog
DoEnableModeless(FALSE);
HWND hWndTop;
HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop);
// re-enable the parent window, so that focus is restored
// correctly when the dialog is dismissed.
if (hWnd != hWndTop)
EnableWindow(hWnd, TRUE);
// set help context if possible
DWORD* pdwContext = NULL;
DWORD dwWndPid=0;
GetWindowThreadProcessId(hWnd,&dwWndPid);
if (hWnd != NULL && dwWndPid==GetCurrentProcessId() )
{
// use app-level context or frame level context
LRESULT lResult = ::SendMessage(hWnd, WM_HELPPROMPTADDR, 0, 0);
if (lResult != 0)
pdwContext = (DWORD*)lResult;
}
// for backward compatibility use app context if possible
if (pdwContext == NULL && pApp != NULL)
pdwContext = &pApp->m_dwPromptContext;
DWORD dwOldPromptContext = 0;
if (pdwContext != NULL)
{
// save old prompt context for restoration later
dwOldPromptContext = *pdwContext;
if (nIDPrompt != 0)
*pdwContext = HID_BASE_PROMPT+nIDPrompt;
}
// determine icon based on type specified
if ((nType & MB_ICONMASK) == 0)
{
switch (nType & MB_TYPEMASK)
{
case MB_OK:
case MB_OKCANCEL:
nType |= MB_ICONEXCLAMATION;
break;
case MB_YESNO:
case MB_YESNOCANCEL:
nType |= MB_ICONQUESTION;
break;
case MB_ABORTRETRYIGNORE:
case MB_RETRYCANCEL:
// No default icon for these types, since they are rarely used.
// The caller should specify the icon.
break;
}
}
#ifdef _DEBUG
if ((nType & MB_ICONMASK) == 0)
TRACE(traceAppMsg, 0, "Warning: no icon specified for message box.\n");
#endif
TCHAR szAppName[_MAX_PATH];
szAppName[0] = '\0';
LPCTSTR pszAppName;
if (pApp != NULL)
pszAppName = pApp->m_pszAppName;
else
{
pszAppName = szAppName;
DWORD dwLen = GetModuleFileName(NULL, szAppName, _MAX_PATH);
if (dwLen == _MAX_PATH)
szAppName[_MAX_PATH - 1] = '\0';
}
int nResult =
::AfxCtxMessageBox(hWnd, lpszPrompt, pszAppName, nType);
// restore prompt context if possible
if (pdwContext != NULL)
*pdwContext = dwOldPromptContext;
// re-enable windows
if (hWndTop != NULL)
::EnableWindow(hWndTop, TRUE);
DoEnableModeless(TRUE);
return nResult;
}
红字的地方是关键,最终 pszAppName 的值会指向当前应用程序的一个字符串 m_pszAppName ,这个值是开放的可以被动态修改的。如此一来,在应用程序初始化之前修改 m_pszAppName 这个值就会更改AfxMessageBox的标题了。 那这个 m_pszAppName 的值怎么修改呢?很简单。
WCHAR* szAppName = new WCHAR[MAX_PATH];
lstrcpy(szAppName , L"TargetTitle");
AfxGetApp()->m_pszAppName = szAppName;


转载自:http://blog.163.com/lvan100@yeah/blog/static/68117214201182054059645/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值