VB在窗體的指定位置模擬鼠標(biāo)單擊動作,想知道怎么得到鼠標(biāo)的坐標(biāo),和怎么單擊,請高高手完成這種效果的代碼::::運行程序后,label1隨時顯示鼠標(biāo)的相對于窗體坐標(biāo)xy.按鈕單1擊后鼠標(biāo)自動移動到屏幕的中心位置.并按下按鈕2(按鈕2在中間,為了證明按鈕2按下,請在command2的chick寫入 print 123),3Q了,50分先說好,我認(rèn)為好的話再加50,請將代碼盡量寫詳細(xì)說明. 滿意答案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 = "鼠標(biāo)位置" 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 '定義鼠標(biāo)常數(shù) 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 '鼠標(biāo)自動移動到屏幕的中心位置. 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 '隨時顯示鼠標(biāo)的相對于窗體坐標(biāo)x End Sub |
|