开发环境
最近公司有一个开发要求,就是到客户平台上定时拿到自己下属业员的业务数据。但发现客户的平台用的是vue ,数据都是后加载,本来想用python去完成采集,但要求手机号登陆,同时所有的业务菜单又进行了相关的加密。找了半天也没有找到对方的API接口。一周几单的量,也没有必要花太多的时间,就准备自己开发一个浏览器,打开业务详情,点击一下就将数据采集出来,post自己公司的接口,将数据放到自己的平台。找了一些资料,大都是C#的,为了不侵权,就将代码改为VB.net,加入了更多自己的业务需求。以下资料只是对自己使用webview2的一些学习资料。
visual studio 2022的控件有一个webview,功能有限,还要自己写代码维护登陆状态,比如拿到cookie一类的,的确麻烦,后来发现webview2,可以说具有了浏览器的一切功能,但加方便快捷。故通过TabControl 里放置webview2,通过打开tabpage里的vewview解决打开新浏览器。再通过toolstrip制作浏览器的顶部菜单及url的框。
-visual studio 2020
- net 8.0
- webview2
引入相关插件或模块
- netget引入webview2
顶部界面
说明一下,如果用toolstrip中的toolstripcombobox时,存放网址时,造成toolstrip变型,不知是环境的事还是版本的事情,最后用了ToolStripTextBox,也就是一个文本框。但用系统自有的ToolStripTextBox,在界面大小改变时 ToolStripTextBox,它的大小不会改变。为了让ToolStripTextBox可以跟随界面大小自动适用。用自写义ToolStripSpringTextBox,以下是代码。
Public Class ToolStripSpringTextBox
Inherits ToolStripTextBox
Public Overrides Function GetPreferredSize(constrainingSize As Size) As Size
' Use the default size if the text box is on the overflow menu
' or is on a vertical ToolStrip.
If IsOnOverflow OrElse Owner.Orientation = Orientation.Vertical Then
Return DefaultSize
End If
' Declare a variable to store the total available width as
' it is calculated, starting with the display width of the
' owning ToolStrip.
Dim width As Integer = Owner.DisplayRectangle.Width
' Subtract the width of the overflow button if it is displayed.
If Owner.OverflowButton.Visible Then
width = width - Owner.OverflowButton.Width -
Owner.OverflowButton.Margin.Horizontal
End If
' Declare a variable to maintain a count of ToolStripSpringTextBox
' items currently displayed in the owning ToolStrip.
Dim springBoxCount As Integer = 0
For Each item As ToolStripItem In Owner.Items
' Ignore items on the overflow menu.
If item.IsOnOverflow Then Continue For
If TypeOf item Is ToolStripSpringTextBox Then
' For ToolStripSpringTextBox items, increment the count and
' subtract the margin width from the total available width.
springBoxCount += 1
width -= item.Margin.Horizontal
Else
' For all other items, subtract the full width from the total
' available width.
width = width - item.Width - item.Margin.Horizontal
End If
Next
' If there are multiple ToolStripSpringTextBox items in the owning
' ToolStrip, divide the total available width between them.
If springBoxCount > 1 Then width /= springBoxCount
' If the available width is less than the default width, use the
' default width, forcing one or more items onto the overflow menu.
If width < DefaultSize.Width Then width = DefaultSize.Width
' Retrieve the preferred size from the base class, but change the
' width to the calculated width.
Dim size As Size = MyBase.GetPreferredSize(constrainingSize)
size.Width = width
Return size
End Function
End Class
调用方式:
ToolStripTextBoxUrl = New ToolStripSpringTextBox() '在界面的设计里面改 ToolStripTextBoxUrl = New ToolStripTextBox()
vb.net的的messagebox,弹出后要点击才能关闭,又写了一个公用的form模拟新的弹框。自动关闭。
Public Class MessageBoxHelper
Inherits Form
Private Shared timer As New Timer()
Private Shared result As DialogResult = DialogResult.None
' Label for displaying the message
Private label As New Label()
Public Sub New()
' Set the form properties
Me.Text = String.Empty ' Clear the title
Me.ControlBox = False ' Hide the control box (minimize, maximize, close buttons)
Me.StartPosition = FormStartPosition.CenterScreen
Me.Size = New Size(288, 100)
' Configure the label
label.Text = String.Empty
label.Dock = DockStyle.Fill
label.AutoSize = False
label.TextAlign = ContentAlignment.MiddleCenter ' Center the text
' Add the label to the form
Me.Controls.Add(label)
' Attach the FormClosed event handler
AddHandler Me.FormClosed,
Sub(sender, e)
timer.Stop()
End Sub
End Sub
Public Sub SetMessage(message As String, duration As Integer)
' Set the message text
label.Text = message
' Start the timer after setting the message
timer.Interval = duration ' 5000 milliseconds (5 seconds)
timer.Start()
' Attach an event handler to stop the timer after the specified duration
AddHandler timer.Tick,
Sub(sender, e)
timer.Stop()
Me.Close()
End Sub
End Sub
Public Shared Sub Show(message As String, title As String)
Dim form As New MessageBoxHelper()
form.Text = title
' Set the message text and duration
form.SetMessage(message, 500)
form.ShowDialog()
End Sub
Public Shared Function Show(message As String, title As String, duration As Integer) As DialogResult
Dim form As New MessageBoxHelper()
form.Text = title
' Set the message text
form.SetMessage(message, duration)
Dim okButton As New Button() With {
.Text = "OK",
.DialogResult = DialogResult.OK,
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
}
form.Controls.Add(okButton)
result = form.ShowDialog()
Return result
End Function
Private Sub MessageBoxHelper_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
timer.Stop()
End Sub
Public Shared Function STime(message As String, title As String, duration As Integer) As DialogResult
Dim form As New MessageBoxHelper()
form.Text = title
' Set the message text and duration
form.SetMessage(message, duration)
Dim okButton As New Button() With {
.Text = "OK",
.DialogResult = DialogResult.OK,
.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
}
form.Controls.Add(okButton)
Return form.ShowDialog()
End Function
End Class
调用方式:
MessageBoxHelper.STime("无效帐号及密码", "提示", 888) ' 5000 毫秒(5 秒)