簡體   English   中英

如何在單擊鼠標左鍵時顯示WebBrowser控件上下文菜單

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

WebBrowser控件具有ContextMenuStrip屬性,可以將其設置為上下文菜單。 但是右鍵單擊會顯示此菜單,如何單擊鼠標左鍵顯示該菜單? 沒有Click的WebBrowser控件事件和MousePositionWebBrowser.Document點擊事件不是精確的。 看來,這取決於鼠標懸停在哪個元素上,以及瀏覽器滾動是否未正確顯示。

您可以將處理程序分配給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);
}

這是給您的一些代碼。 您正在尋找的東西可以通過事件處理程序來實現。 如果您需要幫助,請在評論中提問。

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