简体   繁体   English

如何在单击鼠标左键时显示WebBrowser控件上下文菜单

[英]How to show WebBrowser control context menu on left-click

WebBrowser control has a ContextMenuStrip property that can be set to a context menu. WebBrowser控件具有ContextMenuStrip属性,可以将其设置为上下文菜单。 But this menu appears by right-click, how can I show it by left-click? 但是右键单击会显示此菜单,如何单击鼠标左键显示该菜单? There is no Click event for WebBrowser control and the MousePosition of WebBrowser.Document click event is not precise. 没有Click的WebBrowser控件事件和MousePositionWebBrowser.Document点击事件不是精确的。 It seems it depends on the element the mouse is over and also if the browser scrolls isn't shown in right place. 看来,这取决于鼠标悬停在哪个元素上,以及浏览器滚动是否未正确显示。

You can assign a handler to Click event or other mouse events of Document and show the context menu at Cursor.Position . 您可以将处理程序分配给Click事件或Document其他鼠标事件,并在Cursor.Position显示上下文菜单。

You can also prevent the default click action e.ReturnValue = false; 您还可以阻止默认的点击操作e.ReturnValue = false; .

private void webBrowser1_DocumentCompleted(object sender,
                                           WebBrowserDocumentCompletedEventArgs e)
{
    this.webBrowser1.Document.Click += Document_Click;
}

void Document_Click(object sender, HtmlElementEventArgs e)
{
    //To prevent the default click action you can uncomment next line:
    //e.ReturnValue = false;

    this.contextMenuStrip1.Show(Cursor.Position);
}

Here is some code for you. 这是给您的一些代码。 What you are looking for is doable with an event handler. 您正在寻找的东西可以通过事件处理程序来实现。 If you need help please ask in comments. 如果您需要帮助,请在评论中提问。

this._browser.DocumentCompleted+=new WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
...
private void browser_DocumentCompleted(Object sender, WebBrowserDocumentCompletedEventArgs e)
{
    this._browser.Document.Body.MouseDown += new HtmlElementEventHandler(Body_MouseDown);
}
...
void Body_MouseDown(Object sender, HtmlElementEventArgs e)
{
    switch(e.MouseButtonsPressed)
    {
    case MouseButtons.Left:
        //your code
    break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM