日期:2014-05-18  浏览次数:21097 次

c# webBrowser GetElementFromPoint()的问题
C# code
public void Document_MouseDown(object sender, HtmlElementEventArgs e)
        {
                HtmlElement he = doc.GetElementFromPoint(e.MousePosition);//he is null
                MessageBox.Show(he.Name);//he 是空的为什么 
        }


------解决方案--------------------
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.MouseDown += new HtmlElementEventHandler(Document_MouseDown); 
}

void Document_MouseDown(object sender, HtmlElementEventArgs e)
{
Console.WriteLine(e.MouseButtonsPressed);
if (e.MouseButtonsPressed == MouseButtons.Right)
{
Console.WriteLine(e.MousePosition);
}
else if (e.MouseButtonsPressed == MouseButtons.Left)
{

Console.WriteLine(e.MousePosition);
HtmlElement ele = webBrowser1.Document.GetElementFromPoint(e.MousePosition);
if (ele!= null)
{
Console.WriteLine(ele.GetAttribute("href"));
}
}
}