首先先介绍下使用“Xilium.CefGlue”来在Winform中嵌入Chrome浏览器内核程序
1、新建项目(把下图的这一大串的dll文件放入到你的Debug或者Release文件夹中,稍后我会把整个项目共享出来)
2、添加项目引用
3、在应用程序入口点写入如下代码
- 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 Main()); CefRuntime.Shutdown();
4、因为对代码进行了封装,在这里不做过多解释了,调用代码如下
5、BsControl的封装如下
- BsControl Browser;
- private void Main_Load(object sender, EventArgs e)
- {
- Browser = new BsControl(this);
- }
- /// <summary>
- /// 前台主页跳转
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void Pb_Home_Click(object sender, EventArgs e)
- {
- Browser.LoadUrl("http://blog.amtemai.com/");
- }
- public partial class BsControl
- {
- CefBrowser bs;
- Control parent;
- Form _form;
- public BsControl(Form form)
- {
- _form = form;
- parent = ((Control)form.Controls.Find("BrowserContainer", true)[0]);
- TextBox AddressControl = ((TextBox)form.Controls.Find("Txt_Address", true)[0]);
- var cwi = CefWindowInfo.Create();
- cwi.SetAsChild(parent.Handle, new CefRectangle(0, 0, parent.Width, parent.Height));
- var bc = new BsClient();
- bc.OnCreated += bc_OnCreated;
- bc.OnTitleChanged += (s, e) =>
- {
- form.BeginInvoke(new Action(() =>
- {
- form.Text = ((TitleChangedEventArgs)e).Title+"IT小马";
- }));
- };
- bc.OnAddressChanged += (s, e) =>
- {
- form.BeginInvoke(new Action(() =>
- {
- AddressControl.Text = ((AddressChangedEventArgs)e).Address;
- }));
- };
- bc.OnStatusMessaged += (s, e) =>
- {
- form.BeginInvoke(new Action(() =>
- {
- }));
- };
- bc.OnLoadingStateChanged += (s, e) =>
- {
- form.BeginInvoke(new Action(() =>
- {
- LoadingStateChangedEventArgs loadingstate = ((LoadingStateChangedEventArgs)e);
- if (loadingstate.IsLoading)
- {
- ((PictureBox)form.Controls.Find("Pb_Refresh", true)[0]).Image = Properties.Resources.appbar_close;
- }
- else
- {
- ((PictureBox)form.Controls.Find("Pb_Refresh", true)[0]).Image = Properties.Resources.appbar_refresh;
- }
- if (CanGoForWard())
- {
- ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).Enabled = true;
- ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).BackColor = Color.Transparent;
- }
- else
- {
- ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).Enabled = false;
- ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).BackColor = Color.LightGray;
- }
- if (CanGoBack())
- {
- ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).Enabled = true;
- ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).BackColor = Color.Transparent;
- }
- else
- {
- ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).Enabled = false;
- ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).BackColor = Color.LightGray;
- }
- }));
- };
- var bs = new CefBrowserSettings() { };
- CefBrowserHost.CreateBrowser(cwi, bc, bs, "http://blog.amtemai.com/");
- parent.SizeChanged += parent_SizeChanged;
- AddressControl.Text = "http://blog.amtemai.com/";
- }
- void bc_OnCreated(object sender, EventArgs e)
- {
- bs = (CefBrowser)sender;
- var handle = bs.GetHost().GetWindowHandle();
- ResizeWindow(handle, parent.Width, parent.Height);
- }
- void parent_SizeChanged(object sender, EventArgs e)
- {
- if (bs != null)
- {
- var handle = bs.GetHost().GetWindowHandle();
- ResizeWindow(handle, parent.Width, parent.Height);
- }
- }
- private void ResizeWindow(IntPtr handle, int width, int height)
- {
- if (handle != IntPtr.Zero)
- {
- NativeMethod.SetWindowPos(handle, IntPtr.Zero,
- 0, 0, width, height,
- 0x0002 | 0x0004
- );
- }
- }
- public void LoadUrl(string url)
- {
- bs.GetMainFrame().LoadUrl(url);
- }
- public void ReloadOrStop()
- {
- if (bs.GetMainFrame().Browser.IsLoading)
- {
- bs.GetMainFrame().Browser.StopLoad();
- }
- else
- {
- bs.GetMainFrame().Browser.Reload();
- }
- }
- public bool IsLoading()
- {
- return bs.GetMainFrame().Browser.IsLoading;
- }
- public void GoForward()
- {
- if (bs.GetMainFrame().Browser.CanGoForward)
- {
- bs.GetMainFrame().Browser.GoForward();
- }
- }
- public void GoBack()
- {
- if (bs.GetMainFrame().Browser.CanGoBack)
- {
- bs.GetMainFrame().Browser.GoBack();
- }
- }
- public bool CanGoForWard()
- {
- return bs.GetMainFrame().Browser.CanGoForward;
- }
- public bool CanGoBack()
- {
- return bs.GetMainFrame().Browser.CanGoBack;
- }
- }