2023-12-12 16:53:12 +08:00

41 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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");
}
}
}
}