C# winform调用html的内容

这几天领导安排要做一个项目。计划先在C#窗体应用中打开一个html的页面,我本身是做java,基本上这个案例是0基础做,也许有朋友也会碰到这样的问题。我把我这个实现过程一步一步的记录下来。希望会有帮助。

最终效果图如下:

C# winform调用html的内容
第一步:新建一个C# 窗体应用

C# winform调用html的内容
第二步:将WebBrowser控件拉入窗体,并双击页面,打开代码

C# winform调用html的内容
这时的代码如下图:
C# winform调用html的内容

改后的代码如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace C_shap打开网页内容
{
 //在被调用方法类上加上[ComVisible(true)]标签,意思就是当前类可以com组件的形式供外包调用         [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public partial class Form1 : Form
    {
        //在他的构造方法里写代码
    public Form1()
        {
            InitializeComponent();
           //这里写了三种打开方式   
           //第一种:打开项目路径中的htm文件
          string pathName = "";// Application.StartupPath + "\\" + "HTMLPage2.htm";
           //第二种:打开本地的htl文件
             pathName = "D:\\HTMLPage2.htm";
           //第三种:打开你将要访问的网址的绝对地址
            this.webBrowser1.ObjectForScripting = this;
            webBrowser1.Navigate(pathName);
        }
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
        }
    }
}