Develop chrome core Browser Based on. NET [3]

Source: Internet
Author: User

This article describes how to use cefglue to develop a simple browser.

I:

Cefglue is built on the CEF project,
The CEF Project is a C/C ++ project;
Cefglue only accesses some DLL generated by the CEF project through pinvoke.
Next, let's take a look at the usage of some DLL and resources generated by the CEF project.
Open this directory \ cef_binary_3.1453.1236_windows_ium ium \ release
Libcef. dll --------------------------> core class library of CEF
Icudt. dll --------------------------> supports Unicode class libraries.
Ffmpegsumo. dll ----------------> supports audio and video class libraries
D3dcompiler_43.dll ---------------> A 3D class library is supported in WINXP.
D3dcompiler_46.dll ---------------> Windows 7 and later win support 3D Class Libraries
Libegl. dll -------------------------> used to support 3D
Libglesv2.dll ---------------------> used to support 3D

Open the directory \ cef_binary_3.1453.1236_windows_ium ium \ Resources
Locales ---------------------------> this folder stores the language resources of various countries.
CEF. Pak --------------------------> resources related to WebKit (the core of Google's browser is WebKit)
Devtools_resources.pak ---------> resources related to the debugger (our project can use the Google browser debugger)

II:

Create a winform project named "add cefdemo"
Create a folder named DLL in the Assembly
Set the pre-generated event command for this Assembly in the Assembly attributes

xcopy $(ProjectDir)dll $(TargetDir) /e /i /y

The purpose of this command is to copy the files in the DLL folder to the output directory during each compilation.

Copy all the files in the \ cef_binary_3.1453.1236_windows_xilium \ release directory to the dll Directory of cefdemo.
Copy all the files and folders in the \ cef_binary_3.1453.1236_windows_xilium \ Resources Directory to the dll Directory.
Note:Most of the files in the locales subdirectory are useless. You can delete all the files and leave only the zh-CN.pak files.
Open the xilium. cefglue project, release compile the cefglue assembly, and copy the generated xilium. cefglue. DLL to the dll Directory of cefdemo.
Add xilium. cefglue. dll reference in the cefdemo Project

III:

Modify the code of program. CS:

        static void Main()        {            CefRuntime.Load();            var mainArgs = new CefMainArgs(new string[] { });            var exitCode = CefRuntime.ExecuteProcess(mainArgs, null);            if (exitCode != -1)                return;            var settings = new CefSettings            {                SingleProcess = false,                MultiThreadedMessageLoop = true,                LogSeverity = CefLogSeverity.Disable,                Locale = "zh-CN"            };            CefRuntime.Initialize(mainArgs, settings, null);            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            if (!settings.MultiThreadedMessageLoop)            {                Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };            }            Application.Run(new CefBrowser());            CefRuntime.Shutdown();        }

Let's explain the code at 1.1:

Cefruntime. Load ();
This line of code is used to load the CEF Runtime
------------------------
VaR mainargs = new cefmainargs (New String [] {});
This line of code can collect command line parameters and be passed to the CEF browser.
------------------------
VaR exitcode = cefruntime. executeprocess (mainargs, null );
If (exitcode! =-1)
Return;
The above code is used to start the second process. As for what to do with the second process, I have not studied it in depth (either the second process of the browser or an executable file)
Note:The cefruntime. executeprocess method must be called at the entrance of the program;
--------------------------
VaR settings = new cefsettings
{
Singleprocess = false,
Multithreadedmessageloop = true,
Logseverity = ceflogseverity. Disable,
Locale = "ZH-CN"
};
There are many CEF configuration parameters. Here are some explanations:
Singleprocess = false: multi-process is used here.
Note:We strongly recommend that you do not use a single process, which is unstable and not supported by the chromium kernel.
Multithreadedmessageloop = true: The purpose here is to let the browser's message loop be executed in a separate thread
Note:We strongly recommend that you set this parameter to true, or you have to process the message loop in your program; call cefdomessageloopwork ()
Locale = "ZH-CN": language resources used by WebKit. If this parameter is not set, the default value is en-us.
Note:The locals directory must be in the directory where the executable file is located, and the corresponding resource file must be in this directory.
------------------------------
Cefruntime. initialize (mainargs, settings, null );
This code transfers the created configuration information and command line information to the CEF runtime.
This function must be called in the main thread of the application.
------------------------------
If (! Settings. multithreadedmessageloop)
{
Application. Idle + = (sender, e) =>{ cefruntime. domessageloopwork ();};
}
If the value of multithreadedmessageloop you set earlier is false,
Then you can add the above Code and call cefruntime. domessageloopwork ();
------------------------------
Cefruntime. Shutdown ();
When the main process ends, you need to release the resources of the CEF and end the browser process.

