首页 > 其他分享 >VBA控制鼠标键盘

VBA控制鼠标键盘

时间:2023-07-01 13:35:00浏览次数:71  
标签:VBA End 鼠标 ByVal Long 键盘 Private Text Sub

 

'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
    X As Long
    Y As Long
End Type

Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command1_Click()
Text2.Text = Text1.Text
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer) '在form上敲空格触发事件

If KeyAscii = 32 Then '如果按下的是空格键,注意空格Asc码是32
Call Command1_Click '那么执行command1点击事件
End If
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

正确例题
'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
X As Long
Y As Long

End Type
Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command1_Click()
Text2.Text = Text1.Text
End Sub

Private Sub Form_Load()
Call Command1_Click
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

全正确例题

'sleep函数
Private Declare Sub Sleep Lib “kernel32” (ByVal dwMilliseconds As Long)

'获取鼠标坐标
Private Type POINTAPI
X As Long
Y As Long

End Type
Private Declare Function GetCursorPos Lib “User32” (lpPoint As POINTAPI) As Long

'移动鼠标
Private Declare Function SetCursorPos Lib “User32” (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Sub mouse_event Lib “User32” (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const MOUSEEVENTF_LEFTDOWN = &H2
Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_RIGHTDOWN = &H8
Private Const MOUSEEVENTF_RIGHTUP = &H10

Private Sub Command2_Click()
Clipboard.Clear
Clipboard.SetText Text1.Text
End Sub

Private Sub Form_Load()
Call Command1_Click
End Sub

Private Sub Timer1_Timer()
Dim Point As POINTAPI
GetCursorPos Point
Text1.Text = “鼠标横坐标为:” & Point.X & vbCrLf & “鼠标纵坐标为:” & Point.Y

End Sub

Private Sub Command1_Click()

Text2.Text = Text1.Text

End Sub

Private Sub Command4_Click()
Clipboard.Clear
Clipboard.SetText Text2.Text

End Sub

Private Sub Command5_Click()
Text2.SelText = Clipboard.GetText
End Sub

Private Sub Command3_Click()
Static n As Integer
n = n + 1
If n > 5 Then n = 1
Select Case n
Case 1
Print Text1.Text & vbCrLf
Case 2
Print Text1.Text & vbCrLf
Case 3
Print Text1.Text & vbCrLf
Case 4
Print Text1.Text & vbCrLf
Case 5
Print Text1.Text & vbCrLf
End Select
End Sub

  

标签:VBA,End,鼠标,ByVal,Long,键盘,Private,Text,Sub
From: https://www.cnblogs.com/pythonClub/p/17519165.html

相关文章

  • windows11 添加英语键盘
    windows11添加英语键盘添加英语键盘设置英语键盘为默认1.添加英语键盘时间和语言->语言和区域添加键盘2.设置英语键盘为默认时间和语言->输入->高级键盘设置->替代默认输入法......
  • [转]VB6 如何让代码编辑器垂直滚动条随鼠标滚轮滚动
    VB6毕竟是很老的产品了,它的代码编辑器垂直滚动条并不能随鼠标的滚轮而滚动,这个问题会让我们在编写代码的时候觉得很不方便,不过还是有一种方法可以解决这个问题的。先下载一个微软发布的“VB6IDEMouseWheelAddin.dll”文件(此文件已经上传到百度网盘,网址:http://pan.baidu.com/s/1c0......
  • excel VBA中如何模拟键盘输入
    SendKeys语句将一个或多个按键消息发送到活动窗口,就如同在键盘上进行输入一样。语法SendKeysstring[,wait]SendKeys语句的语法具有以下几个:部分 描述string 必需的。,指定要发送的按键消息。Wait 可选的。指定等待方式的[email protected]值。如果为......
  • Python控制鼠标操作神器
    我们以前讲过怎样使用Python在浏览器中实现页面自动化操作,不管用哪种方式实现,都是通过定位页面中的元素来进行相应的操作。今天我们来聊一聊如何在桌面实现自动化操作。与浏览器页面自动化操作类似,桌面自动化操作也是需要定位鼠标在桌面的位置,然后根据定位的位置执行对应的操作。......
  • 迁移C盘系统到SSD固态硬盘后启动系统后黑屏只有鼠标
    第一步:修复UEFI引导第二步:修改注册表:简单来说就是进入PE系统然后运行RegEdit调出注册表,选中HKEY_LOCAL_MACHINE,打开菜单操作:文件-加载配置单元,然后找到系统目录c:\windows\system32\config\,选择system文件,在弹出对话框中输入sparkle,然后进入注册表sparkle,把mountedevi......
  • 编程初学者入门5_键盘输入5个人的身高(米),求他们的平均身高(米)。(C的没问题,试着用Java写
    写在前面此系列博客为牛客网编程初学者入门题目小结,题目很基础不常用的知识容易遗忘,为了边复习c语言和学习Java,后面系列博客将采用c、c++、Java双语言版记录现在位置(72/140),虽然我走的很慢,但我仍在前进~题目描述从键盘输入5个人的身高(米),求他们的平均身高(米)。输入描述:一行,连续输......
  • 编程初学者入门7_公务员面试现场打分。有7位考官,从键盘输入若干组成绩,每组7个分数(百分
    题目描述公务员面试现场打分。有7位考官,从键盘输入若干组成绩,每组7个分数(百分制),去掉一个最高分和一个最低分,输出每组的平均成绩。输入描述:一行,输入7个整数(0~100),代表7个成绩,用空格分隔。输出描述:一行,输出去掉最高分和最低分的平均成绩,小数点后保留2位,每行输出后换行。示例1我的......
  • 【雕爷学编程】Arduino动手做(131)---跑马灯矩阵键盘模块
    37款传感器与执行器的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里准备逐一动手尝试系列实验,不管成功(程序走通)与否,都会记录下来—小小的进步或是搞......
  • windows下鼠标右键增加--文件夹右键选项“通过Pycharm打开”
    效果如图所示:(可以通过鼠标点击文件夹,然后文件夹右键选择“通过Pycharm打开”)    操作流程:1.win+R输入regedit回车打开注册表编辑器2.进入如下路径:(文件夹右键添加的路径)计算机\HKEY_CLASSES_ROOT\Directory\shell 3.shell下新建项,名为Pycharm(步骤2中图片......
  • VBA 连接SQLserver数据库抽数据
    PrivateSubmyy()'t1=TimerDimiAsLong,jAsLong,shtAsWorksheet'i,j为整数变量;sht为excel工作表对象变量,指向某一工作表,定义为Integer类型的话,变量值超过32767时会溢出'DimcnAsNewADODB.Connection'定义数据链接对象,保存连接数据库信息;请先添加ADO引用......