Form1窗体代码: Option Explicit '窗体Form1上放四个标签Label1-4,三个命令按钮Command1-3,一个文本框控件Text1 Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long #If Win32 Then 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) #End IfPrivate Const MOUSEEVENTF_MOVE = &H1 Private Const MOUSEEVENTF_LEFTDOWN = &H2Private Const MOUSEEVENTF_LEFTUP = &H4 Private Const MOUSEEVENTF_ABSOLUTE = &H8000'Private Const WM_LBUTTONDOWN As Long = &H201 Private Const WM_GETTEXT As Long = &HD 'Private Const BN_CLICKED As Long = 0Dim p1 As POINTAPI, p2 As POINTAPI Dim X, Y, X1, Y1'根据文本框句柄取得文本框内容 Private Sub Command1_Click() Dim cText As String * 256 Call SendMessage(Text1.hwnd, WM_GETTEXT, 256, ByVal cText) '发送消息WM_GETTEXT cText = Left(cText, InStr(cText, Chr(0)) - 1) MsgBox "取得的文本框内容为:" & cText End SubPrivate Sub Command2_Click() MsgBox "yes" End SubPrivate Sub Command3_Click() Call MoveCursor(p1, p2) 'Mouse由Command1 ->Command2 mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTDOWN, X1, Y1, 0, 0 mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTUP, X1, Y1, 0, 0 End SubPrivate Sub Form_Load() Dim rect5 As RECT Call GetWindowRect(Command1.hwnd, rect5) '取得Command1相对於Screen的座标 p1.X = (rect5.Left + rect5.Right) \ 2 p1.Y = (rect5.ToP + rect5.Bottom) \ 2 Call GetWindowRect(Command2.hwnd, rect5) p2.X = (rect5.Left + rect5.Right) \ 2 p2.Y = (rect5.ToP + rect5.Bottom) \ 2 Label1.Caption = p2.X Label2.Caption = p2.Y X = Label1.Caption Y = Label2.Caption X1 = (65535 \ 1024) * X Y1 = (65535 \ 768) * Y Label3.Caption = X1 Label4.Caption = Y1 Text1.Text = "中华人民共和国" 'Me.ScaleMode = 3 End Sub标准模块: Option ExplicitType RECT Left As Long ToP As Long Right As Long Bottom As Long End Type Type POINTAPI X As Long Y As Long End TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Sub MoveCursor(FromP As POINTAPI, ToP As POINTAPI) Dim stepx As Long, stepy As Long, k As Long Dim i As Long, j As Long, sDelay As Long stepx = 1 stepy = 1 i = (ToP.X - FromP.X) If i < 0 Then stepx = -1 i = (ToP.Y - FromP.Y) If i < 0 Then stepy = -1 For i = FromP.X To ToP.X Step stepx Call SetCursorPos(i, FromP.Y) Sleep (1) Next i For i = FromP.Y To ToP.Y Step stepy Call SetCursorPos(ToP.X, i) Sleep (1) Next i End Sub参见:
Option Explicit
'窗体Form1上放四个标签Label1-4,三个命令按钮Command1-3,一个文本框控件Text1
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long
#If Win32 Then
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)
#End IfPrivate Const MOUSEEVENTF_MOVE = &H1
Private Const MOUSEEVENTF_LEFTDOWN = &H2Private Const MOUSEEVENTF_LEFTUP = &H4
Private Const MOUSEEVENTF_ABSOLUTE = &H8000'Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_GETTEXT As Long = &HD
'Private Const BN_CLICKED As Long = 0Dim p1 As POINTAPI, p2 As POINTAPI
Dim X, Y, X1, Y1'根据文本框句柄取得文本框内容
Private Sub Command1_Click()
Dim cText As String * 256
Call SendMessage(Text1.hwnd, WM_GETTEXT, 256, ByVal cText) '发送消息WM_GETTEXT
cText = Left(cText, InStr(cText, Chr(0)) - 1)
MsgBox "取得的文本框内容为:" & cText
End SubPrivate Sub Command2_Click()
MsgBox "yes"
End SubPrivate Sub Command3_Click()
Call MoveCursor(p1, p2) 'Mouse由Command1 ->Command2
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTDOWN, X1, Y1, 0, 0
mouse_event MOUSEEVENTF_ABSOLUTE Or MOUSEEVENTF_LEFTUP, X1, Y1, 0, 0
End SubPrivate Sub Form_Load()
Dim rect5 As RECT Call GetWindowRect(Command1.hwnd, rect5) '取得Command1相对於Screen的座标
p1.X = (rect5.Left + rect5.Right) \ 2
p1.Y = (rect5.ToP + rect5.Bottom) \ 2
Call GetWindowRect(Command2.hwnd, rect5)
p2.X = (rect5.Left + rect5.Right) \ 2
p2.Y = (rect5.ToP + rect5.Bottom) \ 2
Label1.Caption = p2.X
Label2.Caption = p2.Y
X = Label1.Caption
Y = Label2.Caption
X1 = (65535 \ 1024) * X
Y1 = (65535 \ 768) * Y
Label3.Caption = X1
Label4.Caption = Y1 Text1.Text = "中华人民共和国"
'Me.ScaleMode = 3
End Sub标准模块:
Option ExplicitType RECT
Left As Long
ToP As Long
Right As Long
Bottom As Long
End Type
Type POINTAPI
X As Long
Y As Long
End TypeDeclare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)Public Sub MoveCursor(FromP As POINTAPI, ToP As POINTAPI)
Dim stepx As Long, stepy As Long, k As Long
Dim i As Long, j As Long, sDelay As Long
stepx = 1
stepy = 1
i = (ToP.X - FromP.X)
If i < 0 Then stepx = -1
i = (ToP.Y - FromP.Y)
If i < 0 Then stepy = -1
For i = FromP.X To ToP.X Step stepx
Call SetCursorPos(i, FromP.Y)
Sleep (1)
Next i
For i = FromP.Y To ToP.Y Step stepy
Call SetCursorPos(ToP.X, i)
Sleep (1)
Next i
End Sub参见: