Embed GeckoFX in Winforms

Embed GeckoFX in Winforms

Added by Nasir Mahmood updated on Wednesday, October 15, 2014

Problem:

Dot Net has a built in web browser control which is wrapper of IE. This control is depended on IE it uses IE's rendering engine (whatever version is installed on the end-user's machine). This control has its own pros and cons for more information read Webbrowser Control Limitations

In this tutorial we will learn how to embed GeckoFX in winforms using c#.

About Gecko-FX:

GeckoFX is a cross platform Webrowser control for embedding into WinForms Applications. This can be used with .NET on Windows and with mono on Linux

Prerequisites:

This project has following dependences

  1. GeckoFX Library
  2. XulRunner

Setup:

First off all create a new winfowm project. Download GeckoFx library and add reference of assemblies (Geckofx-Core.dll and Geckofx-Winforms.dll) as shown below

adding reference of assemblies

Gecko Web Browser in toolbox:

To add GeckoWebBrowser control in visual studio toolbox, right click in toolbox and click on choose items as shown below

adding items to toolbox in visual studio

adding items to toolbox in visual studio dialog

Now browse files until we find GeckoFX library as shown below.

adding items to toolbox in visual studio dialog

After clicking ok new item will appear in toolbox as shown below

adding geckofx control to toolbox in visual studio

Adding Browser Control:

Now simply drag GeckoWebBrowser control to form as shown below.

adding geckofx control to toolbox in visual studio

Completing UI:

Now add new buttons to winform as shown below

adding geckofx control to toolbox in visual studio

Adding Functionality:

First of all add following code in form constructor, lets suppose we have xulrunner in bin folder

var path = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "xulrunner"); 
Gecko.Xpcom.Initialize(path); 
                    

On form load event add following code

geckoWebBrowser1.Navigate("http://www.nWebFront.com");
                    

we can see the result as below

adding geckofx control to toolbox in visual studio

Adding Goback Support:

Add following code in btnback click event

geckoWebBrowser1.GoBack();
                    

Adding Gofarword Support:

Add following code in btnforward click event

geckoWebBrowser1.GoForward();
                    

Adding Reload Support:

Add following code in btnreload click event

geckoWebBrowser1.Reload();
                    

Adding Stop Support:

Add following code in btnstop click event

geckoWebBrowser1.Stop();
                    

Adding Navigate to Address Support:

Add following code in btngo click event, now browser will navigate to address entered in text box

geckoWebBrowser1.Navigate(textBox1.Text);
                    

Adding Playing Video Support:

By default GeckoWebBrowser does not support memory consuming process like playing videos , to enable it add the following code before navigate:

Gecko.GeckoPreferences.Default["extensions.blocklist.enabled"] = false;
                    

Adding Proxy Support:

Add following code to enable browsing using proxy server

Gecko.GeckoPreferences.User["network.proxy.type"] = 1;
Gecko.GeckoPreferences.User["network.proxy.http"] = xxx.xxx.xxx.xxx;
Gecko.GeckoPreferences.User["network.proxy.http_port"] = xxxx;
Gecko.GeckoPreferences.User["network.proxy.ssl"] = xxx.xxx.xxx.xxx;
Gecko.GeckoPreferences.User["network.proxy.ssl_port"] = xxxx;
                    

Adding Useragent Support:

Add following code to change user agent of browser

string useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; pl; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)";
Gecko.GeckoPreferences.User["general.useragent.override"] = useragent;
                    

Download Code:

Download

About

32 Tutorials
27 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected