How to connect a third-party browser in an application in C #
- Tutorial
- Recovery mode
At some point, I felt uncomfortable using the standard WebBrowser control offered by Visual Studio.
There were several reasons:
1. An IE engine was used, which in itself is already a strong argument.
2. Curve work with JS.
3. Lack of scaling.
4. If you run on a machine where IE6 is installed, then all its "advantages" are transferred to the application.
As a result, the search for alternative solutions was launched.
2 SDKs were reviewed. xulrunner (Mozilla) and Awesomium (Chrome)
Both are connected approximately the same, but just in case, I will describe both.
1. xulrunner
step 1
Go to the official website http://ftp.mozilla.org/pub/mozilla.org/xulrunner/releases/
Choose the version that suits us.
Here I would like to make a small digression. The later the version, the heavier it is: for example, 1.9v weighs 21MB., And already 19v weighs 32MB. In addition, each next version requires more resources. Looking ahead, I’ll say that this was the main reason for rejecting this SDK.
By this link we go to the folder with the selected version, then sdk / xulrunner-XXen-US.win32.sdk.zip
step 2
Download and unpack the contents. We are interested in the archive only daddy bin. Copy it to the application folder and rename it to xulrunner. The name can be another, but so that there is no difference with my description, it is better to make a name like that.
step 3
To work with this SDK, we need the Skybound.GeckoFX.bin.v1.9.1.0 library. It can be downloaded herehttp://code.google.com/p/geckofx/
This library runs xulrunner version 1.9.
If you decide to use a later version, then you need to look for Skybound.GeckoFX 2.0, it is suitable for versions 2.0 and higher.
step 4
In our application, add to the References Skybound.Gecko.dll from the downloaded archive
step 5
Edit the Program class:
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form2());
}
}
We initialize the browser control:
string path = "C:\\Program Files (x86)\\xulrunner\\"; //Путь к распакованной и переименованной папке bin из архива SDK
Skybound.Gecko.Xpcom.Initialize(path);
webBrowser1 = new Skybound.Gecko.GeckoWebBrowser();
webBrowser1.Parent = this.panel1;
webBrowser1.Dock = DockStyle.Fill;
string u = "http://www.ya.ru";
//Загрузка выбранной страницы
webBrowser1.Navigate(u);
According to this code, the parent panel will be panel1
step 6.
In order to execute any JavaScript programmatically, I had to go to tricks, because either the functions that are intended for this purpose are not added in this version, or something else. But the only way out I found was:
webBrowser1.Navigate("javascript:ImGecko()");
The downside is that you cannot get the result of processing, only in the form of alert ();
This problem also caused me to start looking for a replacement and came to Awesomium
2. Awesomium
step 1
Download the SDK at http://awesomium.com/
There are two versions on the site - stable (1.6.5) and test (1.7) . The test overall works better.
For work, we need the files
Awesomium.Core.dll
Awesomium.dll
Awesomium.Windows.Controls.Design.dll
Awesomium.Windows.Controls.dll
Awesomium.Windows.Forms.dll
step 2
Connect the
Awesomium.Core.dll library to the project
Awesomium.Windows .Forms.dll
step 3
In the Toolbox, right-click and select Choose Items. Next, click browse and connect Awesomium.Windows.Forms.dll, after which we will have new controls WebControl, AddressBox, etc. First of all, WebControl is important for us.
Step 4. We place the
control in the design.
Step 5.
Examples of using various functions.
Opening pages
webControl1.LoadURL("http://ya.ru");
Cookies Download
string cookie; // Строка с cookie которые нужно подключить
string domen; // Домен
string[] mascook;
mascook = cookie.Split(';');
Awesomium.Core.WebCore.ClearCookies();
foreach (string cook in mascook)
Awesomium.Core.WebCore.SetCookie("http://" + domen, cook + "; domain=" + domen, true, true);
Getting the values of the variables mx and my
Awesomium.Core.JSValue x = webControl1.ExecuteJavascriptWithResult("mx", 500);
Awesomium.Core.JSValue y = webControl1.ExecuteJavascriptWithResult("my", 500);
500 is a timeout.
JS function call
webControl1.CallJavascriptFunction("", "al", new Awesomium.Core.JSValue[] {});
al - this is the name of the function; the
second parameter passes the values to the function. In this case, it is empty.
The above examples work on Awesomium 1.6.5, in version 1.7 the architecture is slightly changed and some methods may be missing or called differently.
Conclusions
At the moment, in all projects where you need a browser, I use Awesomium: it works more stable and the functionality is richer. The load from it is small. The only negative is that the right-click on the flash does not work out, so you can not change the necessary parameters (maybe I just did not figure out how). Otherwise, this SDK suited me in everything.