Form1.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. namespace EdgeVoyager
  11. {
  12. public partial class Form1 : Form
  13. {
  14. int zoom = 100;
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. InitializeAsync();
  19. }
  20. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  21. {
  22. Form1 newForm = new Form1();
  23. // 设置新窗口中WebView2的Source属性为要打开的链接
  24. newForm.webView21.Source = new Uri(e.Uri);
  25. // 显示新窗口
  26. newForm.Show();
  27. // 设置Handled为true,表示已经处理了新窗口请求
  28. e.Handled = true;
  29. }
  30. async void InitializeAsync()
  31. {
  32. await webView21.EnsureCoreWebView2Async(null);
  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. // 如果图片不是Bitmap类型,可以考虑其他方式来获取图标
  103. // 例如,你可以尝试将图片保存到文件,然后使用Icon.ExtractAssociatedIcon方法获取图标
  104. // 或者直接使用img作为窗体的图标(如果可行)
  105. // 这里以直接使用img作为窗体图标为例(需确保img可以作为窗体图标)
  106. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  114. {
  115. if (e.KeyCode == Keys.Enter)
  116. {
  117. string url = comboBox1.Text;
  118. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  119. {
  120. webView21.CoreWebView2.Navigate(url);
  121. }
  122. else
  123. {
  124. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  125. }
  126. }
  127. }
  128. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  129. {
  130. Environment.Exit(0);
  131. }
  132. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  133. {
  134. progressBar.Value = 50; // 假设加载内容时进度为50%
  135. progressLabel.Text = "50%";
  136. StatusLabel.Text = "内容开始加载";
  137. }
  138. private async void SetZoomLevel(double zoomLevel)
  139. {
  140. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  141. }
  142. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  143. {
  144. SetZoomLevel(100);
  145. toolStripMenuItem3.Enabled = true;
  146. }
  147. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  148. {
  149. zoom = zoom + 25;
  150. SetZoomLevel(zoom);
  151. toolStripMenuItem3.Enabled = true;
  152. }
  153. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  154. {
  155. zoom = zoom - 25;
  156. SetZoomLevel(zoom);
  157. if (zoom == 25)
  158. toolStripMenuItem3.Enabled = false;
  159. }
  160. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  161. {
  162. webView21.CoreWebView2.Reload();
  163. }
  164. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  165. {
  166. webView21.CoreWebView2.Stop();
  167. }
  168. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  169. {
  170. webView21.CoreWebView2.GoForward();
  171. }
  172. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  173. {
  174. webView21.CoreWebView2.GoBack();
  175. }
  176. }
  177. }