Article ID: 192501
Article Last Modified on 5/11/2006
Class ID Default Interface Default Event Interface ----------------- ----------------- ----------------------- CLSID_WebBrowser IWebBrowser2 DWebBrowserEvents2 CLSID_WebBrowser_V1 IWebBrowser DWebBrowserEvents
BOOL CMyApp::PreTranslateMessage(MSG* pMsg)
{
if ( WM_RBUTTONDOWN == pMsg->message
|| WM_RBUTTONDBLCLK == pMsg->message )
{
// Received right-click (dbl)
CWnd* pFrame = AfxGetMainWnd();
CMyView* pView =
((CMyView*)((CFrameWnd*)pFrame)->GetActiveView());
if ( pView->IsWebBrowser( pMsg->hwnd ) )
{
// The WebBrowser control was clicked
pView->DoContextMenu();
return TRUE;
}
}
return CWinApp::PreTranslateMessage(pMsg);
}
Use this code in the MFC view class that hosts the WebBrowser control:
void CMyView::DoContextMenu()
{
CMenu Menu;
CMenu* pPopup = NULL;
POINT pt;
DWORD pos = GetMessagePos();
pt.x = LOWORD(pos);
pt.y = HIWORD(pos);
Menu.LoadMenu( IDR_MAINFRAME ); // Your menu here.
pPopup = Menu.GetSubMenu( 0 );
if ( pPopup )
{
pPopup->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON,
pt.x, pt.y, this );
pPopup->DestroyMenu();
Menu.DestroyMenu();
}
}
static TCHAR gszIEClass[] = "Internet Explorer_Server";
// Checks if message window is WebBrowser control.
// Also works with nested frames.
BOOL CMyView::IsWebBrowser(HWND hWnd)
{
HWND hWndWBOC = (HWND)m_Browser.GetSafeHwnd();
HWND hWndParent;
hWndParent = GetParentWBOC( hWnd );
while ( hWndParent != hWndWBOC
&& hWndParent != NULL )
{
hWndParent = GetParentWBOC( ::GetParent( hWndParent ) );
}
return (hWndParent != NULL);
}
HWND CMyView::GetParentWBOC(HWND hWnd)
{
TCHAR szClass[sizeof(gszIEClass)+1];
HWND hWndParent;
::GetClassName( hWnd, szClass, sizeof(szClass) );
if ( 0 == _tcscmp( szClass, gszIEClass ) )
{
// InternetExplorer_Server
hWndParent = ::GetParent( hWnd ); // Shell DocObject View
return ::GetParent( hWndParent ); // Shell Embedding
}
else
{
return NULL;
}
}
Keywords: kbbug kbfix kbwebbrowser kbie500fix kbie500dp1fix kbcode KB192501