Form1.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System.Diagnostics;
  2. using Microsoft.Web.WebView2.Core;
  3. namespace EdgeVoyager
  4. {
  5. public partial class Form1 : Form
  6. {
  7. int zoom = 100;
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. InitializeAsync();
  12. }
  13. private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
  14. {
  15. Form1 newForm = new Form1();
  16. newForm.webView21.Source = new Uri(e.Uri);
  17. newForm.Show();
  18. e.Handled = true;
  19. }
  20. async void InitializeAsync()
  21. {
  22. await webView21.EnsureCoreWebView2Async(null);
  23. webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
  24. await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
  25. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  26. webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
  27. if (Settings1.Default.firstwindow == true)
  28. {
  29. string htmlString = @"<!DOCTYPE html>
  30. <html lang=""zh-CN"">
  31. <head>
  32. <meta charset=""UTF-8"">
  33. <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
  34. <title>起始页</title>
  35. <style>
  36. body {
  37. margin: 0;
  38. padding: 0;
  39. background-image: url('https://bing.img.run/uhd.php');
  40. background-size: cover;
  41. background-repeat: no-repeat;
  42. background-attachment: fixed;
  43. font-family: Arial, sans-serif;
  44. }
  45. .header {
  46. background-color: rgba(255, 255, 255, 0.8);
  47. backdrop-filter: blur(10px);
  48. padding: 10px 20px;
  49. display: flex;
  50. align-items: center;
  51. position: relative;
  52. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  53. }
  54. .logo {
  55. width: auto;
  56. height: auto;
  57. }
  58. .nav-item {
  59. margin-left: 20px;
  60. color: #333;
  61. text-decoration: none;
  62. font-size: 16px;
  63. }
  64. .nav-links {
  65. display: flex;
  66. align-items: center;
  67. }
  68. .welcome-text {
  69. text-align: center;
  70. margin: 20px 0;
  71. font-size: 36px;
  72. font-weight: bold;
  73. color: #0078D4;
  74. }
  75. .search-container {
  76. display: flex;
  77. justify-content: center;
  78. align-items: center;
  79. margin-top: 20px;
  80. border-radius: 15px;
  81. overflow: hidden;
  82. }
  83. .search-input {
  84. width: 70vw;
  85. max-width: 1000px;
  86. padding: 10px;
  87. font-size: 16px;
  88. border: none;
  89. border-radius: 15px 0 0 15px;
  90. height: 20px;
  91. }
  92. .search-button {
  93. padding: 10px 20px;
  94. width: 80px;
  95. font-size: 16px;
  96. cursor: pointer;
  97. border: none;
  98. background-color: #0078D4;
  99. color: white;
  100. border-radius: 0 15px 15px 0;
  101. height: 40px;
  102. }
  103. .search-engine-select {
  104. margin-left: 0px;
  105. padding: 10px;
  106. font-size: 16px;
  107. border: none;
  108. border-radius: 0px;
  109. background-color: #f2f2f2;
  110. cursor: pointer;
  111. }
  112. .acrylic-panel {
  113. background-color: rgba(255, 255, 255, 0.8);
  114. backdrop-filter: blur(10px);
  115. padding: 20px;
  116. border-radius: 8px;
  117. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  118. margin: 20px 0;
  119. width: 90vw;
  120. margin-left: auto;
  121. margin-right: auto;
  122. max-width: 1200px;
  123. }
  124. @media (max-width: 600px) {
  125. .search-button {
  126. padding: 8px 16px;
  127. }
  128. }
  129. </style>
  130. </head>
  131. <body>
  132. <div class=""header"">
  133. <img src=""https://www.lykns.com.cn/wp-content/uploads/2025/01/pinwheel_48px.png"" alt=""Logo"" class=""logo"">
  134. <span style=""font-size: 24px; color: #333; margin-left: 0;"">LYKNS </span>
  135. <div class=""nav-links"">
  136. <a href=""https://www.lykns.com.cn"" class=""nav-item"">主站</a>
  137. <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
  138. <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
  139. <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
  140. </div>
  141. </div>
  142. <div class=""acrylic-panel"">
  143. <div style=""text-align:center;"">
  144. <?xml version=""1.0"" encoding=""utf-8""?>
  145. <svg xmlns=""http://www.w3.org/2000/svg"" viewBox=""0 0 48 48"" width=""128"" height=""128"">
  146. <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"">
  147. <stop offset=""0"" stop-color=""#737b80"" />
  148. <stop offset="".473"" stop-color=""#686f74"" />
  149. <stop offset=""1"" stop-color=""#575c61"" />
  150. </linearGradient>
  151. <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"" />
  152. <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"">
  153. <stop offset=""0"" stop-color=""#ccc"" />
  154. <stop offset="".758"" stop-color=""#f2f2f2"" />
  155. <stop offset=""1"" stop-color=""#fff"" />
  156. </linearGradient>
  157. <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"" />
  158. <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"">
  159. <stop offset=""0"" stop-color=""#21ad64"" />
  160. <stop offset=""1"" stop-color=""#088242"" />
  161. </linearGradient>
  162. <circle cx=""24"" cy=""24"" r=""9"" fill=""url(#_EnvA6UXZqPqJP5nSvD1Kc)"" />
  163. </svg>
  164. </div>
  165. <div class=""welcome-text"">
  166. 欢迎使用 LYKNS EdgeVoyager!
  167. </div>
  168. <div class=""search-container"">
  169. <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
  170. <select id=""search-engine-select"" class=""search-engine-select"">
  171. <option value=""bing"">Bing</option>
  172. <option value=""baidu"">百度</option>
  173. <option value=""google"">Google</option>
  174. </select>
  175. <button onclick=""search()"" class=""search-button"">搜索</button>
  176. </div>
  177. </div>
  178. <script>
  179. function search() {
  180. var query = document.getElementById('search-input').value;
  181. var engine = document.getElementById('search-engine-select').value;
  182. var url = '';
  183. if (engine === 'bing') {
  184. url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
  185. } else if (engine === 'baidu') {
  186. url = 'https://www.baidu.com/s?wd=' + encodeURIComponent(query);
  187. } else if (engine === 'google') {
  188. url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
  189. }";
  190. webView21.CoreWebView2.NavigateToString(htmlString);
  191. Settings1.Default.firstwindow = false;
  192. Settings1.Default.Save();
  193. }
  194. }
  195. private void back_button_Click(object sender, EventArgs e)
  196. {
  197. webView21.CoreWebView2.GoBack();
  198. }
  199. private void forward_button_Click(object sender, EventArgs e)
  200. {
  201. webView21.CoreWebView2.GoForward();
  202. }
  203. private void refresh_button_Click(object sender, EventArgs e)
  204. {
  205. webView21.CoreWebView2.Reload();
  206. }
  207. private void stop_button_Click(object sender, EventArgs e)
  208. {
  209. webView21.CoreWebView2.Stop();
  210. }
  211. private void search_button_Click(object sender, EventArgs e)
  212. {
  213. if (textBox1.Text.Length > 0)
  214. {
  215. if (comboBox1.Text != "about:blank")
  216. {
  217. Form1 newForm = new Form1();
  218. newForm.webView21.Source = new Uri("https://cn.bing.com/search?q=" + textBox1.Text);
  219. newForm.Show();
  220. }
  221. else
  222. {
  223. webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
  224. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  225. }
  226. }
  227. }
  228. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  229. {
  230. string currentUrl = webView21.Source.ToString();
  231. if (!comboBox1.Items.Contains(currentUrl))
  232. {
  233. comboBox1.Items.Add(currentUrl);
  234. }
  235. comboBox1.Text = currentUrl;
  236. progressBar.Value = 0;
  237. progressLabel.Text = "0%";
  238. StatusLabel.Text = "开始导航到:" + currentUrl;
  239. }
  240. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  241. {
  242. string currentUrl = webView21.Source.ToString();
  243. if (!comboBox1.Items.Contains(currentUrl))
  244. {
  245. comboBox1.Items.Add(currentUrl);
  246. }
  247. comboBox1.Text = currentUrl;
  248. progressBar.Value = 100;
  249. progressLabel.Text = "100%";
  250. StatusLabel.Text = "完成。";
  251. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  252. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  253. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  254. faviconUrl = faviconUrl.Trim('"');
  255. if (!string.IsNullOrEmpty(faviconUrl))
  256. {
  257. Uri uriResult;
  258. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  259. if (isAbsoluteUri)
  260. {
  261. using (var client = new HttpClient())
  262. {
  263. var faviconData = await client.GetByteArrayAsync(uriResult);
  264. using (var ms = new MemoryStream(faviconData))
  265. {
  266. using (var img = Image.FromStream(ms))
  267. {
  268. if (img is Bitmap bitmap)
  269. {
  270. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  271. }
  272. else
  273. {
  274. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  275. }
  276. }
  277. }
  278. }
  279. }
  280. }
  281. }
  282. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  283. {
  284. if (e.KeyCode == Keys.Enter)
  285. {
  286. string url = comboBox1.Text;
  287. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  288. {
  289. webView21.CoreWebView2.Navigate(url);
  290. }
  291. else
  292. {
  293. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  294. }
  295. }
  296. }
  297. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  298. {
  299. Environment.Exit(0);
  300. }
  301. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  302. {
  303. progressBar.Value = 50;
  304. progressLabel.Text = "50%";
  305. StatusLabel.Text = "内容开始加载";
  306. }
  307. private async void SetZoomLevel(double zoomLevel)
  308. {
  309. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  310. }
  311. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  312. {
  313. SetZoomLevel(100);
  314. toolStripMenuItem3.Enabled = true;
  315. }
  316. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  317. {
  318. zoom = zoom + 25;
  319. SetZoomLevel(zoom);
  320. toolStripMenuItem3.Enabled = true;
  321. }
  322. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  323. {
  324. zoom = zoom - 25;
  325. SetZoomLevel(zoom);
  326. if (zoom == 25)
  327. toolStripMenuItem3.Enabled = false;
  328. }
  329. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  330. {
  331. webView21.CoreWebView2.Reload();
  332. }
  333. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  334. {
  335. webView21.CoreWebView2.Stop();
  336. }
  337. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  338. {
  339. webView21.CoreWebView2.GoForward();
  340. }
  341. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  342. {
  343. webView21.CoreWebView2.GoBack();
  344. }
  345. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  346. {
  347. StatusLabel.Text = "源地址已更改";
  348. }
  349. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  350. {
  351. string requestUri = e.Request.Uri;
  352. StatusLabel.Text = "请求资源:" + requestUri;
  353. }
  354. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  355. {
  356. if (工具栏ToolStripMenuItem.Checked == true)
  357. {
  358. statusStrip1.Visible = true;
  359. webView21.Height = webView21.Height - 23;
  360. }
  361. else
  362. {
  363. statusStrip1.Visible = false;
  364. webView21.Height = webView21.Height + 23;
  365. }
  366. }
  367. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  368. {
  369. AboutBox1 aboutBox1 = new AboutBox1();
  370. aboutBox1.ShowDialog();
  371. }
  372. private void 新建NToolStripMenuItem_Click(object sender, EventArgs e)
  373. {
  374. Settings1.Default.firstwindow = true;
  375. Settings1.Default.Save();
  376. Form1 newForm = new Form1();
  377. newForm.Show();
  378. }
  379. private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
  380. {
  381. OpenFileDialog openFileDialog = new OpenFileDialog();
  382. openFileDialog.Filter = "网页文件 (*.html;*.htm)|*.html;*.htm|可缩放矢量图形文件 (*.svg)|*.svg|所有文件 (*.*)|*.*";
  383. if (openFileDialog.ShowDialog() == DialogResult.OK)
  384. {
  385. string filePath = openFileDialog.FileName;
  386. Form1 newForm = new Form1();
  387. newForm.webView21.Source = new Uri("file:///" + filePath);
  388. newForm.Show();
  389. }
  390. }
  391. private async void 保存SToolStripMenuItem_ClickAsync(object sender, EventArgs e)
  392. {
  393. }
  394. private async void 打印PToolStripMenuItem_Click(object sender, EventArgs e)
  395. {
  396. string title = webView21.CoreWebView2.DocumentTitle;
  397. try
  398. {
  399. CoreWebView2PrintStatus printStatus = await webView21.CoreWebView2.PrintAsync(null);
  400. if (printStatus == CoreWebView2PrintStatus.Succeeded)
  401. {
  402. MessageBox.Show(this, "打印 " + title + " 文档到打印机成功", "打印到默认打印机");
  403. }
  404. else if (printStatus == CoreWebView2PrintStatus.PrinterUnavailable)
  405. {
  406. MessageBox.Show(this, "打印机不可用,离线或错误状态", "打印到默认打印机");
  407. }
  408. else
  409. {
  410. MessageBox.Show(this, "打印 " + title + " 文档到打印机失败", "打印到默认打印机");
  411. }
  412. }
  413. catch (Exception)
  414. {
  415. MessageBox.Show(this, "打印 " + title + " 文档已在进行中", "打印到默认打印机");
  416. }
  417. }
  418. private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)
  419. {
  420. webView21.CoreWebView2.ShowPrintUI();
  421. }
  422. private void 发送SToolStripMenuItem_Click(object sender, EventArgs e)
  423. {
  424. string mailtoLink = $"mailto:?subject=共享网页&body=请查看这个网页:" + comboBox1.Text;
  425. Process.Start(new ProcessStartInfo
  426. {
  427. FileName = mailtoLink,
  428. UseShellExecute = true
  429. });
  430. }
  431. }
  432. }