|
@@ -1,4 +1,12 @@
|
|
using Microsoft.Web.WebView2.Core;
|
|
using Microsoft.Web.WebView2.Core;
|
|
|
|
+using System.Drawing.Imaging;
|
|
|
|
+using System;
|
|
|
|
+using System.Drawing;
|
|
|
|
+using System.IO;
|
|
|
|
+using System.Net.Http;
|
|
|
|
+using System.Windows.Forms;
|
|
|
|
+using Microsoft.Web.WebView2.Core;
|
|
|
|
+using Microsoft.Web.WebView2.WinForms;
|
|
|
|
|
|
namespace EdgeVoyager
|
|
namespace EdgeVoyager
|
|
{
|
|
{
|
|
@@ -8,13 +16,30 @@ namespace EdgeVoyager
|
|
public Form1()
|
|
public Form1()
|
|
{
|
|
{
|
|
InitializeComponent();
|
|
InitializeComponent();
|
|
- InitializeAsync();
|
|
|
|
|
|
+ InitializeAsync();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
|
|
|
|
+ {
|
|
|
|
+ Form1 newForm = new Form1();
|
|
|
|
+ // 设置新窗口中WebView2的Source属性为要打开的链接
|
|
|
|
+ newForm.webView21.Source = new Uri(e.Uri);
|
|
|
|
+ // 显示新窗口
|
|
|
|
+ newForm.Show();
|
|
|
|
+ // 设置Handled为true,表示已经处理了新窗口请求
|
|
|
|
+ e.Handled = true;
|
|
}
|
|
}
|
|
|
|
|
|
async void InitializeAsync()
|
|
async void InitializeAsync()
|
|
{
|
|
{
|
|
await webView21.EnsureCoreWebView2Async(null);
|
|
await webView21.EnsureCoreWebView2Async(null);
|
|
- webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN");
|
|
|
|
|
|
+ webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
|
|
|
|
+ if (Settings1.Default.firstwindow == true)
|
|
|
|
+ {
|
|
|
|
+ webView21.CoreWebView2.Navigate("https://cn.bing.com/?mkt=zh-CN");
|
|
|
|
+ Settings1.Default.firstwindow = false;
|
|
|
|
+ Settings1.Default.Save();
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private void back_button_Click(object sender, EventArgs e)
|
|
private void back_button_Click(object sender, EventArgs e)
|
|
@@ -44,7 +69,6 @@ namespace EdgeVoyager
|
|
|
|
|
|
private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
|
|
private void webView21_NavigationStarting(object sender, CoreWebView2NavigationStartingEventArgs e)
|
|
{
|
|
{
|
|
- int num = 0;
|
|
|
|
string currentUrl = webView21.Source.ToString();
|
|
string currentUrl = webView21.Source.ToString();
|
|
if (!comboBox1.Items.Contains(currentUrl))
|
|
if (!comboBox1.Items.Contains(currentUrl))
|
|
{
|
|
{
|
|
@@ -54,31 +78,6 @@ namespace EdgeVoyager
|
|
progressBar.Value = 0;
|
|
progressBar.Value = 0;
|
|
progressLabel.Text = "0%";
|
|
progressLabel.Text = "0%";
|
|
StatusLabel.Text = "开始导航到:" + currentUrl;
|
|
StatusLabel.Text = "开始导航到:" + currentUrl;
|
|
- if (num != 0)
|
|
|
|
- {
|
|
|
|
- Form1 newForm = new Form1();
|
|
|
|
-
|
|
|
|
- // 等待新窗口的WebView2控件初始化完成
|
|
|
|
- newForm.webView21.CoreWebView2InitializationCompleted += (s, args) =>
|
|
|
|
- {
|
|
|
|
- if (args.IsSuccess)
|
|
|
|
- {
|
|
|
|
- // 设置新窗口中WebView2控件的导航目标为当前要打开的URL
|
|
|
|
- newForm.webView21.CoreWebView2.Navigate(e.Uri);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- // 初始化失败,可以进行相应的错误处理
|
|
|
|
- MessageBox.Show("WebView2初始化失败:" + args.InitializationException.Message);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- // 显示新窗口
|
|
|
|
- newForm.Show();
|
|
|
|
-
|
|
|
|
- // 取消当前的导航请求
|
|
|
|
- e.Cancel = true;
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
|
|
private async void webView21_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
|
|
@@ -94,6 +93,35 @@ namespace EdgeVoyager
|
|
StatusLabel.Text = "完成。";
|
|
StatusLabel.Text = "完成。";
|
|
string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
|
|
string title = await webView21.CoreWebView2.ExecuteScriptAsync("document.title;");
|
|
this.Text = "LYKNS EdgeVoyager:" + title.Trim('"');
|
|
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))
|
|
|
|
+ {
|
|
|
|
+ using (var client = new HttpClient())
|
|
|
|
+ {
|
|
|
|
+ var faviconData = await client.GetByteArrayAsync(faviconUrl);
|
|
|
|
+ using (var ms = new MemoryStream(faviconData))
|
|
|
|
+ {
|
|
|
|
+ using (var img = Image.FromStream(ms))
|
|
|
|
+ {
|
|
|
|
+ if (img is Bitmap bitmap)
|
|
|
|
+ {
|
|
|
|
+ this.Icon = Icon.FromHandle(bitmap.GetHicon());
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ // 如果图片不是Bitmap类型,可以考虑其他方式来获取图标
|
|
|
|
+ // 例如,你可以尝试将图片保存到文件,然后使用Icon.ExtractAssociatedIcon方法获取图标
|
|
|
|
+ // 或者直接使用img作为窗体的图标(如果可行)
|
|
|
|
+ // 这里以直接使用img作为窗体图标为例(需确保img可以作为窗体图标)
|
|
|
|
+ this.Icon = Icon.FromHandle(((Bitmap)img.Clone()).GetHicon());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
|
|
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
|