代码开发
顶部菜单的设计
toolstrip的button的设计 注意tag要设计好。通过ToolStrip1_ItemClicked进行判断tag进行功能调用:
Private Async Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked
Dim webView = GetActiveWebView()
Try
If webView IsNot Nothing Then
If webView.CoreWebView2 Is Nothing Then
Await webView.EnsureCoreWebView2Async()
End If
Else
' 处理 webView 为 Nothing 的情况,可以在这里添加适当的日志记录或其他错误处理逻辑
Console.WriteLine("webView is Nothing.")
End If
Catch ex As Exception
' 处理异常,可以在这里添加适当的日志记录或其他错误处理逻辑
Console.WriteLine($"An error occurred: {ex.Message}")
End Try
Select Case TryCast(e.ClickedItem.Tag, String)
Case "search" '搜索
Try
Dim url = ToolStripTextBoxUrl.Text
Debug.WriteLine($"url:{url}")
webView.Source = New Uri(url)
Catch ex As Exception
Try
ToolStripTextBoxUrl.Text = "https://" & ToolStripTextBoxUrl.Text
webView.Source = New Uri(ToolStripTextBoxUrl.Text)
Catch
End Try
End Try
Case "pre"
webView.GoBack()
Case "next"
webView.GoForward()
Case "dev"
webView.CoreWebView2.OpenDevToolsWindow()
Case "refresh"
webView.CoreWebView2.Reload()
Case "close"
Dim index = TabControl1.SelectedIndex
If index <> 0 Then
Try
TabControl1.SelectedIndex = index - 1
TabControl1.TabPages.RemoveAt(index)
webViewList.RemoveAt(index - 1)
webView.Dispose()
Catch ex As Exception
End Try
End If
Case "clear"
If MessageBox.Show("清除所有历史记录?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
Await webView.CoreWebView2.Profile.ClearBrowsingDataAsync()
webView.CoreWebView2.Profile.CookieManager.DeleteAllCookies()
End If
Case "history" '历史,改用下载记录代替
webView.CoreWebView2.OpenDefaultDownloadDialog()
Case "add" '打开新的窗口
LoadEmptyUri()
Case "star"
If ToolStripButtonstar.ImageIndex = -1 Or ToolStripButtonstar.ImageIndex = 11 Then
' 清理标题字符串,然后将其分配给 customLinkInfo.Title
Dim cleanedTitle As String = webView.CoreWebView2.DocumentTitle.Trim()
For Each ch As Char In cleanedTitle
Debug.Write(ch)
Next
Dim customLinkInfo As New LinkInfo(ToolStripTextBoxUrl.Text, cleanedTitle)
linkList.Add(customLinkInfo)
ToolStripButtonstar.ImageIndex = 12
Else
Dim index = linkList.FindIndex(Function(item) item.link = ToolStripTextBoxUrl.Text)
Debug.WriteLine(index)
If index >= 0 Then
linkList.RemoveAt(index)
ToolStripButtonstar.ImageIndex = 11
End If
End If
updateLinkList()
Case "home"
LoadUri(TryCast(LinkLabel1.Tag, String))
Case "user"
Dim childForm As New FormLogin(Me)
childForm.ShowDialog()
Dim returnedValue As String = childForm.ReturnValue
If returnedValue IsNot Nothing Then
' 在这里处理返回的值
' MessageBox.Show(returnedValue, "返回的值")
ToolStripButtonuser.Tag = "quit"
ToolStripButtonpass.Enabled = True
ToolStripButtonpass.Tag = "pass"
ToolStripButtonmanage.Enabled = True
ToolStripButtonCut.Enabled = True
ToolStripButtonpicList.Enabled = True
End If
Case "quit"
Dim result As DialogResult = MessageBox.Show("确定退出吗?", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If result = DialogResult.Yes Then
ToolStripButtonuser.Tag = "user"
ToolStripButtonmanage.Tag = "manage"
ToolStripButtonpass.Enabled = False
ToolStripButtonmanage.Enabled = False
ToolStripButtonCut.Enabled = False
ToolStripButtonpicList.Enabled = False
End If
Case "cut"
Dim screen As New FormScreen(Me)
screen.SomeValue = webView.CoreWebView2.Source
screen.ShowDialog()
Case "pass"
Dim passwordForm As New FormPass(Me)
passwordForm.ShowDialog()
Case "manage"
Dim ManageForm As New FormUrlList(Me)
ManageForm.ShowDialog()
If ManageForm.return_url <> "" Then
AddNewTabPage(ManageForm.return_url)
End If
Case "droplist"
Dim addressForm As New FormAddress(Me)
addressForm.ShowDialog()
Dim returnedValue As String = addressForm.ReturnValue
Case "pics"
Dim picsform As New FormImageList(Me)
picsform.ShowDialog()
Case "help"
Dim helpForm As New FormHelp()
helpForm.ShowDialog()
End Select
End Sub
以上代码只是说明,不能直接使用
界面增加一个tabcontrol1
里面加有一个tabpage ,第1个tabpage1中没有加载webview2,主要功能是查询百度跳转到新的网页
声名变量
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms
Imports System.IO
Imports Newtonsoft.Json.Linq
Imports System.Text.RegularExpressions
Public Class FormBrowser
Public webViewList As New List(Of WebView2)()
Private ReadOnly linkList As New List(Of LinkInfo)()
Private ReadOnly blackLinkList As New List(Of LinkInfo)()
Private ReadOnly LinksFilePath As String = Path.Combine(Application.StartupPath, "links.txt")
Private ReadOnly BlackLinksFilePath As String = Path.Combine(Application.StartupPath, "black_links.txt")
Private ReadOnly DownloadFolderPath As String = Path.Combine(Application.StartupPath, "Download")
Private WithEvents ToolStrip As ToolStrip
Dim startPoint As Point
Dim endPoint As Point
Dim selecting As Boolean = False
Dim screenshotBitmap As Bitmap
Private marginRight As Integer
Public cut_screen As Boolean = True
Dim connectionString As String = "Data Source =" + Environment.CurrentDirectory + "/web.db"