首先要添加如下引用:
WindowsFormsIntegration
System.Drawing
System.Windows.Forms
然后在xaml中添加引用
xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
winform控件需要包含在WindowsFormsHost才能使用,所以webbroser在xaml用法为
<wfi:WindowsFormsHost x:Name="c_wfh">
<winform:WebBrowser x:Name="c_webBrowser">
</winform:WebBrowser>
</wfi:WindowsFormsHost>
然后给webbrowser添加载入完成的事件响应,在载入完成的时候为document添加鼠标点击的事件响应
//添加事件响应代码
c_webBrowser.DocumentCompleted += c_webBrowser_DocumentCompleted;
//事件响应函数,在其中添加了Document.Click方法
void c_webBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
c_webBrowser.Document.Click += new System.Windows.Forms.HtmlElementEventHandler(Document_Click);
}
//响应Document.Click方法,将属性值显示在TextBox中
void Document_Click(object sender, System.Windows.Forms.HtmlElementEventArgs e)
{
if (c_webBrowser.Document != null)
{
System.Windows.Forms.HtmlElement elem = c_webBrowser.Document.GetElementFromPoint(e.ClientMousePosition);
c_text.Text = elem.GetAttribute("id").ToString();
}
}