123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- using System.Web;
- using Microsoft.Web.WebView2.Core;
- namespace EdgeVoyager
- {
- public partial class Form1 : Form
- {
- int zoom = 100;
- public Form1()
- {
- InitializeComponent();
- InitializeAsync();
- }
- private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
- {
- Form1 newForm = new Form1();
- newForm.webView21.Source = new Uri(e.Uri);
- newForm.Show();
- e.Handled = true;
- }
- async void InitializeAsync()
- {
- await webView21.EnsureCoreWebView2Async(null);
- webView21.CoreWebView2.WebResourceRequested += WebView2_WebResourceRequested;
- await webView21.CoreWebView2.ExecuteScriptAsync("window.onload=function(){window.chrome.webview.postMessage('100');};");
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
- if (Settings1.Default.firstwindow == true)
- {
- //webView21.Source = new Uri(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "html", "index.html"));
- string htmlString = @"<!DOCTYPE html>
- <html lang=""zh-CN"">
- <head>
- <meta charset=""UTF-8"">
- <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
- <title>起始页</title>
- <style>
- body {
- margin: 0;
- padding: 0;
- background-image: url('https://bing.img.run/uhd.php');
- background-size: cover;
- background-repeat: no-repeat;
- background-attachment: fixed;
- font-family: Arial, sans-serif;
- }
- .header {
- background-color: rgba(255, 255, 255, 0.8); /* 磨砂玻璃效果 */
- backdrop-filter: blur(10px);
- padding: 10px 20px;
- display: flex;
- align-items: center;
- position: relative;
- box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
- }
- .logo {
- width: auto;
- height: auto;
- }
- .nav-item {
- margin-left: 20px;
- color: #333;
- text-decoration: none;
- font-size: 16px;
- }
- .nav-links {
- display: flex;
- align-items: center;
- }
- .welcome-text {
- text-align: center;
- margin: 20px 0;
- font-size: 36px; /* 加大字体 */
- font-weight: bold; /* 加粗字体 */
- color: #0078D4; /* 颜色设置为0078D4 */
- }
- .search-container {
- display: flex;
- justify-content: center;
- align-items: center;
- margin-top: 20px;
- border-radius: 15px; /* 搜索条圆角 */
- overflow: hidden; /* 隐藏溢出部分,确保圆角效果 */
- }
- .search-input {
- width: 70vw; /* 搜索框宽度为视口宽度的85% */
- max-width: 1000px; /* 搜索框最大宽度不超过1000px */
- padding: 10px;
- font-size: 16px;
- border: none;
- border-radius: 15px 0 0 15px; /* 搜索框左圆角 */
- height: 20px; /* 设置高度 */
- }
- .search-button {
- padding: 10px 20px;
- font-size: 16px;
- cursor: pointer;
- border: none;
- background-color: #0078D4;
- color: white;
- border-radius: 0 15px 15px 0; /* 搜索按钮右圆角 */
- height: 40px; /* 设置高度 */
- }
- .acrylic-panel {
- background-color: rgba(255, 255, 255, 0.8); /* 磨砂玻璃效果 */
- backdrop-filter: blur(10px);
- padding: 20px;
- border-radius: 8px;
- box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
- margin: 20px 0;
- width: 90vw; /* 面板宽度为视口宽度的90% */
- margin-left: auto;
- margin-right: auto;
- max-width: 1200px; /* 面板最大宽度不超过1200px */
- }
- @media (max-width: 600px) {
- .search-button {
- padding: 8px 16px; /* 在小屏幕设备上调整按钮内边距 */
- }
- }
- </style>
- </head>
- <body>
- <div class=""header"">
- <img src=""https://www.lykns.com.cn/wp-content/uploads/2025/01/pinwheel_48px.png"" alt=""Logo"" class=""logo"">
- <span style=""font-size: 24px; color: #333; margin-left: 0;"">LYKNS </span>
- <div class=""nav-links"">
- <a href=""https://www.lykns.com.cn"" class=""nav-item""> 主站</a>
- <a href=""https://its.lykns.com.cn"" class=""nav-item"">云服务</a>
- <a href=""https://dev.lykns.com.cn"" class=""nav-item"">Git 服务</a>
- <a href=""https://survey.lykns.com.cn"" class=""nav-item"">问卷考试系统</a>
- </div>
- </div>
- <div class=""acrylic-panel"">
- <div class=""welcome-text"">
- 欢迎使用LYKNS EdgeVoyager!
- </div>
- <div class=""search-container"">
- <input type=""text"" id=""search-input"" class=""search-input"" placeholder=""搜索..."">
- <button onclick=""search()"" class=""search-button"">搜索</button>
- </div>
- </div>
- <script>
- function search() {
- var query = document.getElementById('search-input').value;
- window.location.href = 'https://cn.bing.com/search?q=' + encodeURIComponent(query);
- }
- </script>
- </body>
- </html>";
- webView21.CoreWebView2.NavigateToString(htmlString);
- Settings1.Default.firstwindow = false;
- Settings1.Default.Save();
- }
- }
- private void back_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void forward_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void refresh_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void stop_button_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void search_button_Click(object sender, EventArgs e)
- {
- if (textBox1.Text.Length > 0)
- {
- webView21.CoreWebView2.Navigate("https://cn.bing.com/search?q=" + textBox1.Text);
- webView21.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
- }
- }
- private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
- {
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- {
- comboBox1.Items.Add(currentUrl);
- }
- comboBox1.Text = currentUrl;
- progressBar.Value = 0;
- progressLabel.Text = "0%";
- StatusLabel.Text = "开始导航到:" + currentUrl;
- }
- private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
- {
- string currentUrl = webView21.Source.ToString();
- if (!comboBox1.Items.Contains(currentUrl))
- {
- comboBox1.Items.Add(currentUrl);
- }
- comboBox1.Text = currentUrl;
- progressBar.Value = 100;
- progressLabel.Text = "100%";
- StatusLabel.Text = "完成。";
- string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
- this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
- string faviconUrl = await webView21.CoreWebView2.ExecuteScriptAsync("Array.from(document.querySelectorAll('link[rel=\"icon\"], link[rel=\"shortcut icon\"]')).map(link => link.href).find(url => url);");
- faviconUrl = faviconUrl.Trim('"');
- if (!string.IsNullOrEmpty(faviconUrl))
- {
- Uri uriResult;
- bool isAbsoluteUri = Uri.TryCreate(faviconUrl, UriKind.Absolute, out uriResult);
- if (isAbsoluteUri)
- {
- using (var client = new HttpClient())
- {
- var faviconData = await client.GetByteArrayAsync(uriResult);
- using (var ms = new MemoryStream(faviconData))
- {
- using (var img = Image.FromStream(ms))
- {
- if (img is Bitmap bitmap)
- {
- this.Icon = Icon.FromHandle(bitmap.GetHicon());
- }
- else
- {
- this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
- }
- }
- }
- }
- }
- }
- }
- private void comboBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
- string url = comboBox1.Text;
- if (Uri.IsWellFormedUriString(url, UriKind.Absolute))
- {
- webView21.CoreWebView2.Navigate(url);
- }
- else
- {
- MessageBox.Show("请输入有效的网址", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- }
- private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Environment.Exit(0);
- }
- private void webView21_ContentLoading(object sender, CoreWebView2ContentLoadingEventArgs e)
- {
- progressBar.Value = 50;
- progressLabel.Text = "50%";
- StatusLabel.Text = "内容开始加载";
- }
- private async void SetZoomLevel(double zoomLevel)
- {
- await webView21.CoreWebView2.ExecuteScriptAsync($"document.body.style.zoom = '{zoomLevel}%'");
- }
- private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
- {
- SetZoomLevel(100);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem2_Click(object sender, EventArgs e)
- {
- zoom = zoom + 25;
- SetZoomLevel(zoom);
- toolStripMenuItem3.Enabled = true;
- }
- private void toolStripMenuItem3_Click(object sender, EventArgs e)
- {
- zoom = zoom - 25;
- SetZoomLevel(zoom);
- if (zoom == 25)
- toolStripMenuItem3.Enabled = false;
- }
- private void 刷新RToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Reload();
- }
- private void 停止TToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.Stop();
- }
- private void 前进QToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoForward();
- }
- private void 后退WToolStripMenuItem_Click(object sender, EventArgs e)
- {
- webView21.CoreWebView2.GoBack();
- }
- private void webView21_SourceChanged(object sender, CoreWebView2SourceChangedEventArgs e)
- {
- StatusLabel.Text = "源地址已更改";
- }
- private void WebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
- {
- string requestUri = e.Request.Uri;
- StatusLabel.Text = "请求资源:" + requestUri;
- }
- private void 工具栏ToolStripMenuItem_CheckStateChanged(object sender, EventArgs e)
- {
- if (工具栏ToolStripMenuItem.Checked == true)
- {
- statusStrip1.Visible = true;
- webView21.Height = webView21.Height - 23;
- }
- else
- {
- statusStrip1.Visible = false;
- webView21.Height = webView21.Height + 23;
- }
- }
- private void 关于EdgeVoyagerToolStripMenuItem_Click(object sender, EventArgs e)
- {
- AboutBox1 aboutBox1 = new AboutBox1();
- aboutBox1.ShowDialog();
- }
- }
- }
|