📄 form1.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region "Windows 窗体设计器生成的代码"
Public Sub New()
MyBase.New()
'此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
End Sub
'窗体重写处置,以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Option2 As System.Windows.Forms.RadioButton
Public WithEvents Option1 As System.Windows.Forms.RadioButton
Public WithEvents Timer1 As System.Windows.Forms.Timer
Public WithEvents Option3 As System.Windows.Forms.RadioButton
Public WithEvents Frame1 As System.Windows.Forms.GroupBox
Public WithEvents Label1 As System.Windows.Forms.Label
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器来修改它。
'不要使用代码编辑器来修改它。
Public WithEvents Button2 As System.Windows.Forms.Button
Public WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.Button2 = New System.Windows.Forms.Button
Me.Option2 = New System.Windows.Forms.RadioButton
Me.Option1 = New System.Windows.Forms.RadioButton
Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
Me.Button1 = New System.Windows.Forms.Button
Me.Frame1 = New System.Windows.Forms.GroupBox
Me.Option3 = New System.Windows.Forms.RadioButton
Me.Label1 = New System.Windows.Forms.Label
Me.Frame1.SuspendLayout()
Me.SuspendLayout()
'
'Button2
'
Me.Button2.BackColor = System.Drawing.SystemColors.Control
Me.Button2.Cursor = System.Windows.Forms.Cursors.Default
Me.Button2.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Button2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Button2.Location = New System.Drawing.Point(160, 128)
Me.Button2.Name = "Button2"
Me.Button2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button2.Size = New System.Drawing.Size(113, 41)
Me.Button2.TabIndex = 6
Me.Button2.Text = "退出程序"
'
'Option2
'
Me.Option2.BackColor = System.Drawing.SystemColors.Control
Me.Option2.Cursor = System.Windows.Forms.Cursors.Default
Me.Option2.Font = New System.Drawing.Font("宋体", 9.6!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Option2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Option2.Location = New System.Drawing.Point(24, 48)
Me.Option2.Name = "Option2"
Me.Option2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Option2.Size = New System.Drawing.Size(185, 25)
Me.Option2.TabIndex = 3
Me.Option2.TabStop = True
Me.Option2.Text = "定时模拟鼠标右键单击事件"
'
'Option1
'
Me.Option1.BackColor = System.Drawing.SystemColors.Control
Me.Option1.Checked = True
Me.Option1.Cursor = System.Windows.Forms.Cursors.Default
Me.Option1.Font = New System.Drawing.Font("宋体", 9.6!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Option1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Option1.Location = New System.Drawing.Point(24, 24)
Me.Option1.Name = "Option1"
Me.Option1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Option1.Size = New System.Drawing.Size(200, 32)
Me.Option1.TabIndex = 2
Me.Option1.TabStop = True
Me.Option1.Text = "定时模拟鼠标左键单击事件"
'
'Timer1
'
Me.Timer1.Interval = 1000
'
'Button1
'
Me.Button1.BackColor = System.Drawing.SystemColors.Control
Me.Button1.Cursor = System.Windows.Forms.Cursors.Default
Me.Button1.Font = New System.Drawing.Font("宋体", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Button1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Button1.Location = New System.Drawing.Point(24, 128)
Me.Button1.Name = "Button1"
Me.Button1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Button1.Size = New System.Drawing.Size(113, 41)
Me.Button1.TabIndex = 1
Me.Button1.Text = "开始模拟"
'
'Frame1
'
Me.Frame1.BackColor = System.Drawing.SystemColors.Control
Me.Frame1.Controls.Add(Me.Option3)
Me.Frame1.Font = New System.Drawing.Font("宋体", 9.6!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Frame1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Frame1.Location = New System.Drawing.Point(8, 8)
Me.Frame1.Name = "Frame1"
Me.Frame1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Frame1.Size = New System.Drawing.Size(313, 113)
Me.Frame1.TabIndex = 4
Me.Frame1.TabStop = False
Me.Frame1.Text = "选择"
'
'Option3
'
Me.Option3.BackColor = System.Drawing.SystemColors.Control
Me.Option3.Cursor = System.Windows.Forms.Cursors.Default
Me.Option3.Font = New System.Drawing.Font("宋体", 9.6!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Option3.ForeColor = System.Drawing.SystemColors.ControlText
Me.Option3.Location = New System.Drawing.Point(16, 64)
Me.Option3.Name = "Option3"
Me.Option3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Option3.Size = New System.Drawing.Size(209, 25)
Me.Option3.TabIndex = 5
Me.Option3.TabStop = True
Me.Option3.Text = "定时模拟鼠标双击事件(小心)"
'
'Label1
'
Me.Label1.BackColor = System.Drawing.SystemColors.Control
Me.Label1.Cursor = System.Windows.Forms.Cursors.Default
Me.Label1.Font = New System.Drawing.Font("宋体", 9.6!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(134, Byte))
Me.Label1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Label1.Location = New System.Drawing.Point(0, 8)
Me.Label1.Name = "Label1"
Me.Label1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Label1.Size = New System.Drawing.Size(321, 33)
Me.Label1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(327, 176)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Option2)
Me.Controls.Add(Me.Option1)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Frame1)
Me.Controls.Add(Me.Label1)
Me.Location = New System.Drawing.Point(4, 24)
Me.Name = "Form1"
Me.Text = "模拟鼠标事件"
Me.Frame1.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
'Option_Tag标示选择了哪一种模拟事件
Dim Option_Tag As Short
'OnTest标示是否处于模拟状态,以便我们停止模拟
Dim OnTest As Boolean
'控制 模拟的开始与结束
Private Sub Button1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button1.Click
'如果不处于模拟状态
If OnTest = False Then
Button1.Text = "快停下来吧"
Timer1.Enabled = True
OnTest = True
'如果处于模拟状态
Else
Button1.Text = "试一试"
Timer1.Enabled = False
OnTest = False
End If
End Sub
Private Sub Button2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button2.Click
End
End Sub
'窗体加载时一些变量需要设置
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Option_Tag = 1
Timer1.Enabled = False
OnTest = False
End Sub
'选择了模拟“鼠标左键单击事件”
Private Sub Option1_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option1.CheckedChanged
If eventSender.Checked Then
Option_Tag = 1 '左键单击事件
Option2.Checked = False
Option3.Checked = False
End If
End Sub
'选择了模拟“鼠标右键单击事件”
Private Sub Option2_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option2.CheckedChanged
If eventSender.Checked Then
Option_Tag = 2 '右键单击事件
Option1.Checked = False
Option3.Checked = False
End If
End Sub
'选择了模拟“鼠标双击事件”
Private Sub Option3_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Option3.CheckedChanged
If eventSender.Checked Then
Option_Tag = 3 '双击事件
Option1.Checked = False
Option2.Checked = False
End If
End Sub
'每隔一秒中模拟一次鼠标事件
Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
If Option_Tag = 1 Then
'调用了mouse_event函数,其参数的设置见前面说明
'如果同时要模拟两个鼠标事件,可以用 Or 将两个参数连接
'这里是 鼠标左键按下 和松开两个事件的组合即一次单击
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
ElseIf Option_Tag = 2 Then
'模拟鼠标右键单击事件
mouse_event(MOUSEEVENTF_RIGHTDOWN Or MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
Else
'两次连续的鼠标左键单击事件 构成一次鼠标双击事件
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -