Form1.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.NewWindowRequested += CoreWebView2_NewWindowRequested;
  23. if (Settings1.Default.firstwindow == true)
  24. {
  25. //webView21.Source = new Uri(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html", "index.html"));
  26. string htmlString = @"<!DOCTYPE html>
  27. <html lang=""zh-CN"">
  28. <head>
  29. <meta charset=""UTF-8"">
  30. <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
  31. <title>起始页</title>
  32. <style>
  33. body {
  34. margin: 0;
  35. padding: 0;
  36. background-image: url('https://bing.img.run/uhd.php');
  37. background-size: cover;
  38. background-repeat: no-repeat;
  39. background-attachment: fixed;
  40. font-family: Arial, sans-serif;
  41. }
  42. .header {
  43. background-color: rgba(255, 255, 255, 0.8); /* 磨砂玻璃效果 */
  44. backdrop-filter: blur(10px);
  45. padding: 10px 20px;
  46. display: flex;
  47. align-items: center;
  48. position: relative;
  49. box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  50. }
  51. .logo {
  52. width: auto;
  53. height: auto;
  54. }
  55. .nav-item {
  56. margin-left: 20px;
  57. color: #333;
  58. text-decoration: none;
  59. font-size: 16px;
  60. }
  61. .nav-links {
  62. display: flex;
  63. align-items: center;
  64. }
  65. .welcome-text {
  66. text-align: center;
  67. margin: 20px 0;
  68. font-size: 24px; /* 加大字体 */
  69. font-weight: bold; /* 加粗字体 */
  70. color: #0078D4; /* 颜色设置为0078D4 */
  71. }
  72. .search-container {
  73. display: flex;
  74. justify-content: center;
  75. align-items: center;
  76. margin-top: 20px;
  77. border-radius: 15px; /* 搜索条圆角 */
  78. overflow: hidden; /* 隐藏溢出部分,确保圆角效果 */
  79. }
  80. .search-input {
  81. width: 70vw; /* 搜索框宽度为视口宽度的85% */
  82. max-width: 1000px; /* 搜索框最大宽度不超过1000px */
  83. padding: 10px;
  84. font-size: 16px;
  85. border: none;
  86. border-radius: 15px 0 0 15px; /* 搜索框左圆角 */
  87. height: 20px; /* 设置高度 */
  88. }
  89. .search-button {
  90. padding: 10px 20px;
  91. font-size: 16px;
  92. cursor: pointer;
  93. border: none;
  94. background-color: #0078D4;
  95. color: white;
  96. border-radius: 0 15px 15px 0; /* 搜索按钮右圆角 */
  97. height: 40px; /* 设置高度 */
  98. }
  99. .acrylic-panel {
  100. background-color: rgba(255, 255, 255, 0.8); /* 磨砂玻璃效果 */
  101. backdrop-filter: blur(10px);
  102. padding: 20px;
  103. border-radius: 8px;
  104. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
  105. margin: 20px 0;
  106. width: 90vw; /* 面板宽度为视口宽度的90% */
  107. margin-left: auto;
  108. margin-right: auto;
  109. max-width: 1200px; /* 面板最大宽度不超过1200px */
  110. }
  111. @media (max-width: 600px) {
  112. .search-button {
  113. padding: 8px 16px; /* 在小屏幕设备上调整按钮内边距 */
  114. }
  115. }
  116. </style>
  117. </head>
  118. <body>
  119. <div class=""header"">
  120. <img src=""https://www.lykns.com.cn/wp-content/uploads/2025/01/pinwheel_48px.png"" alt=""Logo"" class=""logo"">
  121. <span style=""font-size: 24px; color: #333; margin-left: 0;"">LYKNS</span>
  122. <div class=""nav-links"">
  123. <a href=""https://www.lykns.com.cn"" class=""nav-item"">LYKNS 主站</a>
  124. <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
  125. <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
  126. <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
  127. </div>
  128. </div>
  129. <div class=""acrylic-panel"">
  130. <div class=""welcome-text"">
  131. 欢迎使用LYKNS EdgeVoyager!
  132. </div>
  133. <div class=""search-container"">
  134. <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
  135. <button onclick=""search()"" class=""search-button"">搜索</button>
  136. </div>
  137. </div>
  138. <script>
  139. function search() {
  140. var query = document.getElementById('search-input').value;
  141. window.location.href = 'https://cn.bing.com/search?q=' + encodeURIComponent(query);
  142. }
  143. </script>
  144. </body>
  145. </html>";
  146. webView21.CoreWebView2.NavigateToString(htmlString);
  147. Settings1.Default.firstwindow = false;
  148. Settings1.Default.Save();
  149. }
  150. }
  151. private void back_button_Click(object sender, EventArgs e)
  152. {
  153. webView21.CoreWebView2.GoBack();
  154. }
  155. private void forward_button_Click(object sender, EventArgs e)
  156. {
  157. webView21.CoreWebView2.GoForward();
  158. }
  159. private void refresh_button_Click(object sender, EventArgs e)
  160. {
  161. webView21.CoreWebView2.Reload();
  162. }
  163. private void stop_button_Click(object sender, EventArgs e)
  164. {
  165. webView21.CoreWebView2.Stop();
  166. }
  167. private void search_button_Click(object sender, EventArgs e)
  168. {
  169. }
  170. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  171. {
  172. string currentUrl = webView21.Source.ToString();
  173. if (!comboBox1.Items.Contains(currentUrl))
  174. {
  175. comboBox1.Items.Add(currentUrl);
  176. }
  177. comboBox1.Text = currentUrl;
  178. progressBar.Value = 0;
  179. progressLabel.Text = "0%";
  180. StatusLabel.Text = "开始导航到:" + currentUrl;
  181. }
  182. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  183. {
  184. string currentUrl = webView21.Source.ToString();
  185. if (!comboBox1.Items.Contains(currentUrl))
  186. {
  187. comboBox1.Items.Add(currentUrl);
  188. }
  189. comboBox1.Text = currentUrl;
  190. progressBar.Value = 100;
  191. progressLabel.Text = "100%";
  192. StatusLabel.Text = "完成。";
  193. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  194. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  195. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  196. faviconUrl = faviconUrl.Trim('"');
  197. if (!string.IsNullOrEmpty(faviconUrl))
  198. {
  199. Uri uriResult;
  200. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  201. if (isAbsoluteUri)
  202. {
  203. using (var client = new HttpClient())
  204. {
  205. var faviconData = await client.GetByteArrayAsync(uriResult);
  206. using (var ms = new MemoryStream(faviconData))
  207. {
  208. using (var img = Image.FromStream(ms))
  209. {
  210. if (img is Bitmap bitmap)
  211. {
  212. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  213. }
  214. else
  215. {
  216. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  225. {
  226. if (e.KeyCode == Keys.Enter)
  227. {
  228. string url = comboBox1.Text;
  229. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  230. {
  231. webView21.CoreWebView2.Navigate(url);
  232. }
  233. else
  234. {
  235. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  236. }
  237. }
  238. }
  239. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  240. {
  241. Environment.Exit(0);
  242. }
  243. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  244. {
  245. progressBar.Value = 50;
  246. progressLabel.Text = "50%";
  247. StatusLabel.Text = "内容开始加载";
  248. }
  249. private async void SetZoomLevel(double zoomLevel)
  250. {
  251. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  252. }
  253. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  254. {
  255. SetZoomLevel(100);
  256. toolStripMenuItem3.Enabled = true;
  257. }
  258. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  259. {
  260. zoom = zoom + 25;
  261. SetZoomLevel(zoom);
  262. toolStripMenuItem3.Enabled = true;
  263. }
  264. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  265. {
  266. zoom = zoom - 25;
  267. SetZoomLevel(zoom);
  268. if (zoom == 25)
  269. toolStripMenuItem3.Enabled = false;
  270. }
  271. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  272. {
  273. webView21.CoreWebView2.Reload();
  274. }
  275. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  276. {
  277. webView21.CoreWebView2.Stop();
  278. }
  279. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  280. {
  281. webView21.CoreWebView2.GoForward();
  282. }
  283. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  284. {
  285. webView21.CoreWebView2.GoBack();
  286. }
  287. }
  288. }