[MFC编程]如何修改AfxMessageBox的标题?
应用程序的exe的名称可以随意更改,但是如果程序内部弹出一个系统对话框,这时候就会发现对话框的标题和应用程序的名称不一致了。怎么办呢?
系统内部弹出的对话框一般调用的是AfxMessageBox()函数,那就检查一下这个函数吧。一路跟下去,最后发现到了CWinApp类中。
pszAppName
的值会指向当前应用程序的一个字符串
m_pszAppName
,这个值是开放的可以被动态修改的。如此一来,在应用程序初始化之前修改
m_pszAppName
这个值就会更改AfxMessageBox的标题了。 那这个
m_pszAppName
的值怎么修改呢?很简单。
系统内部弹出的对话框一般调用的是AfxMessageBox()函数,那就检查一下这个函数吧。一路跟下去,最后发现到了CWinApp类中。
int CWinApp::ShowAppMessageBox(CWinApp *pApp, LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
红字的地方是关键,最终{// disable windows for modal dialogDoEnableModeless(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 possibleDWORD* pdwContext = NULL;DWORD dwWndPid=0;GetWindowThreadProcessId(hWnd,&dwWndPid);if (hWnd != NULL && dwWndPid==GetCurrentProcessId() ){// use app-level context or frame level contextLRESULT lResult = ::SendMessage(hWnd, WM_HELPPROMPTADDR, 0, 0);if (lResult != 0)pdwContext = (DWORD*)lResult;}// for backward compatibility use app context if possibleif (pdwContext == NULL && pApp != NULL)pdwContext = &pApp->m_dwPromptContext;DWORD dwOldPromptContext = 0;if (pdwContext != NULL){// save old prompt context for restoration laterdwOldPromptContext = *pdwContext;if (nIDPrompt != 0)*pdwContext = HID_BASE_PROMPT+nIDPrompt;}// determine icon based on type specifiedif ((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 _DEBUGif ((nType & MB_ICONMASK) == 0)TRACE(traceAppMsg, 0, "Warning: no icon specified for message box.\n");#endifTCHAR 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 possibleif (pdwContext != NULL)*pdwContext = dwOldPromptContext;// re-enable windowsif (hWndTop != NULL)::EnableWindow(hWndTop, TRUE);DoEnableModeless(TRUE);return nResult;}
WCHAR* szAppName = new WCHAR[MAX_PATH];
lstrcpy(szAppName , L"TargetTitle");
AfxGetApp()->m_pszAppName = szAppName;
转载自:http://blog.163.com/lvan100@yeah/blog/static/68117214201182054059645/