VB怎么移动鼠标到指定位置

 我来答
刘鹏SUlm3
2013-11-27 · 超过74用户采纳过TA的回答
知道答主
回答量:117
采纳率:0%
帮助的人:163万
展开全部
答案2:把下面代码保存为Form1.frm,然后双击打开此文件,运行即可。 VERSION 5.00 Begin VB.Form Form1 BorderStyle = 3 'Fixed Dialog Caption = "Form1" ClientHeight = 4005 ClientLeft = 45 ClientTop = 435 ClientWidth = 6630 LinkTopic = "Form1" MaxButton = 0 'False MinButton = 0 'False ScaleHeight = 4005 ScaleWidth = 6630 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton Command2 Caption = "Command2" Height = 495 Left = 2640 TabIndex = 3 Top = 1440 Width = 1215 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 495 Left = 4200 TabIndex = 2 Top = 3120 Width = 1215 End Begin VB.Frame Frame1 BackColor = &H00C0C0C0& Caption = "鼠标位置" BeginProperty Font Name = "宋体" Size = 9 Charset = 134 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 840 Left = 120 TabIndex = 0 Top = 3000 Width = 3255 Begin VB.Label Label1 BackColor = &H00C0C0C0& Caption = "Label1" Height = 375 Left = 120 TabIndex = 1 Top = 240 Width = 3015 End End Begin VB.Timer Timer1 Interval = 10 Left = 720 Top = 960 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Private Type POINTAPI X As Long Y As Long End Type Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As Long, lpPoint As POINTAPI) As Long Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) 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 Enum MouseClick '定义鼠标常数 MOUSEEVENTF_LEFTDOWN = &H2 MOUSEEVENTF_LEFTUP = &H4 MOUSEEVENTF_RIGHTDOWN = &H8 MOUSEEVENTF_RIGHTUP = &H10 MOUSEEVENTF_MIDDLEDOWN = &H20 MOUSEEVENTF_MIDDLEUP = &H40 End Enum Private Sub Command1_Click() Dim pt As POINTAPI pt.X = Me.ScaleWidth \ Screen.TwipsPerPixelX \ 2 pt.Y = Me.ScaleHeight \ Screen.TwipsPerPixelY \ 2 ClientToScreen Me.hwnd, pt SetCursorPos pt.X, pt.Y '鼠标自动移动到屏幕的中心位置. mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 '自动按下按钮2 End Sub Private Sub Command2_Click() Print "123" '按下按钮2后输出字符123 End Sub Private Sub Form_Load() With Command2 .Left = (Me.ScaleWidth - .Width) \ 2 .Top = (Me.ScaleHeight - .Height) \ 2 End With End Sub Private Sub Timer1_Timer() Dim pt As POINTAPI Call GetCursorPos(pt) ScreenToClient Me.hwnd, pt Label1 = "x=" & pt.X & ";y=" & pt.Y '随时显示鼠标的相对于窗体坐标x End Sub
吹花PM
2013-12-12 · TA获得超过946个赞
知道小有建树答主
回答量:635
采纳率:0%
帮助的人:608万
展开全部
一个API函数即可
Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long

Private Sub Form_Click()
SetCursorPos 500, 500
End Sub
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式