按照步骤 C#在Winform下使用Cefsharp嵌套Chrome内核浏览器(一)完成准备工作后,开始编写我们第一个DEMO代码如下:
1)添加引用
1 2 |
using CefSharp; using CefSharp.WinForms; |
2)初始化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
private Boolean CefInitialized = false;//是否初始化 private ChromiumWebBrowser chromeBrowser = null;//Chromium浏览器 private string AppUrl = "https://www.xiangbuqilai.com"; public ScreenBox screen = null;//ScreenBox 对象 public void InitializeChromium(string url) { CefSettings settings = new CefSettings(); // Initialize cef with the provided settings if (false == CefInitialized) { settings.CefCommandLineArgs.Add("disable-gpu", "1");//设置关闭GPU渲染 Cef.Initialize(settings);//设置 CefInitialized = true; } if("" == url){ url = AppUrl; } // Create a browser component chromeBrowser = new ChromiumWebBrowser(url); chromeBrowser.IsBrowserInitializedChanged += new EventHandler<IsBrowserInitializedChangedEventArgs>(chromeBrowser_IsBrowserInitializedChanged); screen = new ScreenBox(); // Add it to the form and fill it to the form window. screen.Controls.Add(chromeBrowser); chromeBrowser.Dock = DockStyle.Fill; chromeBrowser.KeyDown += ChromeBrowser_KeyDown; screen.Show(); } void chromeBrowser_IsBrowserInitializedChanged(object sender, IsBrowserInitializedChangedEventArgs e) { //chromeBrowser.ShowDevTools();//开启控制台调试 } |
2)运行查看效果
Comments | 1 条评论
码梦为生
加油