Form1.cs 29 KB

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