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

2024-11-20 18:32:14
推荐回答(4个)
回答1:

VB光标移动有两种方式:

1. 移动到指定控件,转移焦点:

Text1.SetFocus

2. 移动指定文本字符串位置:

Text1.SelStart=Len(Text1)

回答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

回答3:

Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Dim a As RECT
Private Sub Command1_Click()
GetWindowRect Me.hwnd, a
SetCursorPos a.Left + 5, a.Top + 20
End Sub

回答4:

使用api