vb.net加webview2以及tabcontrol打造一个简易浏览器2

代码开发

顶部菜单的设计
在这里插入图片描述
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"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 首先,您需要下载安装WebView2 SDK,然后在Visual Studio中使用NuGet管理器安装Microsoft.Web.WebView2.WinForms包。接下来,您可以在您的VB.NET应用程序中添WebView2控件,并使用以下代码来载网页:webView2.Source = New Uri("http://example.com")。 ### 回答2: WebView2 是一个使用 Chromium 内核的 Web 控件,方便在 VB.NET 程序中嵌入网页视图。以下是使用 WebView2 控件编写 VB.NET 程序的基本步骤: 第一步:安装 WebView2 运行时 1. 在 Visual Studio 中打开 VB.NET 程序项目。 2. 在 "工具" 菜单下选择 "NuGet 包管理器",然后选择 "管理解决方案的 NuGet 包"。 3. 在 "NuGet 包管理器控制台" 中,运行以下命令安装 WebView2 运行时: Install-Package Microsoft.Web.WebView2 -Version 1.0.1106.1 第二步:添 WebView2 控件到窗体 1. 在窗体设计器中,从工具箱中找到 "WebView2" 控件并添到窗体上。 2. 调整控件的大小和位置,使其适应窗体布局。 第三步:在代码中使用 WebView2 控件 1. 在窗体代码中,添以下命名空间引用: Imports Microsoft.Web.WebView2.WinForms 2. 在窗体类中定义一个 WebView2 实例变量,例如: Private webView As WebView2 3. 在窗体的载事件中初始化 WebView2 控件,例如: Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load webView = New WebView2() webView.Dock = DockStyle.Fill Me.Controls.Add(webView) webView.Source = New Uri("https://example.com") End Sub 在上述代码中,将 WebView2 控件添到窗体并指定要载的网页。 至此,你已经成功使用 WebView2 控件编写了一个简单的 VB.NET 程序。你可以根据需要进一步定制 WebView2 的功能,如执行 JavaScript、处理事件等。 需要注意的是,你可能还需要将 WebView2 运行时进行包含到你的应用程序安装程序中,以便将 WebView2 与你的程序一起分发给用户。具体的部署可以参考 Microsoft 官方文档。 ### 回答3: 在VB.NET中使用WebView2,首先需要进行以下步骤: 1. 安装WebView2控件:在电脑上安装WebView2控件。可以从WebView2的官方网站(https://developer.microsoft.com/en-us/microsoft-edge/webview2/)下载最新版本的运行时组件。 2. 创建VB.NET项目:在Visual Studio中创建一个新的VB.NET项目。 3. 添WebView2控件到表单:打开Visual Studio的“工具箱”,然后找到“WebView2”控件。将其拖放到表单上以添控件。 4. 设置WebView2控件的属性:选中刚添WebView2控件,然后在属性窗口中为其设置属性。例如,可以设置WebView2控件的大小、位置等。 5. 编写VB.NET代码:使用VB.NET代码来控制WebView2控件的行为。例如,可以载网页、显示网页标题等等。以下是一个简单的示例代码: ```vb Imports Microsoft.Web.WebView2.Core Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ' 初始化WebView2控件 webView2Control.Source = New Uri("https://www.example.com") ' 注册WebView2控件的事件 AddHandler webView2Control.NavigationCompleted, AddressOf WebView2_NavigationCompleted End Sub Private Sub WebView2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) ' 在导航完成时显示网页标题 MessageBox.Show(webView2Control.CoreWebView2.DocumentTitle) End Sub End Class ``` 在这个示例中,首先在窗体载时初始化WebView2控件,并通过指定URL一个网页。然后,通过注册NavigationCompleted事件来监听WebView2控件的导航完成事件。在导航完成时,使用MessageBox显示网页的标题。 以上就是在VB.NET中使用WebView2控件的基本步骤和示例代码。当然,你还可以根据具体需求来使用WebView2控件,例如执行JavaScript代码、与网页进行交互等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

老大白菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值