using Microsoft.Web.WebView2.WinForms; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Wpf; namespace EdgeVoyager { public partial class Form1 : Form { int zoom = 100; public Form1() { InitializeComponent(); InitializeAsync(); } async void InitializeAsync() { await webView21.EnsureCoreWebView2Async(null); webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN"); } private void back_button_Click(object sender, EventArgs e) { webView21.CoreWebView2.GoBack(); } private void forward_button_Click(object sender, EventArgs e) { webView21.CoreWebView2.GoForward(); } private void refresh_button_Click(object sender, EventArgs e) { webView21.CoreWebView2.Reload(); } private void stop_button_Click(object sender, EventArgs e) { webView21.CoreWebView2.Stop(); } private void search_button_Click(object sender, EventArgs e) { } private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e) { string currentUrl = webView21.Source.ToString(); if (!comboBox1.Items.Contains(currentUrl)) { comboBox1.Items.Add(currentUrl); } comboBox1.Text = currentUrl; progressBar.Value = 0; progressLabel.Text = "0%"; } private void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e) { string currentUrl = webView21.Source.ToString(); if (!comboBox1.Items.Contains(currentUrl)) { comboBox1.Items.Add(currentUrl); } comboBox1.Text = currentUrl; progressBar.Value = 100; progressLabel.Text = "100%"; if (e.IsSuccess) { string script = @" var title = document.title; window.domAutomationController.send(title);"; using (var result = await webview21.CoreWebView2.) { string pageTitle = result?.GetString(); Console.WriteLine($"页面标题: {pageTitle}"); } // 获取网页的图标 string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);"); faviconUrl = faviconUrl.Trim('"'); // 去掉引号 if (!string.IsNullOrEmpty(faviconUrl)) { using (var client = new HttpClient()) { try { varfaviconData = await client.GetByteArrayAsync(faviconUrl); using (var ms = new MemoryStream(faviconData)) { this.Icon = Icon.FromHandle((Bitmap.FromStream(ms)).GetHicon()); } } catch (Exception ex) { StatusLabel.Text = ex.Message; } } } } } private void comboBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { string url = comboBox1.Text; if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) { webView21.CoreWebView2.Navigate(url); } else { MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) { Environment.Exit(0); } private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e) { progressBar.Value = 50; // 假设加载内容时进度为50% progressLabel.Text = "50%"; } private async void SetZoomLevel(double zoomLevel) { await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'"); } private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e) { SetZoomLevel(100); toolStripMenuItem3.Enabled = true; } private void toolStripMenuItem2_Click(object sender, EventArgs e) { zoom = zoom + 25; SetZoomLevel(zoom); toolStripMenuItem3.Enabled = true; } private void toolStripMenuItem3_Click(object sender, EventArgs e) { zoom = zoom - 25; SetZoomLevel(zoom); if (zoom == 25) toolStripMenuItem3.Enabled = false; } } }