System TipThis article applies to a different operating system than the one you are using. Article content that may not be relevant to you is disabled.
This article was previously published under Q236312
Create a default CHtmlView Appwizard application. When you
right-click on any Web site in Microsoft Internet Explorer, a default pop-up
menu appears. Driller (MFC) Sample from Microsoft shows how to override this
behavior for the WebBrowser Control so that pop-up menu does not appear. You
might disable this pop-up menu if you do not want users to view the Web page's
source or properties, or use other features such as the Back and Forward
buttons. Or, if you want to add your own functionality to the pop-up menu, you
must also disable the default pop-up menu.
Note Driller (MFC) Sample shows how to override this behavior only for
the WebBrowser Control. It does not use the CHtmlView class.
Driller includes the code used here for Custsite.h, Custsite.cpp,
Idispimp.h, and Idispimp.cpp. These methods are supported in Internet Explorer
4 and Internet Explorer 5.
Add a new CPP file called "Idispimp.cpp" and add the
following code to it:
/*
* idispimp.CPP
* IDispatch for Extending Dynamic HTML Object Model
*
* Copyright (c)1995-1999 Microsoft Corporation, All Rights Reserved
*/
#include "stdafx.h"
#include "idispimp.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// Hardcoded information for extending the Object Model
// Typically this would be supplied through a TypeInfo
// In this case the name "xxyyzz" maps to DISPID_Extend
const WCHAR pszExtend[10]=L"xxyyzz";
#define DISPID_Extend 12345
/*
* CImpIDispatch::CImpIDispatch
* CImpIDispatch::~CImpIDispatch
*
* Parameters (Constructor):
* pSite PCSite of the site we're in.
* pUnkOuter LPUNKNOWN to which we delegate.
*/
CImpIDispatch::CImpIDispatch( void )
{
m_cRef = 0;
}
CImpIDispatch::~CImpIDispatch( void )
{
ASSERT( m_cRef == 0 );
}
/*
* CImpIDispatch::QueryInterface
* CImpIDispatch::AddRef
* CImpIDispatch::Release
*
* Purpose:
* IUnknown members for CImpIDispatch object.
*/
STDMETHODIMP CImpIDispatch::QueryInterface( REFIID riid, void **ppv )
{
*ppv = NULL;
if ( IID_IDispatch == riid )
{
*ppv = this;
}
if ( NULL != *ppv )
{
((LPUNKNOWN)*ppv)->AddRef();
return NOERROR;
}
return E_NOINTERFACE;
}
STDMETHODIMP_(ULONG) CImpIDispatch::AddRef(void)
{
return ++m_cRef;
}
STDMETHODIMP_(ULONG) CImpIDispatch::Release(void)
{
return --m_cRef;
}
//IDispatch
STDMETHODIMP CImpIDispatch::GetTypeInfoCount(UINT* /*pctinfo*/)
{
return E_NOTIMPL;
}
STDMETHODIMP CImpIDispatch::GetTypeInfo(/* [in] */ UINT /*iTInfo*/,
/* [in] */ LCID /*lcid*/,
/* [out] */ ITypeInfo** /*ppTInfo*/)
{
return E_NOTIMPL;
}
STDMETHODIMP CImpIDispatch::GetIDsOfNames(
/* [in] */ REFIID riid,
/* [size_is][in] */ OLECHAR** rgszNames,
/* [in] */ UINT cNames,
/* [in] */ LCID lcid,
/* [size_is][out] */ DISPID* rgDispId)
{
HRESULT hr;
UINT i;
// Assume some degree of success
hr = NOERROR;
// Hardcoded mapping for this sample
// A more usual procedure would be to use a TypeInfo
for ( i=0; i < cNames; i++)
{
if ( 2 == CompareString( lcid, NORM_IGNOREWIDTH, (char*)pszExtend, 3, (char*)rgszNames[i], 3 ) )
{
rgDispId[i] = DISPID_Extend;
}
else
{
// One or more are unknown so set the return code accordingly
hr = ResultFromScode(DISP_E_UNKNOWNNAME);
rgDispId[i] = DISPID_UNKNOWN;
}
}
return hr;
}
STDMETHODIMP CImpIDispatch::Invoke(
/* [in] */ DISPID dispIdMember,
/* [in] */ REFIID /*riid*/,
/* [in] */ LCID /*lcid*/,
/* [in] */ WORD wFlags,
/* [out][in] */ DISPPARAMS* pDispParams,
/* [out] */ VARIANT* pVarResult,
/* [out] */ EXCEPINFO* /*pExcepInfo*/,
/* [out] */ UINT* puArgErr)
{
// For this sample we only support a Property Get on DISPID_Extend
// returning a BSTR with "Wibble" as the value
if ( dispIdMember == DISPID_Extend )
{
if ( wFlags & DISPATCH_PROPERTYGET )
{
if ( pVarResult != NULL )
{
WCHAR buff[10]=L"Wibble";
BSTR bstrRet = SysAllocString( buff );
VariantInit(pVarResult);
V_VT(pVarResult)=VT_BSTR;
V_BSTR(pVarResult) = bstrRet;
}
}
}
return S_OK;
}
Open MySample.cpp and, in the InitInstance of CMySample,
add the following code. Also comment out the call to
AfxEnableControlContainer():
BOOL CMySampleApp::InitInstance()
{
CCustomOccManager *pMgr = new CCustomOccManager;
// Create an IDispatch class for extending the Dynamic HTML Object Model
m_pDispOM = new CImpIDispatch;
// Set our control containment up but using our control container
// management class instead of MFC's default
AfxEnableControlContainer(pMgr);
// AfxEnableControlContainer();
//... rest of the code here
}
Also add the following to the list of include files to
MySample.cpp:
Go to MySample.h and add the following statement at the
bottom of the file.
extern CMySampleApp theApp;
Now go to the ClassView tab and override the Create virtual function for CMySampleView.
Replace the function body with the code shown below, so the function should
look like:
BOOL CMySampleView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// create the view window itself
m_pCreateContext = pContext;
if (!CView::Create(lpszClassName, lpszWindowName,
dwStyle, rect, pParentWnd, nID, pContext))
{
return FALSE;
}
RECT rectClient;
GetClientRect(&rectClient);
// create the control window
// AFX_IDW_PANE_FIRST is a safe but arbitrary ID
if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName,
WS_VISIBLE | WS_CHILD, rectClient, this, AFX_IDW_PANE_FIRST))
{
DestroyWindow();
return FALSE;
}
LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);
if (!SUCCEEDED(hr))
{
m_pBrowserApp = NULL;
m_wndBrowser.DestroyWindow();
DestroyWindow();
return FALSE;
}
return TRUE;
}
Rebuild and run the application. When you right-click, the
default pop-up menu will not appear. This is because in the
CCustomControlSite::XDocHostUIHandler::ShowContextMenu method, the procedure
simply returns an S_OK so that Mshtml.dll no longer tries to display its own
pop-up menu. At this point, you could also add your own pop-up menu to replace
the default pop-up menu.
Note If future MFC versions change the implementation of
COleControlSite or COccManager, this technique to disable the pop-up menu might
not work. If you are using this technique to design your code, please be
advised that you may have to change your code in the future.