Form1.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. using System.Collections;
  2. using System.Diagnostics;
  3. using System.Resources;
  4. using System.Text;
  5. using Microsoft.Web.WebView2.Core;
  6. namespace EdgeVoyager
  7. {
  8. public partial class Form1 : Form
  9. {
  10. int zoom = 100;
  11. private bool isWebView2Focused = false;
  12. private HttpClient httpClient = new HttpClient();
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. InitializeAsync();
  17. }
  18. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  19. {
  20. Form1 newForm = new Form1();
  21. newForm.webView21.Source = new Uri(e.Uri);
  22. newForm.Show();
  23. e.Handled = true;
  24. }
  25. async void InitializeAsync()
  26. {
  27. await webView21.EnsureCoreWebView2Async(null);
  28. webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
  29. await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
  30. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  31. webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
  32. webView21.GotFocus += (s, e) => isWebView2Focused = true;
  33. webView21.LostFocus += (s, e) => isWebView2Focused = false;
  34. if (Settings1.Default.firstwindow == true)
  35. {
  36. string htmlString = @"<!DOCTYPE html>
  37. <html lang=""zh-CN"">
  38. <head>
  39. <meta charset=""UTF-8"">
  40. <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
  41. <title>起始页</title>
  42. <style>
  43. body {
  44. margin: 0;
  45. padding: 0;
  46. background-image: url('https://www.beijing.gov.cn/images/index_top_ch_20190924.jpg');
  47. background-size: cover;
  48. background-repeat: no-repeat;
  49. background-position: right top;
  50. background-attachment: fixed;
  51. font-family: Arial, sans-serif;
  52. }
  53. .header {
  54. background-color: rgba(255, 255, 255, 0.8);
  55. backdrop-filter: blur(10px);
  56. padding: 10px 20px;
  57. display: flex;
  58. align-items: center;
  59. position: relative;
  60. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  61. }
  62. .logo {
  63. width: 48px;
  64. height: 48px;
  65. }
  66. .nav-item {
  67. margin-left: 20px;
  68. color: #333;
  69. text-decoration: none;
  70. font-size: 16px;
  71. }
  72. .nav-links {
  73. display: flex;
  74. align-items: center;
  75. }
  76. .welcome-text {
  77. text-align: center;
  78. margin: 20px 0;
  79. font-size: 36px;
  80. font-weight: bold;
  81. color: #0078D4;
  82. }
  83. .search-container {
  84. display: flex;
  85. justify-content: center;
  86. align-items: center;
  87. margin-top: 20px;
  88. border-radius: 15px;
  89. overflow: hidden;
  90. }
  91. .search-input {
  92. width: 70vw;
  93. max-width: 1000px;
  94. padding: 10px;
  95. font-size: 16px;
  96. border: none;
  97. border-radius: 15px 0 0 15px;
  98. height: 20px;
  99. }
  100. .search-button {
  101. padding: 10px 20px;
  102. width: 80px;
  103. font-size: 16px;
  104. cursor: pointer;
  105. border: none;
  106. background-color: #0078D4;
  107. color: white;
  108. border-radius: 0 15px 15px 0;
  109. height: 40px;
  110. }
  111. .search-engine-select {
  112. margin-left: 0px;
  113. padding: 10px;
  114. font-size: 16px;
  115. border: none;
  116. border-radius: 0px;
  117. background-color: #f2f2f2;
  118. cursor: pointer;
  119. }
  120. .acrylic-panel {
  121. background-color: rgba(255, 255, 255, 0.8);
  122. backdrop-filter: blur(10px);
  123. padding: 20px;
  124. border-radius: 8px;
  125. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  126. margin: 20px 0;
  127. width: 90vw;
  128. margin-left: auto;
  129. margin-right: auto;
  130. max-width: 1200px;
  131. }
  132. @media (max-width: 600px) {
  133. .search-button {
  134. padding: 8px 16px;
  135. }
  136. }
  137. </style>
  138. </head>
  139. <body>
  140. <div class=""header"">
  141. <svg class=""logo"" xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"">
  142. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHna"" x1=""27.702"" x2=""35.722"" y1=""18.41"" y2=""26.008"" gradientUnits=""userSpaceOnUse"">
  143. <stop offset=""0"" stop-color=""#0e5fa4""/>
  144. <stop offset=""1"" stop-color=""#1595df""/>
  145. </linearGradient>
  146. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHna)"" d=""M24,6l18,16v17.586c0,0.891-1.077,1.337-1.707,0.707L24,24V6z""/>
  147. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnb"" x1=""16.667"" x2=""22.667"" y1=""27"" y2=""27"" gradientTransform=""matrix(-3 0 0 -1 92 42)"" gradientUnits=""userSpaceOnUse"">
  148. <stop offset=""0"" stop-color=""#50e6ff""/>
  149. <stop offset=""1"" stop-color=""#32bdef""/>
  150. </linearGradient>
  151. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnb)"" d=""M24,6l18,16c-7.249-2.341-14-2-18,2V6z""/>
  152. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnc"" x1=""18.483"" x2=""27.736"" y1=""19.517"" y2=""10.264"" gradientUnits=""userSpaceOnUse"">
  153. <stop offset=""0"" stop-color=""#0e5fa4""/>
  154. <stop offset=""1"" stop-color=""#1595df""/>
  155. </linearGradient>
  156. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnc)"" d=""M6,24L22,6h17.586c0.891,0,1.337,1.077,0.707,1.707L24,24H6z""/>
  157. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnd"" x1=""3"" x2=""9"" y1=""24"" y2=""24"" gradientTransform=""matrix(0 3 -1 0 39 -3)"" gradientUnits=""userSpaceOnUse"">
  158. <stop offset=""0"" stop-color=""#50e6ff""/>
  159. <stop offset=""1"" stop-color=""#32bdef""/>
  160. </linearGradient>
  161. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnd)"" d=""M6,24L22,6c-2.341,7.249-2,14,2,18H6z""/>
  162. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHne"" x1=""19.748"" x2=""7.491"" y1=""29.748"" y2=""17.491"" gradientUnits=""userSpaceOnUse"">
  163. <stop offset=""0"" stop-color=""#0e5fa4""/>
  164. <stop offset=""1"" stop-color=""#1595df""/>
  165. </linearGradient>
  166. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHne)"" d=""M24,42L6,26V8.414c0-0.891,1.077-1.337,1.707-0.707L24,24V42z""/>
  167. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnf"" x1=""6"" x2=""12"" y1=""33"" y2=""33"" gradientTransform=""matrix(3 0 0 1 -12 0)"" gradientUnits=""userSpaceOnUse"">
  168. <stop offset=""0"" stop-color=""#50e6ff""/>
  169. <stop offset=""1"" stop-color=""#32bdef""/>
  170. </linearGradient>
  171. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnf)"" d=""M24,42L6,26c7.249,2.341,14,2,18-2V42z""/>
  172. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHng"" x1=""28.457"" x2=""19.623"" y1=""29.543"" y2=""38.377"" gradientUnits=""userSpaceOnUse"">
  173. <stop offset=""0"" stop-color=""#0e5fa4""/>
  174. <stop offset=""1"" stop-color=""#1595df""/>
  175. </linearGradient>
  176. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHng)"" d=""M24,24L7.707,40.293C7.077,40.923,7.523,42,8.414,42H26l7.529-8.471L24,24z""/>
  177. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHnh"" x1=""24"" x2=""33.529"" y1=""33"" y2=""33"" gradientUnits=""userSpaceOnUse"">
  178. <stop offset=""0"" stop-color=""#50e6ff""/>
  179. <stop offset=""1"" stop-color=""#32bdef""/>
  180. </linearGradient>
  181. <path fill=""url(#Z3u55as5xQT0Xm3pZ~tHnh)"" d=""M33.529,33.529L24,24c4,4,4.341,10.751,2,18L33.529,33.529z""/>
  182. <linearGradient id=""Z3u55as5xQT0Xm3pZ~tHni"" x1=""24.517"" x2=""23.183"" y1=""24.517"" y2=""23.183"" gradientUnits=""userSpaceOnUse"">
  183. <stop offset=""0"" stop-color=""#ffd000""/>
  184. <stop offset=""1"" stop-color=""#fede00""/>
  185. </linearGradient>
  186. <circle cx=""24"" cy=""24"" r=""2"" fill=""url(#Z3u55as5xQT0Xm3pZ~tHni)""/>
  187. </svg>
  188. <span style=""font-size: 24px; color: #333; margin-left: 10px;"">LYKNS</span>
  189. <div class=""nav-links"">
  190. <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
  191. <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
  192. <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
  193. <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
  194. </div>
  195. </div>
  196. <div class=""acrylic-panel"">
  197. <div style=""text-align:center;"">
  198. <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
  199. <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Ka"" x1=""12.209"" x2=""34.197"" y1=""42.462"" y2=""7.83"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
  200. <stop offset=""0"" stop-color=""#737b80"" />
  201. <stop offset="".473"" stop-color=""#686f74"" />
  202. <stop offset=""1"" stop-color=""#575c61"" />
  203. </linearGradient>
  204. <path fill=""url(#_EnvA6UXZqPqJP5nSvD1Ka)"" d=""M44,24c0,11.044-8.956,20-20,20S4,35.044,4,24S12.956,4,24,4S44,12.956,44,24z"" />
  205. <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Kb"" x1=""16.956"" x2=""31.044"" y1=""37.138"" y2=""10.642"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
  206. <stop offset=""0"" stop-color=""#ccc"" />
  207. <stop offset="".758"" stop-color=""#f2f2f2"" />
  208. <stop offset=""1"" stop-color=""#fff"" />
  209. </linearGradient>
  210. <path fill=""url(#_EnvA6UXZqPqJP5nSvD1Kb)"" d=""M39,24c0,8.287-6.713,15-15,15S9,32.287,9,24S15.713,9,24,9S39,15.713,39,24z"" />
  211. <linearGradient id=""_EnvA6UXZqPqJP5nSvD1Kc"" x1=""19.774"" x2=""28.226"" y1=""31.838"" y2=""15.942"" gradientTransform=""matrix(1 0 0 -1 0 47.89)"" gradientUnits=""userSpaceOnUse"">
  212. <stop offset=""0"" stop-color=""#21ad64"" />
  213. <stop offset=""1"" stop-color=""#088242"" />
  214. </linearGradient>
  215. <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
  216. </svg>
  217. </div>
  218. <div class=""welcome-text"">
  219. 欢迎使用 LYKNS EdgeVoyager!
  220. </div>
  221. <div class=""search-container"">
  222. <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
  223. <select id=""search-engine-select"" class=""search-engine-select"">
  224. <option value=""bing"">Bing</option>
  225. <option value=""baidu"">百度</option>
  226. <option value=""google"">Google</option>
  227. </select>
  228. <button onclick=""search()"" class=""search-button"">搜索</button>
  229. </div>
  230. </div>
  231. <script>
  232. function search() {
  233. var query = document.getElementById('search-input').value;
  234. var engine = document.getElementById('search-engine-select').value;
  235. var url = '';
  236. if (engine === 'bing') {
  237. url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
  238. } else if (engine === 'baidu') {
  239. url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
  240. } else if (engine === 'google') {
  241. url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
  242. }
  243. window.open(url, '_blank');
  244. }
  245. </script>
  246. </body>
  247. </html>";
  248. webView21.CoreWebView2.NavigateToString(htmlString);
  249. Settings1.Default.firstwindow = false;
  250. Settings1.Default.Save();
  251. }
  252. }
  253. private void back_button_Click(object sender, EventArgs e)
  254. {
  255. webView21.CoreWebView2.GoBack();
  256. }
  257. private void forward_button_Click(object sender, EventArgs e)
  258. {
  259. webView21.CoreWebView2.GoForward();
  260. }
  261. private void refresh_button_Click(object sender, EventArgs e)
  262. {
  263. webView21.CoreWebView2.Reload();
  264. }
  265. private void stop_button_Click(object sender, EventArgs e)
  266. {
  267. webView21.CoreWebView2.Stop();
  268. }
  269. private void search_button_Click(object sender, EventArgs e)
  270. {
  271. if (textBox1.Text.Length > 0)
  272. {
  273. if (comboBox1.Text != "about:blank")
  274. {
  275. Form1 newForm = new Form1();
  276. newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
  277. newForm.Show();
  278. }
  279. else
  280. {
  281. webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
  282. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  283. }
  284. }
  285. }
  286. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  287. {
  288. string currentUrl = webView21.Source.ToString();
  289. if (!comboBox1.Items.Contains(currentUrl))
  290. {
  291. comboBox1.Items.Add(currentUrl);
  292. }
  293. comboBox1.Text = currentUrl;
  294. progressBar.Value = 0;
  295. progressLabel.Text = "0%";
  296. StatusLabel.Text = "开始导航到:" + currentUrl;
  297. }
  298. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  299. {
  300. string currentUrl = webView21.Source.ToString();
  301. if (!comboBox1.Items.Contains(currentUrl))
  302. {
  303. comboBox1.Items.Add(currentUrl);
  304. }
  305. comboBox1.Text = currentUrl;
  306. progressBar.Value = 100;
  307. progressLabel.Text = "100%";
  308. StatusLabel.Text = "完成。";
  309. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  310. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  311. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  312. faviconUrl = faviconUrl.Trim('"');
  313. if (!string.IsNullOrEmpty(faviconUrl))
  314. {
  315. Uri uriResult;
  316. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  317. if (isAbsoluteUri)
  318. {
  319. using (var client = new HttpClient())
  320. {
  321. var faviconData = await client.GetByteArrayAsync(uriResult);
  322. using (var ms = new MemoryStream(faviconData))
  323. {
  324. using (var img = Image.FromStream(ms))
  325. {
  326. if (img is Bitmap bitmap)
  327. {
  328. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  329. }
  330. else
  331. {
  332. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. }
  340. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  341. {
  342. if (e.KeyCode == Keys.Enter)
  343. {
  344. string url = comboBox1.Text;
  345. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  346. {
  347. webView21.CoreWebView2.Navigate(url);
  348. }
  349. else
  350. {
  351. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  352. }
  353. }
  354. }
  355. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  356. {
  357. Environment.Exit(0);
  358. }
  359. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  360. {
  361. progressBar.Value = 50;
  362. progressLabel.Text = "50%";
  363. StatusLabel.Text = "内容开始加载";
  364. }
  365. private async void SetZoomLevel(double zoomLevel)
  366. {
  367. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  368. }
  369. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  370. {
  371. SetZoomLevel(100);
  372. toolStripMenuItem3.Enabled = true;
  373. }
  374. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  375. {
  376. zoom = zoom + 25;
  377. SetZoomLevel(zoom);
  378. toolStripMenuItem3.Enabled = true;
  379. }
  380. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  381. {
  382. zoom = zoom - 25;
  383. SetZoomLevel(zoom);
  384. if (zoom == 25)
  385. toolStripMenuItem3.Enabled = false;
  386. }
  387. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  388. {
  389. webView21.CoreWebView2.Reload();
  390. }
  391. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  392. {
  393. webView21.CoreWebView2.Stop();
  394. }
  395. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  396. {
  397. webView21.CoreWebView2.GoForward();
  398. }
  399. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  400. {
  401. webView21.CoreWebView2.GoBack();
  402. }
  403. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  404. {
  405. StatusLabel.Text = "源地址已更改";
  406. }
  407. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  408. {
  409. string requestUri = e.Request.Uri;
  410. StatusLabel.Text = "请求资源:" + requestUri;
  411. }
  412. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  413. {
  414. if (工具栏ToolStripMenuItem.Checked == true)
  415. {
  416. statusStrip1.Visible = true;
  417. webView21.Height = webView21.Height - 23;
  418. }
  419. else
  420. {
  421. statusStrip1.Visible = false;
  422. webView21.Height = webView21.Height + 23;
  423. }
  424. }
  425. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  426. {
  427. AboutBox1 aboutBox1 = new AboutBox1();
  428. aboutBox1.ShowDialog();
  429. }
  430. private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  431. {
  432. Settings1.Default.firstwindow = true;
  433. Settings1.Default.Save();
  434. Form1 newForm = new Form1();
  435. newForm.Show();
  436. }
  437. private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  438. {
  439. OpenFileDialog openFileDialog = new OpenFileDialog();
  440. openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
  441. if (openFileDialog.ShowDialog() == DialogResult.OK)
  442. {
  443. string filePath = openFileDialog.FileName;
  444. Form1 newForm = new Form1();
  445. newForm.webView21.Source = new Uri("file:///" + filePath);
  446. newForm.Show();
  447. }
  448. }
  449. private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
  450. {
  451. }
  452. private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  453. {
  454. string title = webView21.CoreWebView2.DocumentTitle;
  455. try
  456. {
  457. CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
  458. if (printStatus == CoreWebView2PrintStatus.Succeeded)
  459. {
  460. MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
  461. }
  462. else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
  463. {
  464. MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
  465. }
  466. else
  467. {
  468. MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
  469. }
  470. }
  471. catch (Exception)
  472. {
  473. MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
  474. }
  475. }
  476. private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  477. {
  478. webView21.CoreWebView2.ShowPrintUI();
  479. }
  480. private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
  481. {
  482. string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
  483. Process.Start(new ProcessStartInfo
  484. {
  485. FileName = mailtoLink,
  486. UseShellExecute = true
  487. });
  488. }
  489. private async void 属性PToolStripMenuItem_Click(object sender, EventArgs e)
  490. {
  491. string title = webView21.CoreWebView2.DocumentTitle;
  492. string url = webView21.CoreWebView2.Source;
  493. // 获取网页的字符数和字节数
  494. string content = await GetWebPageContent(url);
  495. int charCount = content.Length;
  496. int byteCount = Encoding.UTF8.GetByteCount(content);
  497. // 获取网页的编码、语言、内容类型
  498. string encoding = "UTF-8"; // 默认值,实际需要从响应头中获取
  499. string language = "zh-CN"; // 默认值,实际需要从响应头中获取
  500. string contentType = "text/html"; // 默认值,实际需要从响应头中获取
  501. // 获取网页的元数据
  502. string author = await GetMetaData("author");
  503. string description = await GetMetaData("description");
  504. string keywords = await GetMetaData("keywords");
  505. string updateDate = await GetMetaData("last-modified");
  506. // 获取网页的安全信息
  507. string securityInfo = await GetSecurityInfo(url);
  508. string securityLevel = "High"; // 默认值,实际需要根据安全证书信息判断
  509. // 获取网页的其他信息
  510. string faviconUrl = await GetFaviconUrl();
  511. string encodingLanguage = "HTML, JavaScript"; // 默认值,实际需要从网页内容中解析
  512. // 显示网页属性
  513. MessageBox.Show(
  514. $"常规信息:\n" +
  515. $"标题:{title}\n" +
  516. $"地址(URL):{url}\n" +
  517. $"编码:{encoding}\n" +
  518. $"语言:{language}\n" +
  519. $"内容类型:{contentType}\n" +
  520. $"\n网页内容信息:\n" +
  521. $"字符数:{charCount}\n" +
  522. $"字节数:{byteCount}\n" +
  523. $"编码方式:HTML, XML\n" +
  524. $"\n网页元数据:\n" +
  525. $"作者:{author}\n" +
  526. $"描述:{description}\n" +
  527. $"关键词:{keywords}\n" +
  528. $"更新日期:{updateDate}\n" +
  529. $"\n网页安全信息:\n" +
  530. $"安全证书信息:{securityInfo}\n" +
  531. $"安全级别:{securityLevel}\n" +
  532. $"\n其他信息:\n" +
  533. $"收藏夹图标:{faviconUrl}\n" +
  534. $"编码语言:{encodingLanguage}",
  535. "网页属性",
  536. MessageBoxButtons.OK,
  537. MessageBoxIcon.Information
  538. );
  539. }
  540. private async Task<string> GetWebPageContent(string url)
  541. {
  542. try
  543. {
  544. HttpResponseMessage response = await httpClient.GetAsync(url);
  545. response.EnsureSuccessStatusCode();
  546. return await response.Content.ReadAsStringAsync();
  547. }
  548. catch (Exception ex)
  549. {
  550. return ex.Message;
  551. }
  552. }
  553. private async Task<string> GetMetaData(string name)
  554. {
  555. try
  556. {
  557. string script = $"document.querySelector('meta[name=\"{name}\"]').content;";
  558. return await webView21.CoreWebView2.ExecuteScriptAsync(script);
  559. }
  560. catch (Exception)
  561. {
  562. return "不可用";
  563. }
  564. }
  565. private async Task<string> GetFaviconUrl()
  566. {
  567. try
  568. {
  569. string script = "Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);";
  570. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  571. return faviconUrl.Trim('"');
  572. }
  573. catch (Exception)
  574. {
  575. return "不可用";
  576. }
  577. }
  578. private async Task<string> GetSecurityInfo(string url)
  579. {
  580. try
  581. {
  582. HttpResponseMessage response = await httpClient.GetAsync(url);
  583. response.EnsureSuccessStatusCode();
  584. if (response.RequestMessage.RequestUri.Scheme == "https")
  585. {
  586. var headers = response.Headers;
  587. if (headers.Contains("Strict-Transport-Security"))
  588. {
  589. return "HSTS已开启";
  590. }
  591. }
  592. return "无HSTS";
  593. }
  594. catch (Exception ex)
  595. {
  596. return ex.Message;
  597. }
  598. }
  599. private void 剪切TToolStripMenuItem_Click(object sender, EventArgs e)
  600. {
  601. if (comboBox1.Focused)
  602. {
  603. if (comboBox1.SelectedText != "")
  604. {
  605. Clipboard.SetText(comboBox1.SelectedText);
  606. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength);
  607. comboBox1.Focus();
  608. }
  609. }
  610. else if (textBox1.Focused)
  611. {
  612. if (textBox1.SelectedText != "")
  613. {
  614. Clipboard.SetText(textBox1.SelectedText);
  615. textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart, textBox1.SelectionLength);
  616. }
  617. }
  618. }
  619. private async void 复制CToolStripMenuItem_Click(object sender, EventArgs e)
  620. {
  621. if (comboBox1.Focused)
  622. {
  623. if (comboBox1.SelectedText != "")
  624. {
  625. Clipboard.SetText(comboBox1.SelectedText);
  626. }
  627. }
  628. else if (textBox1.Focused)
  629. {
  630. if (textBox1.SelectedText != "")
  631. {
  632. Clipboard.SetText(textBox1.SelectedText);
  633. }
  634. }
  635. else if (isWebView2Focused)
  636. {
  637. string script = "window.getSelection().toString();";
  638. string selectedText = await webView21.CoreWebView2.ExecuteScriptAsync(script);
  639. selectedText = selectedText.Trim('"');
  640. if (!string.IsNullOrEmpty(selectedText))
  641. {
  642. Clipboard.SetText(selectedText);
  643. }
  644. }
  645. }
  646. private async void 粘贴PToolStripMenuItem_Click(object sender, EventArgs e)
  647. {
  648. if (comboBox1.Focused)
  649. {
  650. if (comboBox1.SelectionLength > 0)
  651. {
  652. string clipboardText = Clipboard.GetText();
  653. comboBox1.Text = comboBox1.Text.Remove(comboBox1.SelectionStart, comboBox1.SelectionLength) + clipboardText;
  654. comboBox1.SelectionStart = comboBox1.Text.Length;
  655. }
  656. else
  657. {
  658. string clipboardText = Clipboard.GetText();
  659. comboBox1.Text += clipboardText;
  660. comboBox1.SelectionStart = comboBox1.Text.Length;
  661. }
  662. }
  663. else if (textBox1.Focused)
  664. {
  665. textBox1.Paste();
  666. }
  667. }
  668. private async void 全选AToolStripMenuItem_Click(object sender, EventArgs e)
  669. {
  670. if (comboBox1.Focused)
  671. {
  672. comboBox1.SelectAll();
  673. }
  674. else if (textBox1.Focused)
  675. {
  676. textBox1.SelectAll();
  677. }
  678. else
  679. {
  680. string script = "document.execCommand('selectAll', false, null);";
  681. await webView21.CoreWebView2.ExecuteScriptAsync(script);
  682. }
  683. }
  684. private void 查找FToolStripMenuItem_Click(object sender, EventArgs e)
  685. {
  686. }
  687. private async void 添加到收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
  688. {
  689. string resxFilePath = "favoritesMenuItem.resx";
  690. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  691. string pageTitle = title.Trim('"');
  692. string pageUrl = webView21.CoreWebView2.Source.ToString();
  693. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("var link = document.querySelector('link[rel~=\"icon\"]') || document.querySelector('link[rel~=\"shortcut icon\"]');link ? link.href : '';");
  694. byte[] iconData = await DownloadFaviconAsync(faviconUrl);
  695. bool titleExists = false;
  696. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  697. {
  698. foreach (DictionaryEntry entry in reader)
  699. {
  700. if (entry.Key.ToString() == pageTitle)
  701. {
  702. titleExists = true;
  703. break;
  704. }
  705. }
  706. }
  707. if (!titleExists)
  708. {
  709. using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
  710. {
  711. writer.AddResource(pageTitle, pageUrl);
  712. writer.Generate();
  713. }
  714. }
  715. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  716. {
  717. foreach (DictionaryEntry entry in reader)
  718. {
  719. if (entry.Key.ToString() == pageTitle)
  720. {
  721. titleExists = true;
  722. break;
  723. }
  724. }
  725. }
  726. if (!titleExists)
  727. {
  728. using (ResXResourceWriter writer = new ResXResourceWriter(resxFilePath))
  729. {
  730. writer.AddResource(pageTitle, iconData);
  731. writer.Generate();
  732. }
  733. }
  734. // 1. 清空“收藏夹AToolStripMenuItem”下的菜单中分割线下方的所有按钮
  735. ToolStripItem[] itemsToRemove = 收藏夹AToolStripMenuItem.DropDownItems.Cast<ToolStripItem>()
  736. .SkipWhile(item => item != 收藏夹AToolStripMenuItem.DropDownItems["toolStripSeparator1"])
  737. .ToArray();
  738. foreach (var item in itemsToRemove)
  739. {
  740. 收藏夹AToolStripMenuItem.DropDownItems.Remove(item);
  741. }
  742. // 2. 从 favoritesMenuItem.resx 文件中读取所有字符串资源
  743. using (ResXResourceReader reader = new ResXResourceReader(resxFilePath))
  744. {
  745. foreach (DictionaryEntry entry in reader)
  746. {
  747. string resourceName = entry.Key.ToString();
  748. string resourceValue = entry.Value.ToString();
  749. // 3. 为每个字符串资源创建一个新的 ToolStripMenuItem
  750. ToolStripMenuItem menuItem = new ToolStripMenuItem
  751. {
  752. Text = resourceName,
  753. Tag = resourceValue // 你可以将资源值存储在 Tag 属性中,方便后续使用
  754. };
  755. // 4. 为每个按钮添加点击事件处理程序
  756. menuItem.Click += MenuItem_Click;
  757. // 5. 从 favoritesMenuItemIcon.resx 文件中读取对应的图标
  758. string iconResxFilePath = "favoritesMenuItemIcon.resx";
  759. using (ResXResourceReader iconReader = new ResXResourceReader(iconResxFilePath))
  760. {
  761. foreach (DictionaryEntry iconEntry in iconReader)
  762. {
  763. if (iconEntry.Key.ToString() == resourceName)
  764. {
  765. using (MemoryStream ms = new MemoryStream(iconData))
  766. {
  767. Image iconImage = Image.FromStream(ms);
  768. menuItem.Image = iconImage;
  769. }
  770. break;
  771. }
  772. }
  773. }
  774. // 6. 将新的 ToolStripMenuItem 添加到“收藏夹AToolStripMenuItem”下
  775. 收藏夹AToolStripMenuItem.DropDownItems.Add(menuItem);
  776. }
  777. }
  778. }
  779. private async Task<byte[]> DownloadFaviconAsync(string faviconUrl)
  780. {
  781. using (HttpClient client = new HttpClient())
  782. {
  783. try
  784. {
  785. return await client.GetByteArrayAsync(faviconUrl);
  786. }
  787. catch (Exception ex)
  788. {
  789. return null; // 返回 null 或默认图标数据
  790. }
  791. }
  792. }
  793. private void MenuItem_Click(object sender, EventArgs e)
  794. {
  795. // 获取点击的按钮
  796. ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
  797. // 获取按钮的 Tag 属性,即资源值(URL)
  798. string url = (string)menuItem.Tag;
  799. // 令 WebView2 导航至该地址
  800. webView21.CoreWebView2.Navigate(url);
  801. }
  802. }
  803. }