zoukankan      html  css  js  c++  java
  • WPF 使用winform的webbrowser

    首先要添加如下引用:

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

  • 相关阅读:
    用Visual C#实现文件下载
    解读C#中的规则表达式
    NET(C#)连接各类数据库集锦
    C#中编写多线程(1)——起步
    C#中的常用加密算法与其它语言的兼容性
    C#的事件处理机制应用
    TCP IP协议之通信详解
    手把手教你AspNetCore WebApi:认证与授权
    TCP IP协议之初识
    mysql中exit和in的区别
  • 原文地址:https://www.cnblogs.com/ExMan/p/3654419.html
Copyright © 2011-2022 走看看