Recently, I tried an interesting desktop application writing method: using HTML+JS+CSS to do the display of the view, but not the control of WinForm. It worked out pretty well.

1. Create a Form.

2. Add a WebBrowser control to the Form.

        public static WebBrowser CreateWebBrower(Object objectForScripting)
        {
            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.ScrollBarsEnabled = false;
            webBrowser1.AllowWebBrowserDrop = false;// Set the AllowWebBrowserDrop property of the WebBrowser control to false to prevent the WebBrowser control from opening files that it drags onto.
            webBrowser1.IsWebBrowserContextMenuEnabled = false;/ / to the control of IsWebBrowserContextMenuEnabled attribute is set to false, in order to prevent the WebBrowser control when users right-click it display the shortcut menu.
            webBrowser1.WebBrowserShortcutsEnabled = false;/ / to the control of WebBrowserShortcutsEnabled attribute is set to false, in order to prevent the WebBrowser control response shortcuts.
            webBrowser1.ScriptErrorsSuppressed = true;// Set the ScriptErrorsSuppressed property of this control to true to prevent the WebBrowser control from displaying error messages about script code problems.
            if(objectForScripting ! =null)
            {
                webBrowser1.ObjectForScripting = objectForScripting;
            }
            return webBrowser1;
        }
Copy the code

3. Use HTML to write page view display, more flexible than using WinForm Control.

4. Specify webBrowser1.url as the page you just did in step 3.

    WebBrowser1.Url = new Uri("file://" + Path.Combine(Application.StartupPath, "web/index.htm"));
Copy the code

5. Specify WebBrowser1. ObjectForScripting as just newly established form object, and in the form on the class statement “com visibility”. Do it:

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Form1 : Form
    {
        public Form1(){ InitializeComponent(); }}Copy the code

6. Write a method in the form that must be public. This method, let’s call it background method.

        public void Run2()
        {
        }
Copy the code

7. This allows us to call the run2 method in HTML

 window.external.Run2();
Copy the code

This completes a basic structure in which pages can be written in HTML, displayed in winform, and methods written in c# can be called in js from HTML pages. Of course you can pass parameters in a method, you can pass parameters to basic data like string,int,float, etc.

——————–

However, we specify that JS is event-driven. After we call a method, if the method execution event is too long, it’s not good to block there, so we have to think about the method implementation of the callback.

As follows:

In the HTML page, I need to call a background method (let’s say :method1, which I wrote in c# as described above), and when method method1 is done, call the callback method to get the callback data back in the HTML.

Let’s take a step-by-step look at how to implement it.

(fn4) {fn4_callback (fn4_callback) {fn4_callback (fn4_callback) {fn4_callback (fn4_callback); We want to execute a long background method (run2), and when run2 is done, the callback executes our fn4_callback method.

        function fn4() {
            window.external.Run2(fn4_callback); 
        }
        function fn4_callback(no,str) {
            alert(no);
            alert(str);
        }
Copy the code

2. Let’s look at the background method, how to write run2.

Public void Run2(object STR) {//dosomethind Type t = str.getType (); t.InvokeMember("", System.Reflection.BindingFlags.InvokeMethod, null, str, new object[]{1,"xx"}); }Copy the code

Note that the argument here is of type object, which is of type “system.__comObject” when viewed using the getType() method. We use reflection to call it. The more powerful method is T.INvokdemember.

This completes the implementation of the entire callback. Very cool.

Download code: yunpan.cn/Qe3yByINkUW…

———

Reference:www.pcreview.co.uk/forums/hand…