Form1.cs 11 KB

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