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

时间:2015-12-23 17:28:31

标签: c# winforms webbrowser-control

WebBrowser控件具有ContextMenuStrip属性,可以将其设置为上下文菜单。但右键单击会显示此菜单,如何通过左键单击显示它? WebBrowser控件没有MousePosition事件,WebBrowser.Document点击事件的SELECT IF(IFNULL(fc.counter,0) > 0, IFNULL(fc.counter,0), 0) AS counter , b.*, fc.* FROM client_branche AS cb INNER JOIN branche AS b ON b.id = cb.branche_id LEFT JOIN ( SELECT count(*) AS counter, ctn_b.branche_id AS b_id FROM `historique` AS h INNER JOIN contenu_branche AS ctn_b ON ctn_b.contenu_id = h.contenu_id INNER JOIN utilisateur AS u ON u.id = h.utilisateur_id WHERE h.h_fini = 1 AND ( u.client_id = 1 OR u.client_id = 0 ) AND h.h_dateheure BETWEEN '2015-12-24' AND '2015-12-30' GROUP BY ctn_b.`branche_id` ) AS fc ON fc.b_id = cb.branche_id WHERE cb.client_id = 1 ; 不准确。它似乎取决于鼠标所在的元素,以及浏览器滚动时是否显示在正确的位置。

2 个答案:

答案 0 :(得分:1)

您可以为Click事件或Document的其他鼠标事件分配处理程序,并在Cursor.Position显示上下文菜单。

您还可以阻止默认点击操作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);
}

答案 1 :(得分:0)

以下是一些代码。您正在寻找的是事件处理程序。如果您需要帮助,请在评论中提问。

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;
    }
}