妙博客

主机评测 香港服务器 洛杉矶VPS测评

C# winform自制基于Cersharp的chrom浏览器,

1.创建哪一个c#winform项目

2.拉一个panel到窗体程序,然后输入如下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using CefSharp;
using CefSharp.WinForms;
namespace browser_2tubiao
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            father = this;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CefSettings settings = new CefSettings();
            settings.ProductVersion = "";
            //     Cef.Initialize(settings);
            string url = "https://baidu.com";
            web = new ChromiumWebBrowser(url);
            //  web.RegisterJsObject("server", server, false);
            this.panel1.Controls.Add(web);
            web.FrameLoadEnd += loadDone1;
            web.KeyboardHandler = new CEFKeyBoardHander();
        }
        static Form1 father = null;
        static ChromiumWebBrowser web;
        Server server = new Server();
        class CEFKeyBoardHander : IKeyboardHandler
        {//https://bbs.csdn.net/topics/392375553
            public bool OnKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
            {
                if (type == KeyType.KeyUp && Enum.IsDefined(typeof(Keys), windowsKeyCode))
                {
                    var key = (Keys)windowsKeyCode;
                    switch (key)
                    {
                        case Keys.F12:
                            browser.ShowDevTools();
                            break;
                        case Keys.F5:
                            if (modifiers == CefEventFlags.ControlDown)
                            {
                                //MessageBox.Show("ctrl+f5");
                                browser.Reload(true); //强制忽略缓存
                            }
                            else
                            {
                                //MessageBox.Show("f5");
                                browser.Reload();
                            }
                            break;
                    }
                }
                return false;
            }
            public bool OnPreKeyEvent(IWebBrowser browserControl, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
            {
                return false;
            }
        }
        class Server
        {
        }
        public void loadDone1(object sender, FrameLoadEndEventArgs e)
        {
            web.FrameLoadEnd += loadDone2;
        }
        private void loadDone2(object sender, FrameLoadEndEventArgs e)
        {
        }
    }
}

3.发现报错,则下载Cefsharp的dll,直接解压到debug目录

链接:https://pan.baidu.com/s/1v4gWecBYzzm4n3qRnFG78w 

提取码:blpq 


4.右键项目,添加引用

image.png


5.点最近,浏览器,然后把所有以dll结尾的添加进来,即可。

image.png

直接运行,就能看到百度的首页了!

Copyright Your 142132.com Rights Reserved. 赣ICP备17010829号-2