我有下面的代碼,但是當我點擊網頁時,它會觸發2-6次。我怎麼才能讓它只發一次?C#webBrowser HTMLDoc.Click觸發多次,爲什麼?
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document != null)
{
htmlDoc = webBrowser1.Document;
htmlDoc.Click += htmlDoc_Click;
//htmlDoc.MouseDown += htmlDoc_MouseDown;
//htmlDoc.MouseMove += htmlDoc_MouseMove;
htmlDoc.ContextMenuShowing += htmlDoc_ContextMenuShowing;
}
}
void htmlDoc_Click(object sender, HtmlElementEventArgs e)
{
Console.WriteLine("Mouse Click");
if (e.CtrlKeyPressed)
{
if (webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).InnerHtml != null)
{
//MessageBox.Show(webBrowser1.Document.GetElementFromPoint(webBrowser1.PointToClient(MousePosition)).InnerText.ToString());
Debug.WriteLine(webBrowser1.Document.GetElementFromPoint(e.ClientMousePosition).InnerHtml);
}
e.ReturnValue = false;
return;
}
// stop mouse events moving on to the HTML doc return false
e.ReturnValue = true;
}
每次文檔加載時,您都會意識到您將註冊點擊事件......因此可能會創建您正在描述的內容。 – 2010-11-17 19:41:10
是你的頁面被加載到WebBrowser控件坐在窗體上? – dexter 2010-11-17 19:42:34
@ Max是的。 @Aaron謝謝我認爲你是正確的,但我不知道如何使它只在最後一個documentComplete上添加事件處理程序? – Adrian 2010-11-17 19:52:50