现在的位置: 首页 > 综合 > 正文

MFC修改AfxMessageBox的标题

2013年11月26日 ⁄ 综合 ⁄ 共 2328字 ⁄ 字号 评论关闭

[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/

抱歉!评论已关闭.