WebBrowser 控件与 Windows 表单之间的交互

Interaction between WebBrowser Control and Windows Forms

我正在管理各种联系信息 - phone、地址、电子邮件、IM 等,我希望它看起来不错并节省空间,所以我想在 WebBrowser 控件中显示它。我可以将标记和内容动态地创建到一个流中,并以任何格式显示它,并且可以轻松调整颜色和字体大小。我还可以放置用于添加、编辑和删除的按钮 <input>。我喜欢这种方法,因为它看起来比 RichTextBox 更简单、更好看(如果您不这么认为,请纠正我。)
问题是关于响应这些按钮。如果选择了一个,我想隐藏 WebBrowser 并取消隐藏带有允许输入新联系人或编辑现有联系人所需的 TextBox 控件的面板。我听说这可以做到。我希望得到建议。我能想到的就是一些代码来接收 AJAX 调用,这会引发 Windows 事件,但这似乎......很奇怪。
任何想法、链接或建议都将不胜感激……甚至是不这样做的充分理由,但这似乎是高质量信息呈现的好主意,我已经动态生成了大量 html。

您可以操作 FormControls 或使用 JavaScript 从 WebBrowser 调用 C# 方法,您还可以操作 WebBrowser 控件的内容或从 C# 调用 JavaScript 方法。

从 Html

操作 WinForms

要从 WebBrowser 控件操作您的 Form 并调用 C# 方法并访问您的表单属性,您应该使用 [ComVisibleAttribute(true)] then you can set the form as ObjectForScripting 属性 装饰您的表单 WebBrowser 控制。

[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.ObjectForScripting = this;
    }
}

然后您可以简单地调用方法并以这种方式访问​​ windows 表单的元素:

从 JavaScript 调用 C# 方法:

window.external.SomeCSMethod('Method called from JavaScript');

设置来自 JavaScript 的 WinForms 控件的值:

通过使用 desginer 将 Modifier 属性 的值设置为 public,使 Form 上的 textBox1 控件成为 public .然后可以从 JavaScript:

访问它
window.external.textBox1.Text='Value set from JavaScript';

从 WinForms

操作 Html

您可以从 C# 代码操作 html Web 浏览器控件的内容并调用 JavaScript 方法或使用 Document [=93] 的方法设置 html 元素的值=] 的 WebBrowser 控制:

从 C# 调用 JavaScript 方法:

this.webBrowser1.Document.InvokeScript("someJSMethod", new []{"Method called from C#"});

设置来自 C# 的 Html 控件的值:

this.webBrowser1.Document.GetElementById("text1")
                         .SetAttribute("Value set from C#", "Value From C#");

示例代码:

您可以创建一个 Form1 class 并将 button1button2 以及 textBox1webBrowser1 放在您的 FormtextBox1Modifer 设置为 public:

[ComVisibleAttribute(true)]
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += Form1_Load;
        button1.Click += button1_Click;
        button2.Click += button2_Click;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.webBrowser1.DocumentText =
        @"<html>
        <head>
        <title>Test</title>
        <script>
            function someJSMethod(value){alert(value);}
        </script>
        </head>
        <body>
            <input type=""text"" id=""text1""/>
            <br/>
            <input type=""button"" value=""Call C# Method"" id=""button1""
            onclick=""window.external.SomeCSMethod('Method called from JavaScript');""/>
            <br/>
            <input type=""button"" value=""Set WinForms Control Value"" id=""button2""
            onclick=""window.external.textBox1.Text='Value set from JavaScript';""/>
        </body>
        </html>";
        this.webBrowser1.ObjectForScripting = this;
    }

    public void SomeCSMethod(string value)
    {
        MessageBox.Show(value);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Document
                        .InvokeScript("someJSMethod", new[]{ "Method called from C#" });
    }

    private void button2_Click(object sender, EventArgs e)
    {
        this.webBrowser1.Document.GetElementById("text1")
                                 .SetAttribute("value", "Value set from C#");
    }
}