[原创] 在.Net程序中嵌入Chrome浏览器内核(二)

首先先介绍下使用“Xilium.CefGlue”来在Winform中嵌入Chrome浏览器内核程序

1、新建项目(把下图的这一大串的dll文件放入到你的Debug或者Release文件夹中,稍后我会把整个项目共享出来)

2、添加项目引用

3、在应用程序入口点写入如下代码


 
 
  1. CefRuntime.Load();
  2. var mainArgs = new CefMainArgs(new string[] { });
  3. var exitCode = CefRuntime.ExecuteProcess(mainArgs, null);
  4. if (exitCode != -1)
  5. return;
  6. var settings = new CefSettings
  7. {
  8. SingleProcess = false,
  9. MultiThreadedMessageLoop = true,
  10. LogSeverity = CefLogSeverity.Disable,
  11. Locale = "zh-CN"
  12. };
  13. CefRuntime.Initialize(mainArgs, settings, null);
  14. Application.EnableVisualStyles();
  15. Application.SetCompatibleTextRenderingDefault(false);
  16. if (!settings.MultiThreadedMessageLoop)
  17. {
  18. Application.Idle += (sender, e) => { CefRuntime.DoMessageLoopWork(); };
  19. }
  20. Application.Run(new Main()); CefRuntime.Shutdown();

4、因为对代码进行了封装,在这里不做过多解释了,调用代码如下


 
 
  1. BsControl Browser;
  2. private void Main_Load(object sender, EventArgs e)
  3. {
  4. Browser = new BsControl(this);
  5. }
  6.  
  7. /// <summary>
  8. /// 前台主页跳转
  9. /// </summary>
  10. /// <param name="sender"></param>
  11. /// <param name="e"></param>
  12. private void Pb_Home_Click(object sender, EventArgs e)
  13. {
  14. Browser.LoadUrl("http://blog.amtemai.com/");
  15. }
5、BsControl的封装如下



 
 
  1. public partial class BsControl
  2. {
  3. CefBrowser bs;
  4. Control parent;
  5. Form _form;
  6. public BsControl(Form form)
  7. {
  8. _form = form;
  9. parent = ((Control)form.Controls.Find("BrowserContainer", true)[0]);
  10. TextBox AddressControl = ((TextBox)form.Controls.Find("Txt_Address", true)[0]);
  11. var cwi = CefWindowInfo.Create();
  12. cwi.SetAsChild(parent.Handle, new CefRectangle(0, 0, parent.Width, parent.Height));
  13. var bc = new BsClient();
  14. bc.OnCreated += bc_OnCreated;
  15. bc.OnTitleChanged += (s, e) =>
  16. {
  17. form.BeginInvoke(new Action(() =>
  18. {
  19. form.Text = ((TitleChangedEventArgs)e).Title+"IT小马";
  20. }));
  21. };
  22. bc.OnAddressChanged += (s, e) =>
  23. {
  24. form.BeginInvoke(new Action(() =>
  25. {
  26. AddressControl.Text = ((AddressChangedEventArgs)e).Address;
  27. }));
  28. };
  29. bc.OnStatusMessaged += (s, e) =>
  30. {
  31. form.BeginInvoke(new Action(() =>
  32. {
  33.  
  34. }));
  35. };
  36. bc.OnLoadingStateChanged += (s, e) =>
  37. {
  38. form.BeginInvoke(new Action(() =>
  39. {
  40. LoadingStateChangedEventArgs loadingstate = ((LoadingStateChangedEventArgs)e);
  41. if (loadingstate.IsLoading)
  42. {
  43. ((PictureBox)form.Controls.Find("Pb_Refresh", true)[0]).Image = Properties.Resources.appbar_close;
  44. }
  45. else
  46. {
  47. ((PictureBox)form.Controls.Find("Pb_Refresh", true)[0]).Image = Properties.Resources.appbar_refresh;
  48. }
  49. if (CanGoForWard())
  50. {
  51. ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).Enabled = true;
  52. ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).BackColor = Color.Transparent;
  53. }
  54. else
  55. {
  56. ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).Enabled = false;
  57. ((PictureBox)form.Controls.Find("Pb_Right", true)[0]).BackColor = Color.LightGray;
  58. }
  59. if (CanGoBack())
  60. {
  61. ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).Enabled = true;
  62. ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).BackColor = Color.Transparent;
  63. }
  64. else
  65. {
  66. ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).Enabled = false;
  67. ((PictureBox)form.Controls.Find("Pb_Left", true)[0]).BackColor = Color.LightGray;
  68. }
  69. }));
  70. };
  71. var bs = new CefBrowserSettings() { };
  72. CefBrowserHost.CreateBrowser(cwi, bc, bs, "http://blog.amtemai.com/");
  73. parent.SizeChanged += parent_SizeChanged;
  74. AddressControl.Text = "http://blog.amtemai.com/";
  75. }
  76.  
  77.  
  78. void bc_OnCreated(object sender, EventArgs e)
  79. {
  80. bs = (CefBrowser)sender;
  81. var handle = bs.GetHost().GetWindowHandle();
  82. ResizeWindow(handle, parent.Width, parent.Height);
  83. }
  84. void parent_SizeChanged(object sender, EventArgs e)
  85. {
  86. if (bs != null)
  87. {
  88. var handle = bs.GetHost().GetWindowHandle();
  89. ResizeWindow(handle, parent.Width, parent.Height);
  90. }
  91. }
  92. private void ResizeWindow(IntPtr handle, int width, int height)
  93. {
  94. if (handle != IntPtr.Zero)
  95. {
  96. NativeMethod.SetWindowPos(handle, IntPtr.Zero,
  97. 0, 0, width, height,
  98. 0x0002 | 0x0004
  99. );
  100. }
  101. }
  102. public void LoadUrl(string url)
  103. {
  104. bs.GetMainFrame().LoadUrl(url);
  105. }
  106. public void ReloadOrStop()
  107. {
  108. if (bs.GetMainFrame().Browser.IsLoading)
  109. {
  110. bs.GetMainFrame().Browser.StopLoad();
  111. }
  112. else
  113. {
  114. bs.GetMainFrame().Browser.Reload();
  115. }
  116. }
  117.  
  118. public bool IsLoading()
  119. {
  120. return bs.GetMainFrame().Browser.IsLoading;
  121. }
  122. public void GoForward()
  123. {
  124. if (bs.GetMainFrame().Browser.CanGoForward)
  125. {
  126. bs.GetMainFrame().Browser.GoForward();
  127. }
  128. }
  129. public void GoBack()
  130. {
  131. if (bs.GetMainFrame().Browser.CanGoBack)
  132. {
  133. bs.GetMainFrame().Browser.GoBack();
  134. }
  135. }
  136. public bool CanGoForWard()
  137. {
  138. return bs.GetMainFrame().Browser.CanGoForward;
  139. }
  140. public bool CanGoBack()
  141. {
  142. return bs.GetMainFrame().Browser.CanGoBack;
  143. }
  144. }


点我下载



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值