在MFC中,可以使用Adobe DISPATCH_METHOD
来禁用导航栏、书签和右键单击等功能。下面是一个使用Adobe DISPATCH_METHOD
的代码示例:
// MyWebBrowser.h
class CMyWebBrowser : public CWebBrowser2
{
public:
DECLARE_DYNCREATE(CMyWebBrowser)
protected:
CMyWebBrowser(); // protected constructor used by dynamic creation
public:
virtual ~CMyWebBrowser();
protected:
DECLARE_DISPATCH_MAP()
afx_msg void OnPropertyChange(LPCTSTR lpszProperty);
afx_msg BOOL OnAmbientProperty(COleControlSite* pSite, DISPID dispid, VARIANT* pvar);
DECLARE_MESSAGE_MAP()
};
// MyWebBrowser.cpp
IMPLEMENT_DYNCREATE(CMyWebBrowser, CWebBrowser2)
BEGIN_DISPATCH_MAP(CMyWebBrowser, CWebBrowser2)
DISP_FUNCTION_ID(CMyWebBrowser, "OnPropertyChange", 0x00000001, OnPropertyChange, VT_EMPTY, VTS_BSTR)
END_DISPATCH_MAP()
BEGIN_MESSAGE_MAP(CMyWebBrowser, CWebBrowser2)
ON_CONTROL_REFLECT(DISPID_UNKNOWN, &CMyWebBrowser::OnAmbientProperty)
END_MESSAGE_MAP()
CMyWebBrowser::CMyWebBrowser()
{
}
CMyWebBrowser::~CMyWebBrowser()
{
}
void CMyWebBrowser::OnPropertyChange(LPCTSTR lpszProperty)
{
// 禁用导航栏、书签和右键单击等功能的代码
if (lstrcmpi(lpszProperty, _T("MenuBar")) == 0 ||
lstrcmpi(lpszProperty, _T("ToolBar")) == 0 ||
lstrcmpi(lpszProperty, _T("StatusBar")) == 0 ||
lstrcmpi(lpszProperty, _T("FullScreen")) == 0 ||
lstrcmpi(lpszProperty, _T("Resizable")) == 0 ||
lstrcmpi(lpszProperty, _T("NavigateBack")) == 0 ||
lstrcmpi(lpszProperty, _T("NavigateForward")) == 0 ||
lstrcmpi(lpszProperty, _T("RightClick")) == 0)
{
SetProperty(DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, VT_BOOL, TRUE);
}
}
BOOL CMyWebBrowser::OnAmbientProperty(COleControlSite* pSite, DISPID dispid, VARIANT* pvar)
{
if (dispid == DISPID_UNKNOWN)
{
// 禁用鼠标右键单击和选择功能的代码
SetProperty(dispid, VT_BOOL, TRUE);
return TRUE;
}
return FALSE;
}
CMyWebBrowser
替代CWebBrowser2
。// MyDialog.h
class CMyDialog : public CDialogEx
{
public:
CMyDialog();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_MYDIALOG };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
CMyWebBrowser m_WebBrowser;
};
// MyDialog.cpp
CMyDialog::CMyDialog() : CDialogEx(IDD_MYDIALOG)
{
}
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_WEB_BROWSER, m_WebBrowser);
}
这样,你就可以使用CMyWebBrowser
来禁用导航栏、书签和右键单击等功能了。注意,你需要将对应的WebBrowser控件的ID设置为IDC_WEB_BROWSER
。