VB怎么移动鼠标到指定位置
VB在窗体的指定位置模拟鼠标单击动作,想知道怎么得到鼠标的坐标,和怎么单击,请高高手完成这种效果的代码::::运行程序后,label1随时显示鼠标的相对于窗体坐标xy.按...
VB在窗体的指定位置模拟鼠标单击动作,想知道怎么得到鼠标的坐标,和怎么单击,请高高手完成这种效果的代码::::运行程序后,label1随时显示鼠标的相对于窗体坐标xy.按钮单1击后鼠标自动移动到屏幕的中心位置.并按下按钮2(按钮2在中间,为了证明按钮2按下,请在command2的chick写入 print 123),3Q了,50分先说好,我认为好的话再加50,请将代码尽量写详细说明.
二楼,请解说一下什么是把下面的代码保存为form1.frm????? 展开
二楼,请解说一下什么是把下面的代码保存为form1.frm????? 展开
4个回答
展开全部
把下面代码保存为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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用api
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询