我有WebBrowser控件,我想在单击右键时显示上下文菜单以获取该上下文菜单的句柄。
可能吗?
答案 0 :(得分:1)
是
您可以参考以下代码。
//this code assumes WebBrowser object(_webBrowser) is already initiated
//in class scope.
//this method is needed to execute when form is loaded.
//Register it to load event
private void Loaded(object sender, RoutedEventArgs e)
{
_webBrowser.LoadCompleted += _webBrowser_LoadCompleted;
}
private HTMLDocumentEvents2_Event _docEvent;
private void _webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
if (_docEvent != null)
{
_docEvent.oncontextmenu -= new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
}
if (_webBrowser.Document != null)
{
_docEvent = (HTMLDocumentEvents2_Event)_webBrowser.Document;
_docEvent.oncontextmenu += new HTMLDocumentEvents2_oncontextmenuEventHandler(_docEvent_oncontextmenu);
}
}
bool _docEvent_oncontextmenu(IHTMLEventObj pEvtObj)
{
//do something and determine you want whether context menu shows or not
//if you want to shows context menu, you'll need to return true.
return true;
}
答案 1 :(得分:1)
如果只想显示自己的contextMenu。我在这里发布了一个适用于winforms WebBrowser控件的解决方案:
How do you override the ContextMenu that appears when right clicking on winforms WebBrowser Control?