WinForm 打开指定网页,类似浏览器
2021-06-17 00:02
标签:网页 linear lan ati etc cas github art x11 需求:一般用于考试,窗口打开默认最大化,禁用最小化,最大化,鼠标右键,双击窗口最小化等 读取指定地址跳转,指定标题等 技术:采用CefSharp https://github.com/cefsharp/CefSharp/wiki 一,添加包 引用 using CefSharp; WinForm 打开指定网页,类似浏览器 标签:网页 linear lan ati etc cas github art x11 原文地址:https://www.cnblogs.com/qingjiawen/p/14888667.html CefSharp简单来说就是一款.Net编写的浏览器包,方便你在Winform和WPF中内嵌的Chrome浏览器组件。
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();
}
///
文章标题:WinForm 打开指定网页,类似浏览器
文章链接:http://soscw.com/index.php/essay/94789.html