123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- using Microsoft.Web.WebView2.Core;
- using System.Drawing.Imaging;
- using System;
- using System.Drawing;
- using System.IO;
- using System.Net.Http;
- using System.Windows.Forms;
- using Microsoft.Web.WebView2.Core;
- using Microsoft.Web.WebView2.WinForms;
- using Microsoft.Web.WebView2.Wpf;
- namespace EdgeVoyager
- {
- public partial class Form1 : Form
- {
- int zoom = 100;
- public Form1()
- {
- InitializeComponent();
- InitializeAsync();
- }
- private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri(e.Uri);
- newForm.Show();
- e.Handled = true;
- }
- async void InitializeAsync()
- {
- await webView21.EnsureCoreWebView2Async(null);
- await webView21.EnsureCoreWebView2Async(null);
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
- if (Settings1.Default.firstwindow == true)
- {
- webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN");
- Settings1.Default.firstwindow = false;
- Settings1.Default.Save();
- }
- }
- 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%";
- StatusLabel.Text = "开始导航到:" + currentUrl;
- }
- private async 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%";
- StatusLabel.Text = "完成。";
- string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
- this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
- 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())
- {
- var faviconData = await client.GetByteArrayAsync(faviconUrl);
- using (var ms = new MemoryStream(faviconData))
- {
- using (var img = Image.FromStream(ms))
- {
- if (img is Bitmap bitmap)
- {
- this.Icon = Icon.FromHandle(bitmap.GetHicon());
- }
- else
- {
- this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
- }
- }
- }
- }
- }
- }
- 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%";
- StatusLabel.Text = "内容开始加载";
- }
- 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;
- }
- private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- }
- }
|