BUG: AfxMessageBox Help Fails to Bring Up Proper TopicID: Q229964
|
When F1 is pressed to invoke context sensitive help in response to a call to AfxMessageBox(), the default help topic is brought up instead of the help topic associated with the call to AfxMessageBox(). This problem may be seen when MFC42.DLL is updated with a version greater than or equal to 6.0.8168.0.
The implementation of context sensitive help for AfxMessageBox() was changed for Visual C++ version 6.0. A private message WM_HELPPROMPTADDR is sent to retrieve the address of m_dwPromptContext from the associated frame window. MFC uses an internal helper function CWnd::GetSafeOwner_(), to determine which window to send this message to. MFC uses the value returned by the pWndTop parameter of CWnd::GetSafeOwner_() for the target window handle. The problem is that in most scenarios this value is NULL. The NULL value causes the call ::SendMessage(hWndTop, WM_HELPPROMPTADDR, 0, 0) to have no affect.
One workaround for this problem is to override the virtual function CWinApp::DoMessageBox(), and copy and paste the base class implementation. Next, change the SendMessage call to use the HWND returned from AfxGetMainWnd()->GetSafeHwnd(), for example:
#include "afxpriv.h"
...
int CHelpApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
// disable windows for modal dialog
EnableModeless(FALSE);
HWND hWndTop;
HWND hWnd = CWnd::GetSafeOwner_(NULL, &hWndTop);
// set help context if possible
DWORD* pdwContext = NULL;
HWND hWnd2 = AfxGetMainWnd()->GetSafeHwnd();
if (hWnd2 != NULL)
{
// use app-level context or frame level context
LRESULT lResult = ::SendMessage(hWnd2, WM_HELPPROMPTADDR, 0, 0); // Use "MainWnd" HWND
if (lResult != 0)
pdwContext = (DWORD*)lResult;
}
// for backward compatibility use app context if possible
if (pdwContext == NULL && this != NULL)
pdwContext = &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_ICONEXCLAMATION;
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)
TRACE0("Warning: no icon specified for message box.\n");
#endif
TCHAR szAppName[_MAX_PATH];
LPCTSTR pszAppName;
if (this != NULL)
pszAppName = m_pszAppName;
else
{
pszAppName = szAppName;
GetModuleFileName(NULL, szAppName, _MAX_PATH);
}
int nResult =
::MessageBox(hWnd, lpszPrompt, pszAppName, nType);
// restore prompt context if possible
if (pdwContext != NULL)
*pdwContext = dwOldPromptContext;
// re-enable windows
if (hWndTop != NULL)
::EnableWindow(hWndTop, TRUE);
EnableModeless(TRUE);
return nResult;
}
Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article.
© Microsoft Corporation May 25, 1999, All Rights Reserved.
Contributions by Mike Francis, Microsoft Corporation
CWnd::GetSafeOwner, AfxMessageBox
Additional query words:
Keywords : kbCSHelp kbMFC kbVC600bug kbDSupport kbGrpMFCATL
Version : winnt:6.0
Platform : winnt
Issue type : kbbug
Last Reviewed: June 9, 1999