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 = @"
起始页
LYKNS
";
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();
}
}
}