WebBrowser 控件和 Windows 窗体之间的交互 [英] Interaction between WebBrowser Control and Windows Forms

查看:21
本文介绍了WebBrowser 控件和 Windows 窗体之间的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am managing various contact information - phone, address, email, IM, other and I want it to look good and save real estate, so I want to display it in a WebBrowser control. I can create the markup and content dynamically into a stream and display it with any format with colors and font size easily adjusted. I can also put buttons <input> for Add, Edit, and Delete. I like this method, because it seems easier and better looking than a RichTextBox (correct me if you think otherwise.)
The question is about responding to those buttons. If one is selected, I want to hide the WebBrowser and unhide a Panel with the TextBox controls needed to allow entry of a new contact or editing of an existing one. I've heard this can be done. I was hoping for suggestions. All I can think of is some code to pick up an AJAX call, that would raise a Windows event, but that seems... odd.
Any ideas, links or suggestions would be appreciated .. or even a good reason why not to do it, but it seems like a good idea for high quality information presentation and I've generated plenty of html dynamically.

推荐答案

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

You can manipulate the Form and Controls or call C# methods from WebBrowser using JavaScript and also you can manipulate content of WebBrowser control or call JavaScript methods from C#.

要从 WebBrowser 控件操作您的 Form 并调用 C# 方法并访问您的表单属性,您应该使用 [ComVisibleAttribute(true)] 然后您可以将表单设置为 ObjectForScripting WebBrowser 控件的属性.

To manipulate your Form from WebBrowser control and call C# methods and access your form properties you should decorate your form with [ComVisibleAttribute(true)] then you can set the form as ObjectForScripting property of WebBrowser control.

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

然后,您可以通过这种方式简单地调用方法并访问窗体的元素:

Then you can simply call methods and access to elements of your windows form this way:

从 JavaScript 调用 C# 方法:

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

从 JavaScript 设置 WinForms 控件的值:

通过将Modifier 属性的值设置为public,使Form 上的textBox1 控件公开使用设计师.然后它可以从 JavaScript 访问:

Make the textBox1 control on your Form to be public by setting the value of Modifier property to public using desginer. Then it can be accessible from JavaScript:

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

从 WinForms 操作 Html

您可以从 C# 代码中操作 Web 浏览器控件的 html 内容,并使用 Document 的属性网页浏览器控件:

从 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 类并放置 button1button2textBox1webBrowser1 在您的 Form 上将 textBox1Modifer 设置为 public:

You can create a Form1 class and put button1 and button2 and textBox1 and webBrowser1 on your Form set the Modifer of textBox1 to 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#");
    }
}

这篇关于WebBrowser 控件和 Windows 窗体之间的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