41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
namespace Webview2WinFormsApp
|
||
{
|
||
public partial class Form1 : Form
|
||
{
|
||
/// <summary>
|
||
/// https://www.bilibili.com/read/cv23844020/
|
||
/// </summary>
|
||
private AutoAdaptWindowsSize autoSize;
|
||
public Form1()
|
||
{
|
||
InitializeComponent();
|
||
autoSize = new AutoAdaptWindowsSize(this);
|
||
this.SizeChanged += Form1_SizeChanged;
|
||
}
|
||
|
||
private void Form1_SizeChanged(object? sender, EventArgs e)
|
||
{
|
||
if (autoSize != null) // 一定加这个判断,电脑缩放布局不是100%的时候,会报错
|
||
{
|
||
autoSize.FormSizeChanged();
|
||
}
|
||
}
|
||
|
||
private void Form1_Load(object sender, EventArgs e)
|
||
{
|
||
webView21.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\web\\index.html");
|
||
}
|
||
|
||
private void webView21_CoreWebView2InitializationCompleted(object sender, Microsoft.Web.WebView2.Core.CoreWebView2InitializationCompletedEventArgs e)
|
||
{
|
||
if (e.IsSuccess)
|
||
{
|
||
Console.WriteLine("webView21_CoreWebView2InitializationCompleted");
|
||
var bridge = new Bridge();
|
||
//
|
||
webView21.CoreWebView2.AddHostObjectToScript("bridge", bridge);
|
||
webView21.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\web\\index.html");
|
||
}
|
||
}
|
||
}
|
||
} |