WinForm嵌入Web网站
2024-10-21 09:25:10
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();
} }
}
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;
}
}
}
---------------------------
最新文章
- Python 修饰符
- 1117 新冲刺 day1
- android Button 切换背景,实现动态按钮和按钮颜色渐变
- [04] SQL语句优化之索引
- 转:CString::GetLength()获得字节数的正确方法
- hadoop下载
- STM32驱动OV7725摄像头颜色识别
- js实现一个简单钟表动画(javascript+html5 canvas)
- AndroidVerifyBoot
- hadoop的节点间的通信
- 安装Visual Studio 语言包时出现windows 程序兼容模式已打开.请将其关闭
- Python 3.x 格式化输出字符串 % &; format 笔记
- [转]MyEclipse8.5破解方法
- (转)看穿机器学习(W-GAN模型)的黑箱
- vmware中装的ubuntu上不了网
- ps命令各个内容信息详解
- PHP设计模式系列 - 观察者模式
- php实现注册审核功能
- java的初始化(编程思想)
- StringUtils 中 isEmpty 和 isBlank 的区别