WinForm 打开指定网页,类似浏览器
时间:2021-06-16 18:13:49
收藏:0
阅读:0
需求:一般用于考试,窗口打开默认最大化,禁用最小化,最大化,鼠标右键,双击窗口最小化等
读取指定地址跳转,指定标题等
技术:采用CefSharp
CefSharp简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。
https://github.com/cefsharp/CefSharp/wiki
一,添加包
引用
using CefSharp;
using CefSharp.WinForms;
public partial class Form1 : Form { private static string WebUrl = ""; private static string WebName = ""; public Form1() { InitializeComponent(); // Start the browser after initialize global component InitializeChromium(); } private ChromiumWebBrowser chromeBrowser = null; private void Form1_Load(object sender, EventArgs e) { //chromeBrowser.ShowDevTools(); chromeBrowser.MenuHandler = new MenuHandler(); } private void InitializeChromium() { //string page = string.Format(@"{0}\assets\test.html", Application.StartupPath); //if (!File.Exists(page)) //{ // MessageBox.Show("未找到网页文件:" + page); //} XmlModel.Config _model = DataOperation.Instance.GetConfig(); if (_model != null) { WebUrl = _model.WebUrl; WebName = _model.WebName; } this.Text = WebName; string page = WebUrl; // Initialize cef with the provided settings CefSettings settings = new CefSettings(); settings.CefCommandLineArgs.Add("disable-gpu", "1");//添加禁用GPU,解决偏移问题 Cef.Initialize(settings); // Create a browser component chromeBrowser = new ChromiumWebBrowser(page); // Add it to the form and fill it to the form window. this.Controls.Add(chromeBrowser); this.WindowState = FormWindowState.Maximized; chromeBrowser.Dock = DockStyle.Fill; // Allow the use of local resources in the browser BrowserSettings browserSettings = new BrowserSettings(); browserSettings.FileAccessFromFileUrls = CefState.Enabled; browserSettings.UniversalAccessFromFileUrls = CefState.Enabled; chromeBrowser.BrowserSettings = browserSettings; CefSharpSettings.LegacyJavascriptBindingEnabled = true; // Register an object in javascript named "cefCustomObject" with function of the CefCustomObject class :3 chromeBrowser.RegisterJsObject("cefCustomObject", new CefCustomObject(chromeBrowser)); } protected override void WndProc(ref Message m) { if (m.Msg == 0x112) { switch ((int)m.WParam) { //禁止双击标题栏关闭窗体 case 0xF063: case 0xF093: m.WParam = IntPtr.Zero; break; //禁止拖拽标题栏还原窗体 case 0xF012: case 0xF010: m.WParam = IntPtr.Zero; break; //禁止双击标题栏 case 0xf122: m.WParam = IntPtr.Zero; break; //允许关闭按钮 case 0xF060: //m.WParam = IntPtr.Zero; break; ////允许最大化按钮 //case 0xf020: // //m.WParam = IntPtr.Zero; // break; ////允许最小化按钮 //case 0xf030: // //m.WParam = IntPtr.Zero; // break; ////允许还原按钮 //case 0xf120: // //m.WParam = IntPtr.Zero; // break; default: m.WParam = IntPtr.Zero; break; } } base.WndProc(ref m); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Cef.Shutdown(); } /// <summary> /// 屏蔽右键 /// </summary> internal class MenuHandler : IContextMenuHandler { public bool OnBeforeContextMenu(IWebBrowser browser, IBrowser ibrower, IFrame iframe, IContextMenuParams icontextmenuparams, IMenuModel imenumodel) { return false; } void CefSharp.IContextMenuHandler.OnBeforeContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model) { model.Clear(); } bool CefSharp.IContextMenuHandler.OnContextMenuCommand(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.CefMenuCommand commandId, CefSharp.CefEventFlags eventFlags) { //throw new NotImplementedException(); return false; } void CefSharp.IContextMenuHandler.OnContextMenuDismissed(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame) { //throw new NotImplementedException(); } bool CefSharp.IContextMenuHandler.RunContextMenu(CefSharp.IWebBrowser browserControl, CefSharp.IBrowser browser, CefSharp.IFrame frame, CefSharp.IContextMenuParams parameters, CefSharp.IMenuModel model, CefSharp.IRunContextMenuCallback callback) { return false; } } }
评论(0)