Form1.cs 17 KB

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