Form1.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. if (textBox1.Text.Length > 0)
  174. {
  175. webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
  176. webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
  177. }
  178. }
  179. private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
  180. {
  181. string currentUrl = webView21.Source.ToString();
  182. if (!comboBox1.Items.Contains(currentUrl))
  183. {
  184. comboBox1.Items.Add(currentUrl);
  185. }
  186. comboBox1.Text = currentUrl;
  187. progressBar.Value = 0;
  188. progressLabel.Text = "0%";
  189. StatusLabel.Text = "开始导航到:" + currentUrl;
  190. }
  191. private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
  192. {
  193. string currentUrl = webView21.Source.ToString();
  194. if (!comboBox1.Items.Contains(currentUrl))
  195. {
  196. comboBox1.Items.Add(currentUrl);
  197. }
  198. comboBox1.Text = currentUrl;
  199. progressBar.Value = 100;
  200. progressLabel.Text = "100%";
  201. StatusLabel.Text = "完成。";
  202. string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
  203. this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
  204. string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
  205. faviconUrl = faviconUrl.Trim('"');
  206. if (!string.IsNullOrEmpty(faviconUrl))
  207. {
  208. Uri uriResult;
  209. bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
  210. if (isAbsoluteUri)
  211. {
  212. using (var client = new HttpClient())
  213. {
  214. var faviconData = await client.GetByteArrayAsync(uriResult);
  215. using (var ms = new MemoryStream(faviconData))
  216. {
  217. using (var img = Image.FromStream(ms))
  218. {
  219. if (img is Bitmap bitmap)
  220. {
  221. this.Icon = Icon.FromHandle(bitmap.GetHicon());
  222. }
  223. else
  224. {
  225. this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
  226. }
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. private void comboBox1_KeyDown(object sender, KeyEventArgs e)
  234. {
  235. if (e.KeyCode == Keys.Enter)
  236. {
  237. string url = comboBox1.Text;
  238. if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
  239. {
  240. webView21.CoreWebView2.Navigate(url);
  241. }
  242. else
  243. {
  244. MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  245. }
  246. }
  247. }
  248. private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
  249. {
  250. Environment.Exit(0);
  251. }
  252. private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
  253. {
  254. progressBar.Value = 50;
  255. progressLabel.Text = "50%";
  256. StatusLabel.Text = "内容开始加载";
  257. }
  258. private async void SetZoomLevel(double zoomLevel)
  259. {
  260. await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
  261. }
  262. private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
  263. {
  264. SetZoomLevel(100);
  265. toolStripMenuItem3.Enabled = true;
  266. }
  267. private void toolStripMenuItem2_Click(object sender, EventArgs e)
  268. {
  269. zoom = zoom + 25;
  270. SetZoomLevel(zoom);
  271. toolStripMenuItem3.Enabled = true;
  272. }
  273. private void toolStripMenuItem3_Click(object sender, EventArgs e)
  274. {
  275. zoom = zoom - 25;
  276. SetZoomLevel(zoom);
  277. if (zoom == 25)
  278. toolStripMenuItem3.Enabled = false;
  279. }
  280. private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
  281. {
  282. webView21.CoreWebView2.Reload();
  283. }
  284. private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
  285. {
  286. webView21.CoreWebView2.Stop();
  287. }
  288. private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
  289. {
  290. webView21.CoreWebView2.GoForward();
  291. }
  292. private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
  293. {
  294. webView21.CoreWebView2.GoBack();
  295. }
  296. private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
  297. {
  298. StatusLabel.Text = "源地址已更改";
  299. }
  300. private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
  301. {
  302. string requestUri = e.Request.Uri;
  303. StatusLabel.Text = "请求资源:" + requestUri;
  304. }
  305. private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
  306. {
  307. if (工具栏ToolStripMenuItem.Checked == true)
  308. {
  309. statusStrip1.Visible = true;
  310. webView21.Height = webView21.Height - 23;
  311. }
  312. else
  313. {
  314. statusStrip1.Visible = false;
  315. webView21.Height = webView21.Height + 23;
  316. }
  317. }
  318. private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
  319. {
  320. AboutBox1 aboutBox1 = new AboutBox1();
  321. aboutBox1.ShowDialog();
  322. }
  323. }
  324. }