System.Windows.Controls.WebBrowser

WebBrowser js与C#交互

 

 下面是HTML实现。(E:/kzf/C/svn/IvyCloudVideo/src/XYLite/bin/Release/test.html)


<html>
    <head>
        <script>
            function test(message) { alert(message); }
        </script>
    </head>
    <body>
        <button onclick="window.external.Test('called from script code')">call client code from script code</button>
    </body>
</html>

如下所示WPF实现。 

webBrowser.ObjectForScripting = new ScriptEvent();
webBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser_LoadCompleted);

webBrowser.Navigate("E:/kzf/C/svn/IvyCloudVideo/src/XYLite/bin/Release/test.html");

[System.Runtime.InteropServices.ComVisible(true)]
public class ScriptEvent
{
    //供JS调用
    public void Test(string message)
    {
        MessageBox.Show(message);
    }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
    try
    {
        //要在web加载完成后才能调用成功。通过LoadCompleted事件检测。
        //执行当前加载的文档所实现的脚本函数。
        webBrowser.InvokeScript("test", "asb");
    }
    catch (Exception ex)
    {
        Commons.LogHelper.Error("error");
    }
}

 

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace _2 { public partial class 主界面 : Form { public 主界面() { InitializeComponent(); } private void InitializeComponent() { // 主容器和布局 this.splitContainer1 = new System.Windows.Forms.SplitContainer(); this.treeViewNotes = new System.Windows.Forms.TreeView(); this.listViewNotes = new System.Windows.Forms.ListView(); this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader2 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader3 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader4 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.panel1 = new System.Windows.Forms.Panel(); this.lblLastModified = new System.Windows.Forms.Label(); this.lblNoteTitle = new System.Windows.Forms.Label(); this.splitContainer2 = new System.Windows.Forms.SplitContainer(); this.listViewVersions = new System.Windows.Forms.ListView(); this.columnHeader5 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader6 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.columnHeader7 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.文件ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.上传文件ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.打开ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.退出登录ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.注销账号ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.编辑ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.修改ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.修改密码ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.删除ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.笔记ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.上传文件ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.修改ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.删除ToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); this.分类ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.版本历史ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.imageList1 = new System.Windows.Forms.ImageList(); // 设置控件属性 this.SuspendLayout(); // // splitContainer1 // this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer1.Location = new System.Drawing.Point(0, 24); this.splitContainer1.Name = "splitContainer1"; // // splitContainer1.Panel1 (左侧面板) // this.splitContainer1.Panel1.Controls.Add(this.treeViewNotes); this.splitContainer1.Panel1.Controls.Add(this.listViewNotes); // // splitContainer1.Panel2 (右侧面板) // this.splitContainer1.Panel2.Controls.Add(this.panel1); this.splitContainer1.Size = new System.Drawing.Size(1000, 576); this.splitContainer1.SplitterDistance = 300; this.splitContainer1.TabIndex = 0; // // treeViewNotes (笔记分类树) // this.treeViewNotes.Dock = System.Windows.Forms.DockStyle.Top; this.treeViewNotes.Location = new System.Drawing.Point(0, 0); this.treeViewNotes.Name = "treeViewNotes"; this.treeViewNotes.Size = new System.Drawing.Size(300, 300); this.treeViewNotes.TabIndex = 0; this.treeViewNotes.ImageList = imageList1; this.treeViewNotes.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeViewNotes_AfterSelect); this.treeViewNotes.NodeMouseDoubleClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeViewNotes_NodeMouseDoubleClick); // 设置图像列表 imageList1.Images.Add(SystemIcons.Folder); // 文件夹图标用于分类 imageList1.Images.Add(SystemIcons.Document); // 文档图标用于笔记 // // listViewNotes (笔记列表) // this.listViewNotes.Dock = System.Windows.Forms.DockStyle.Fill; this.listViewNotes.View = System.Windows.Forms.View.Details; this.listViewNotes.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader1, // 标题 this.columnHeader2, // 分类 this.columnHeader3, // 创建时间 this.columnHeader4 // 最后修改 }); this.listViewNotes.Location = new System.Drawing.Point(0, 300); this.listViewNotes.Name = "listViewNotes"; this.listViewNotes.Size = new System.Drawing.Size(300, 276); this.listViewNotes.TabIndex = 1; this.listViewNotes.UseCompatibleStateImageBehavior = false; this.listViewNotes.FullRowSelect = true; this.listViewNotes.MultiSelect = false; this.listViewNotes.SelectedIndexChanged += new System.EventHandler(this.listViewNotes_SelectedIndexChanged); // 设置列宽 this.columnHeader1.Text = "标题"; this.columnHeader1.Width = 120; this.columnHeader2.Text = "分类"; this.columnHeader2.Width = 80; this.columnHeader3.Text = "创建时间"; this.columnHeader3.Width = 120; this.columnHeader4.Text = "最后修改"; this.columnHeader4.Width = 120; // // panel1 (主内容面板) // this.panel1.Controls.Add(this.splitContainer2); this.panel1.Controls.Add(this.lblLastModified); this.panel1.Controls.Add(this.lblNoteTitle); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1"; this.panel1.Size = new System.Drawing.Size(696, 576); this.panel1.TabIndex = 0; // // lblNoteTitle (笔记标题标签) // this.lblNoteTitle.AutoSize = true; this.lblNoteTitle.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.lblNoteTitle.Location = new System.Drawing.Point(10, 10); this.lblNoteTitle.Name = "lblNoteTitle"; this.lblNoteTitle.Size = new System.Drawing.Size(100, 21); this.lblNoteTitle.TabIndex = 1; this.lblNoteTitle.Text = "未选择笔记"; // // lblLastModified (最后修改标签) // this.lblLastModified.AutoSize = true; this.lblLastModified.Location = new System.Drawing.Point(10, 40); this.lblLastModified.Name = "lblLastModified"; this.lblLastModified.Size = new System.Drawing.Size(0, 12); this.lblLastModified.TabIndex = 2; // // splitContainer2 (右侧分割容器) // this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainer2.Location = new System.Drawing.Point(0, 60); this.splitContainer2.Name = "splitContainer2"; this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal; // // splitContainer2.Panel1 (上部分 - 笔记内容) // this.splitContainer2.Panel1.Controls.Add(this.webBrowser1); // // splitContainer2.Panel2 (下部分 - 版本历史) // this.splitContainer2.Panel2.Controls.Add(this.listViewVersions); this.splitContainer2.Size = new System.Drawing.Size(696, 516); this.splitContainer2.SplitterDistance = 350; this.splitContainer2.TabIndex = 3; // // webBrowser1 (笔记内容浏览器) // this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill; this.webBrowser1.Location = new System.Drawing.Point(0, 0); this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser1.Name = "webBrowser1"; this.webBrowser1.Size = new System.Drawing.Size(696, 350); this.webBrowser1.TabIndex = 0; // // listViewVersions (版本历史列表) // this.listViewVersions.Dock = System.Windows.Forms.DockStyle.Fill; this.listViewVersions.View = System.Windows.Forms.View.Details; this.listViewVersions.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { this.columnHeader5, // 版本 this.columnHeader6, // 创建时间 this.columnHeader7 // 描述 }); this.listViewVersions.Location = new System.Drawing.Point(0, 0); this.listViewVersions.Name = "listViewVersions"; this.listViewVersions.Size = new System.Drawing.Size(696, 162); this.listViewVersions.TabIndex = 0; this.listViewVersions.UseCompatibleStateImageBehavior = false; this.listViewVersions.FullRowSelect = true; this.listViewVersions.MultiSelect = false; this.listViewVersions.DoubleClick += new System.EventHandler(this.listViewVersions_DoubleClick); // 设置列宽 this.columnHeader5.Text = "版本"; this.columnHeader5.Width = 80; this.columnHeader6.Text = "创建时间"; this.columnHeader6.Width = 120; this.columnHeader7.Text = "描述"; this.columnHeader7.Width = 400; // // menuStrip1 (主菜单) // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.文件ToolStripMenuItem, this.编辑ToolStripMenuItem, this.笔记ToolStripMenuItem }); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1000, 25); this.menuStrip1.TabIndex = 1; this.menuStrip1.Text = "menuStrip1"; // // 文件ToolStripMenuItem (文件菜单) // this.文件ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.上传文件ToolStripMenuItem, this.打开ToolStripMenuItem1, this.退出登录ToolStripMenuItem, this.注销账号ToolStripMenuItem }); this.文件ToolStripMenuItem.Name = "文件ToolStripMenuItem"; this.文件ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); this.文件ToolStripMenuItem.Text = "文件"; // // 上传文件ToolStripMenuItem (上传文件菜单项) // this.上传文件ToolStripMenuItem.Name = "上传文件ToolStripMenuItem"; this.上传文件ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.上传文件ToolStripMenuItem.Text = "上传文件"; this.上传文件ToolStripMenuItem.Click += new System.EventHandler(this.上传文件ToolStripMenuItem_Click); // // 打开ToolStripMenuItem1 (打开文件菜单项) // this.打开ToolStripMenuItem1.Name = "打开ToolStripMenuItem1"; this.打开ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.打开ToolStripMenuItem1.Text = "打开"; this.打开ToolStripMenuItem1.Click += new System.EventHandler(this.打开ToolStripMenuItem1_Click); // // 退出登录ToolStripMenuItem (退出登录菜单项) // this.退出登录ToolStripMenuItem.Name = "退出登录ToolStripMenuItem"; this.退出登录ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.退出登录ToolStripMenuItem.Text = "退出登录"; this.退出登录ToolStripMenuItem.Click += new System.EventHandler(this.退出登录ToolStripMenuItem_Click); // // 注销账号ToolStripMenuItem (注销账号菜单项) // this.注销账号ToolStripMenuItem.Name = "注销账号ToolStripMenuItem"; this.注销账号ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.注销账号ToolStripMenuItem.Text = "注销账号"; this.注销账号ToolStripMenuItem.Click += new System.EventHandler(this.注销账号ToolStripMenuItem_Click); // // 编辑ToolStripMenuItem (编辑菜单) // this.编辑ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.修改ToolStripMenuItem, this.修改密码ToolStripMenuItem, this.删除ToolStripMenuItem }); this.编辑ToolStripMenuItem.Name = "编辑ToolStripMenuItem"; this.编辑ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); this.编辑ToolStripMenuItem.Text = "编辑"; // // 修改ToolStripMenuItem (修改菜单项) // this.修改ToolStripMenuItem.Name = "修改ToolStripMenuItem"; this.修改ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.修改ToolStripMenuItem.Text = "修改"; // // 修改密码ToolStripMenuItem (修改密码菜单项) // this.修改密码ToolStripMenuItem.Name = "修改密码ToolStripMenuItem"; this.修改密码ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.修改密码ToolStripMenuItem.Text = "修改密码"; this.修改密码ToolStripMenuItem.Click += new System.EventHandler(this.修改密码ToolStripMenuItem_Click); // // 删除ToolStripMenuItem (删除菜单项) // this.删除ToolStripMenuItem.Name = "删除ToolStripMenuItem"; this.删除ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.删除ToolStripMenuItem.Text = "删除"; // // 笔记ToolStripMenuItem (笔记菜单) // this.笔记ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.上传文件ToolStripMenuItem1, this.修改ToolStripMenuItem1, this.删除ToolStripMenuItem1, this.分类ToolStripMenuItem, this.版本历史ToolStripMenuItem }); this.笔记ToolStripMenuItem.Name = "笔记ToolStripMenuItem"; this.笔记ToolStripMenuItem.Size = new System.Drawing.Size(44, 21); this.笔记ToolStripMenuItem.Text = "笔记"; // // 上传文件ToolStripMenuItem1 (上传笔记菜单项) // this.上传文件ToolStripMenuItem1.Name = "上传文件ToolStripMenuItem1"; this.上传文件ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.上传文件ToolStripMenuItem1.Text = "上传笔记"; this.上传文件ToolStripMenuItem1.Click += new System.EventHandler(this.上传文件ToolStripMenuItem_Click); // // 修改ToolStripMenuItem1 (修改笔记菜单项) // this.修改ToolStripMenuItem1.Name = "修改ToolStripMenuItem1"; this.修改ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.修改ToolStripMenuItem1.Text = "修改"; this.修改ToolStripMenuItem1.Click += new System.EventHandler(this.修改ToolStripMenuItem_Click); // // 删除ToolStripMenuItem1 (删除笔记菜单项) // this.删除ToolStripMenuItem1.Name = "删除ToolStripMenuItem1"; this.删除ToolStripMenuItem1.Size = new System.Drawing.Size(152, 22); this.删除ToolStripMenuItem1.Text = "删除"; this.删除ToolStripMenuItem1.Click += new System.EventHandler(this.删除ToolStripMenuItem1_Click); // // 分类ToolStripMenuItem (分类菜单项) // this.分类ToolStripMenuItem.Name = "分类ToolStripMenuItem"; this.分类ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.分类ToolStripMenuItem.Text = "分类"; this.分类ToolStripMenuItem.Click += new System.EventHandler(this.分类ToolStripMenuItem_Click); // // 版本历史ToolStripMenuItem (版本历史菜单项) // this.版本历史ToolStripMenuItem.Name = "版本历史ToolStripMenuItem"; this.版本历史ToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.版本历史ToolStripMenuItem.Text = "版本历史"; this.版本历史ToolStripMenuItem.Click += new System.EventHandler(this.版本历史ToolStripMenuItem_Click); // 窗体设置 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1000, 600); this.Controls.Add(this.splitContainer1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Form2"; this.Text = "学习管理助手"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form2_FormClosing); this.Load += new System.EventHandler(this.Form2_Load); this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); this.splitContainer1.ResumeLayout(false); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); this.splitContainer2.Panel1.ResumeLayout(false); this.splitContainer2.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit(); this.splitContainer2.ResumeLayout(false); this.menuStrip1.ResumeLayout(false); private void 主界面_Load(object sender, EventArgs e) { } } } 为什么报错怎么改
最新发布
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值