Form1.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Microsoft.Web.WebView2.Core;
  2. using System.Drawing.Imaging;
  3. using System;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Net.Http;
  7. using System.Windows.Forms;
  8. using Microsoft.Web.WebView2.Core;
  9. using Microsoft.Web.WebView2.WinForms;
  10. using Microsoft.Web.WebView2.Wpf;
  11. namespace EdgeVoyager
  12. {
  13. public partial class Form1 : Form
  14. {
  15. int zoom = 100;
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. InitializeAsync();
  20. }
  21. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  22. {
  23. Form1 newForm = new Form1();
  24. newForm.webView21.Source = new Uri(e.Uri);
  25. newForm.Show();
  26. e.Handled = true;
  27. }
  28. async void InitializeAsync()
  29. {
  30. await webView21.EnsureCoreWebView2Async(null);
  31. await webView21.EnsureCoreWebView2Async(null);
  32. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  33. webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
  34. if (Settings1.Default.firstwindow == true)
  35. {
  36. webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN");
  37. Settings1.Default.firstwindow = false;
  38. Settings1.Default.Save();
  39. }
  40. }
  41. private void back_button_Click(object sender, EventArgs e)
  42. {
  43. webView21.CoreWebView2.GoBack();
  44. }
  45. private void forward_button_Click(object sender, EventArgs e)
  46. {
  47. webView21.CoreWebView2.GoForward();
  48. }
  49. private void refresh_button_Click(object sender, EventArgs e)
  50. {
  51. webView21.CoreWebView2.Reload();
  52. }
  53. private void stop_button_Click(object sender, EventArgs e)
  54. {
  55. webView21.CoreWebView2.Stop();
  56. }
  57. private void search_button_Click(object sender, EventArgs e)
  58. {
  59. }
  60. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  61. {
  62. string currentUrl = webView21.Source.ToString();
  63. if (!comboBox1.Items.Contains(currentUrl))
  64. {
  65. comboBox1.Items.Add(currentUrl);
  66. }
  67. comboBox1.Text = currentUrl;
  68. progressBar.Value = 0;
  69. progressLabel.Text = "0%";
  70. StatusLabel.Text = "开始导航到:" + currentUrl;
  71. }
  72. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  73. {
  74. string currentUrl = webView21.Source.ToString();
  75. if (!comboBox1.Items.Contains(currentUrl))
  76. {
  77. comboBox1.Items.Add(currentUrl);
  78. }
  79. comboBox1.Text = currentUrl;
  80. progressBar.Value = 100;
  81. progressLabel.Text = "100%";
  82. StatusLabel.Text = "完成。";
  83. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  84. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  85. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  86. faviconUrl = faviconUrl.Trim('"'); // 去掉引号
  87. if (!string.IsNullOrEmpty(faviconUrl))
  88. {
  89. using (var client = new HttpClient())
  90. {
  91. var faviconData = await client.GetByteArrayAsync(faviconUrl);
  92. using (var ms = new MemoryStream(faviconData))
  93. {
  94. using (var img = Image.FromStream(ms))
  95. {
  96. if (img is Bitmap bitmap)
  97. {
  98. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  99. }
  100. else
  101. {
  102. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  110. {
  111. if (e.KeyCode == Keys.Enter)
  112. {
  113. string url = comboBox1.Text;
  114. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  115. {
  116. webView21.CoreWebView2.Navigate(url);
  117. }
  118. else
  119. {
  120. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  121. }
  122. }
  123. }
  124. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  125. {
  126. Environment.Exit(0);
  127. }
  128. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  129. {
  130. progressBar.Value = 50; // 假设加载内容时进度为50%
  131. progressLabel.Text = "50%";
  132. StatusLabel.Text = "内容开始加载";
  133. }
  134. private async void SetZoomLevel(double zoomLevel)
  135. {
  136. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  137. }
  138. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  139. {
  140. SetZoomLevel(100);
  141. toolStripMenuItem3.Enabled = true;
  142. }
  143. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  144. {
  145. zoom = zoom + 25;
  146. SetZoomLevel(zoom);
  147. toolStripMenuItem3.Enabled = true;
  148. }
  149. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  150. {
  151. zoom = zoom - 25;
  152. SetZoomLevel(zoom);
  153. if (zoom == 25)
  154. toolStripMenuItem3.Enabled = false;
  155. }
  156. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  157. {
  158. webView21.CoreWebView2.Reload();
  159. }
  160. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  161. {
  162. webView21.CoreWebView2.Stop();
  163. }
  164. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  165. {
  166. webView21.CoreWebView2.GoForward();
  167. }
  168. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  169. {
  170. webView21.CoreWebView2.GoBack();
  171. }
  172. }
  173. }