【问题标题】:C# winforms webbrowser control with JavascriptC# winforms webbrowser 控件与 Javascript
【发布时间】:2017-10-10 09:57:40
【问题描述】:

我在 webbrowser 控件中加载了一个 HTML 页面。 HTML有一个javascript函数windows.print,它试图从我的浏览器打印。请问如何通过 windows.print() 函数通过 Winforms C# 进行打印。或者如何将要打印的 javascript 对象传递到要打印的 C# 中。

我是 C# 的初学者,希望能得到详细的解释。非常感谢!

【问题讨论】:

    标签: javascript c# .net winforms webbrowser-control


    【解决方案1】:

    你可能会做这样的事情

    namespace WindowsFormsApplication
    {
        // This first namespace is required for the ComVisible attribute used on the ScriptManager class.
        using System.Runtime.InteropServices;
        using System.Windows.Forms;
    
        // This is your form.
        public partial class Form1 : Form
        {
            // This nested class must be ComVisible for the JavaScript to be able to call it.
            [ComVisible(true)]
            public class ScriptManager
            {
                // Variable to store the form of type Form1.
                private Form1 mForm;
    
                // Constructor.
                public ScriptManager(Form1 form)
                {
                    // Save the form so it can be referenced later.
                    mForm = form;
                }
    
                // This method can be called from JavaScript.
                public void MethodToCallFromScript()
                {
                    // Call a method on the form.
                    mForm.DoSomething();
                }
    
                // This method can also be called from JavaScript.
                public void AnotherMethod(string message)
                {
                    MessageBox.Show(message);
                }
            }
    
            // This method will be called by the other method (MethodToCallFromScript) that gets called by JavaScript.
            public void DoSomething()
            {
                // Indicate success.
                MessageBox.Show("It worked!");
            }
    
            // Constructor.
            public Form1()
            {
                // Boilerplate code.
                InitializeComponent();
    
                // Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#.
                webBrowser1.ObjectForScripting = new ScriptManager(this);
    
                // Create the webpage.
                webBrowser1.DocumentText = @"<html>
                    <head>
                        <title>Test</title>
                    </head>
                    <body>
                    <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                        <br />
                        <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello');"" />
                    </body>
                    </html>";
            }
        }
    }
    

    【讨论】:

    • 非常感谢,成功了。现在我正在尝试向 MethodCallFromScript 方法添加一个参数,但它会引发错误。只需要一次争论。
    • 你总是可以定义一个方法来在你的 ScriptManager 类上接受多个参数,定义一个适合你的 js 函数调用的方法签名并调用它。
    • 请你举个例子,如何用上面的代码实现?
    • 没关系,我已经能够做到这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    相关资源
    最近更新 更多