Form1.cs 17 KB

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