WinForm嵌入Web网站

1.安装CefSharp

2.GetMac()获取用户mac地址方法

3.根据错误提示新建CEFKeyBoardHander.cs

form1.cs

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        private ChromiumWebBrowser browser;

        private string mac { get; set; }

        public Form1()
        {
            GetMac();
            InitializeComponent();
            InitBrowser();
        }

        public void GetMac() {
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection nics = mc.GetInstances();
            foreach (ManagementObject nic in nics)
            {
                if (Convert.ToBoolean(nic["ipEnabled"]) == true)
                {
                    mac = nic["MacAddress"].ToString();//Mac地址
                    //string ip = (nic["IPAddress"] as String[])[0];//IP地址
                    //string ipsubnet = (nic["IPSubnet"] as String[])[0];//子网掩码
                    //string ipgateway = (nic["DefaultIPGateway"] as String[])[0];//默认网关
                }
            }
        }

        public void InitBrowser()
        {
            //设置语言环境  以上这段代码一定要在new ChromiumWebBrowser之前调用
            var setting = new CefSettings();

            setting.CefCommandLineArgs.Add("--disable-web-security", "1");//关闭同源策略,允许跨域

            setting.Locale = "zh-CN";
            //缓存路径
            setting.CachePath = System.AppDomain.CurrentDomain.BaseDirectory + "BrowserCache";
            //浏览器引擎的语言
            setting.AcceptLanguageList = "zh-CN,zh;q=0.8";
            setting.LocalesDirPath = System.AppDomain.CurrentDomain.BaseDirectory + "LocaleDir";
            //日志文件
            setting.LogFile = System.AppDomain.CurrentDomain.BaseDirectory + "LogData";


            setting.CefCommandLineArgs.Add("mute-audio", "true");

            setting.PersistSessionCookies = true;
          
            setting.UserDataPath = System.AppDomain.CurrentDomain.BaseDirectory + "UserData";
            Cef.Initialize(setting);


            // string url = AppDomain.CurrentDomain.BaseDirectory + @"../../html/index.html";
            // url = url.Replace("\\", "/").Replace(" ", "%20");

            mac = mac.Replace(":", "-");

            string url = @"http://111.111.111.111:9000/111.php/index/login/mac/"+mac;

            
            //   Cef.Initialize(new CefSettings());
            browser = new ChromiumWebBrowser(url);
            Font font = new Font("微软雅黑", 10.5f);

            browser.Font = font;
            browser.Dock = DockStyle.Fill;

            browser.KeyboardHandler = new CEFKeyBoardHander();



            browser.LoadingStateChanged += new EventHandler<LoadingStateChangedEventArgs>(LoadingStateChangeds);
            // 加载完成后 , 执行操作 
            browser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(FrameEndFunc);


            // 加载完成后 , 执行操作 
            browser.FrameLoadStart += new EventHandler<FrameLoadStartEventArgs>(FrameLoadStart);

            this.Controls.Add(browser);
        }

        private void LoadingStateChangeds(object sender, LoadingStateChangedEventArgs e)
        {
           // throw new NotImplementedException();
        }

        private void FrameLoadStart(object sender, FrameLoadStartEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void FrameEndFunc(object sender, FrameLoadEndEventArgs e)
        {
           // throw new NotImplementedException();
        }

    }
}
View Code

CEFKeyBoardHander.cs

using CefSharp;

namespace WindowsFormsApp
{
    internal class CEFKeyBoardHander : IKeyboardHandler
    {
        public bool OnKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey)
        {
            //throw new System.NotImplementedException();
            return false;
        }

        public bool OnPreKeyEvent(IWebBrowser chromiumWebBrowser, IBrowser browser, KeyType type, int windowsKeyCode, int nativeKeyCode, CefEventFlags modifiers, bool isSystemKey, ref bool isKeyboardShortcut)
        {
            return false;
        }
    }
}
View Code

 

 

 

 

 

 

 

 

---------------------------

posted on 2022-06-20 16:19  逝年-  阅读(351)  评论(0编辑  收藏  举报