For carrying WebBrowser view, HTML written page, write the back-end code c#; JS

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

A recent attempt to a funny desktop application method: use HTML+JS+CSS to do view display, without WinForm control. The effect of good.

1 a new form Form.

2 in Form add a WebBrowser control.

        public static WebBrowser CreateWebBrower(Object objectForScripting)
            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.ScrollBarsEnabled = false;
            webBrowser1.AllowWebBrowserDrop = false;//The AllowWebBrowserDrop control's WebBrowser property is set to false, to prevent the WebBrowser control from opening files dropped onto it. 
            webBrowser1.IsWebBrowserContextMenuEnabled = false;//Set the control's IsWebBrowserContextMenuEnabled property to false, to prevent the display the shortcut menu in the WebBrowser control when the user right clicks it.
            webBrowser1.WebBrowserShortcutsEnabled = false;//Set the control's WebBrowserShortcutsEnabled property to false to prevent the WebBrowser control response, shortcut keys. 
            webBrowser1.ScriptErrorsSuppressed = true;//Set the control's ScriptErrorsSuppressed property to true, in order to prevent the WebBrowser control to display error messages for script code problems. 
            if (objectForScripting != null)
                webBrowser1.ObjectForScripting = objectForScripting;
            return webBrowser1;

3 use HTML write page view display, more flexible than WinForm control.

4 specify WebBrowser1.url for just third steps to do page.

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

5 specify WebBrowser1.ObjectForScripting for just the new form object, and the statement "com visibility in the form of class “. To do so:

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    public partial class Form1 : Form
        public Form1()


6 in the form of writing methods, must be public. This approach, we first called &ldquo background method;”

        public void Run2()

7 so that we can be used in the HTML calls the run2 method.


We have completed a basic structure, in this structure, we can use HTML to write a page, and displayed in the WinForm window method, and can be written using JS to call c# on the HTML page. Of course there is method can pass parameters, parameters can be passed, the basic data such as string, int, float etc.


However, we have to specify the JS is an event driven. We call a method, if the method execution events is too long, obstruction there is no good, so we consider the callback method to achieve.

The following:

In the HTML page, I need to call a background method (hypothesis: method1, here refers to the method of c# written in accordance with the above steps), when the method1 is completed, calling the callback method, so that HTML can receive the callback data.

We have a look how to achieve it.

1 so we call in the JS method of our background, look carefully at the fn4 method, in this method, call back method Run2, and passes a parameter “ fn4_callback”, this parameter is a method (function). The method we want to execute a long time (run2) when the run2 method, after the execution is completed, the callback executes the fn4_callback method we.

        function fn4() {
        function fn4_callback(no,str) {

2 we have a look back method, how to write run2.

public void Run2(object str)
        //Dosomethind long time.
            Type t = str.GetType();
            t.InvokeMember("", System.Reflection.BindingFlags.InvokeMethod, null, str, new object[]{1,"xx"});

Note that the parameter is of type object, use the getType () method to view it is a “ System.__ComObject” type. Reflection we use the way to call it. Is awesome.

The t.InvokdeMember method.

So we can implement the callback. Very cool.

Download code:


Reference resources

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Dean at November 13, 2013 - 11:01 AM