IV:

Create a form in the project,
In the design view, adjust the window to a proper size.
(Adjust the size of the browser if you want it to become large)
Then, adjust some attributes of the form.

This. formborderstyle = system. windows. forms. formborderstyle. fixeddialog; this. maximizebox = false; this. minimizebox = false; this. name = "cefbrowser"; this. TEXT = "simplest implementation ";

In the example provided in this article, the browser has not changed with the size of the container form
Therefore, we disabled the window maximization function and the drag function to change the window size.

V:

Add the following code to the window constructor:

var cwi = CefWindowInfo.Create();cwi.SetAsChild(this.Handle, new CefRectangle(0, 0, this.Width, this.Height));var bc = new BrowserClient();var bs = new CefBrowserSettings() { };CefBrowserHost.CreateBrowser(cwi,bc, bs,"http://www.cnblogs.com/liulun");

Then run the program and you will see a browser, such:

Although there is no scroll bar, the window cannot be dragged to change the size
But when you move the mouse over the webpage, scroll the scroll wheel and the webpage will still scroll.

VI:

Next we will explain in detail the meaning of the above Code.

Cefwindowinfo is a class implemented by the CEF browser window, including the specific implementation in windows, Linux, and Mac.
The create static method in this class is responsible for creating instances of this class,
I will execute this statement in Windows to get the implementation method of the CEF browser in windows.
------------------
CWI. setaschild (this. Handle, new cefrectangle (0, 0, this. Width, this. Height ));
This line of code is used to combine the created CEF browser window with the winform window we created
This. Handle is the handle of the winform window we created.
The setaschild function renders the CEF browser window as a child window of the winform window.
Cefrectangle indicates the position and size of the CEF browser window that will appear in the parent window.
-------------------
VaR BC = new browserclient ();
Browserclient is a Class I created in the project.
This class does not have any logic and attributes, but inherits the cefclient class.
The cefclient class has many virtual methods for rewriting,
For example, getdisplayhandler, getdownloadhandler, getjsdialoghandler, etc.
Note:This class is very important. We will add a lot of content for this class in the next chapter.
---------------------
VaR BS = new cefbrowsersettings (){};
Previously, we set cefsettings in program.
Some global settings for the CEF Environment
Here is cefbrowsersettings
This is a global setting for the CEF browser environment.
You can configure many parameters here.
For example:
Defaultencoding (encoding method for all web content, ISO-8859-1 by default)
Userstylesheetlocation (used for all webpage styles. Set this field in this format: Data: text/CSS; charset = UTF-8; base64, [csscontent])
Remotefonts (font for all webpages)
JavaScript (whether JavaScript scripts can be executed on all webpages)
Javascriptopenwindows (used to determine whether all webpages can open a window through JS)
(There are also many similar settings that readers can study on their own)
----------------------------
Cefbrowserhost. createbrowser (CWI, BC, BS, "http://www.cnblogs.com/liulun ");
When the code is executed in this line, the browser subwindow is created.
Needless to say about the first three parameters of createbrowser.
The last parameter is the page you want your browser to access.
Note:This method is executed asynchronously (non-blocking). That is to say, you cannot know when the window is created. (You can register an event that is successfully created by the window in other ways .)

Download source code:

Http://files.cnblogs.com/liulun/CefDemo.zip
Note: For download convenience, I have removed the resources in the DLL folder and the class library to be referenced.

Modification record:
: I created an article and completed part of it.
: Added part of the article, and encountered problems.
: Solved the problem, updated and added most of the content, and modified the article layout.
: Added the last part of content, modified the layout, read through the article, and corrected the typos.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.