Form1.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using Microsoft.Web.WebView2.WinForms;
  2. using Microsoft.Web.WebView2.Core;
  3. using Microsoft.Web.WebView2.Wpf;
  4. namespace EdgeVoyager
  5. {
  6. public partial class Form1 : Form
  7. {
  8. int zoom = 100;
  9. public Form1()
  10. {
  11. InitializeComponent();
  12. InitializeAsync();
  13. }
  14. async void InitializeAsync()
  15. {
  16. await webView21.EnsureCoreWebView2Async(null);
  17. webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN");
  18. }
  19. private void back_button_Click(object sender, EventArgs e)
  20. {
  21. webView21.CoreWebView2.GoBack();
  22. }
  23. private void forward_button_Click(object sender, EventArgs e)
  24. {
  25. webView21.CoreWebView2.GoForward();
  26. }
  27. private void refresh_button_Click(object sender, EventArgs e)
  28. {
  29. webView21.CoreWebView2.Reload();
  30. }
  31. private void stop_button_Click(object sender, EventArgs e)
  32. {
  33. webView21.CoreWebView2.Stop();
  34. }
  35. private void search_button_Click(object sender, EventArgs e)
  36. {
  37. }
  38. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  39. {
  40. string currentUrl = webView21.Source.ToString();
  41. if (!comboBox1.Items.Contains(currentUrl))
  42. {
  43. comboBox1.Items.Add(currentUrl);
  44. }
  45. comboBox1.Text = currentUrl;
  46. progressBar.Value = 0;
  47. progressLabel.Text = "0%";
  48. }
  49. private void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  50. {
  51. string currentUrl = webView21.Source.ToString();
  52. if (!comboBox1.Items.Contains(currentUrl))
  53. {
  54. comboBox1.Items.Add(currentUrl);
  55. }
  56. comboBox1.Text = currentUrl;
  57. progressBar.Value = 100;
  58. progressLabel.Text = "100%";
  59. if (e.IsSuccess)
  60. {
  61. string script = @"
  62. var title = document.title;
  63. window.domAutomationController.send(title);";
  64. using (var result = await webview21.CoreWebView2.)
  65. {
  66. string pageTitle = result?.GetString();
  67. Console.WriteLine($"页面标题: {pageTitle}");
  68. }
  69. // 获取网页的图标
  70. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  71. faviconUrl = faviconUrl.Trim('"'); // 去掉引号
  72. if (!string.IsNullOrEmpty(faviconUrl))
  73. {
  74. using (var client = new HttpClient())
  75. {
  76. try
  77. {
  78. varfaviconData = await client.GetByteArrayAsync(faviconUrl);
  79. using (var ms = new MemoryStream(faviconData))
  80. {
  81. this.Icon = Icon.FromHandle((Bitmap.FromStream(ms)).GetHicon());
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. StatusLabel.Text = ex.Message;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  93. {
  94. if (e.KeyCode == Keys.Enter)
  95. {
  96. string url = comboBox1.Text;
  97. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  98. {
  99. webView21.CoreWebView2.Navigate(url);
  100. }
  101. else
  102. {
  103. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  104. }
  105. }
  106. }
  107. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  108. {
  109. Environment.Exit(0);
  110. }
  111. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  112. {
  113. progressBar.Value = 50; // 假设加载内容时进度为50%
  114. progressLabel.Text = "50%";
  115. }
  116. private async void SetZoomLevel(double zoomLevel)
  117. {
  118. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  119. }
  120. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  121. {
  122. SetZoomLevel(100);
  123. toolStripMenuItem3.Enabled = true;
  124. }
  125. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  126. {
  127. zoom = zoom + 25;
  128. SetZoomLevel(zoom);
  129. toolStripMenuItem3.Enabled = true;
  130. }
  131. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  132. {
  133. zoom = zoom - 25;
  134. SetZoomLevel(zoom);
  135. if (zoom == 25)
  136. toolStripMenuItem3.Enabled = false;
  137. }
  138. }
  139. }