' 發展單位:風禹科技驗證有限公司 ' 撰寫人:鄭子璉(Tzu-Lien, Cheng, 璉璉) ,成大水利博肄,微軟最有價值專家 ' Web: http://tlcheng.no-ip.com/TLCheng/ E-Mail: qvb3377@ms5.hinet.net ' -------------------------------------------------------------------------------------- Public Class cMouse #Region "Windows API 宣告" Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As enuMOUSEEVENTF, ByVal dx As Integer, ByVal dy As Integer, ByVal cButtons As Integer, ByVal dwExtraInfo As Integer) Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As Integer) As Integer Private Declare Function GetCapture Lib "user32" () As Integer Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer Private Declare Function SetCursorPos Lib "user32" (ByVal X As Integer, ByVal Y As Integer) As Integer Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Integer, ByVal yPoint As Integer) As Integer Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As Integer, ByVal nX As Integer, ByVal nY As Integer) As Integer Private Declare Function SetPixel Lib "gdi32" (ByVal hDC As Integer, ByVal nX As Integer, ByVal nY As Integer, ByVal crColor As Integer) As Integer Private Structure POINTAPI Dim X As Integer Dim Y As Integer End Structure Public Enum enuMouseButton As Integer Left = &H2 Right = &H8 Middle = &H20 End Enum Private Enum enuMouseAction As Integer MouseDown = 1 MouseUp = 2 MouseClick = MouseDown + MouseUp End Enum Private Enum enuMOUSEEVENTF As Integer MOVE = &H1 ' mouse move ABSOLUTE = &H8000 ' absolute move LEFTDOWN = enuMouseButton.Left * enuMouseAction.MouseDown ' left button down LEFTUP = enuMouseButton.Left * enuMouseAction.MouseUp ' left button up LEFTClick = enuMouseButton.Left * enuMouseAction.MouseClick ' left button Click RIGHTDOWN = enuMouseButton.Right * enuMouseAction.MouseDown ' right button down RIGHTUP = enuMouseButton.Right * enuMouseAction.MouseUp ' right button up RIGHTClick = enuMouseButton.Right * enuMouseAction.MouseClick ' right button Click MIDDLEDOWN = enuMouseButton.Middle * enuMouseAction.MouseDown ' middle button down MIDDLEUP = enuMouseButton.Middle * enuMouseAction.MouseUp ' middle button up MIDDLEClick = enuMouseButton.Middle * enuMouseAction.MouseClick ' middle button Click End Enum #End Region Public dwData As Integer, dwExtraInfo As Integer Private dx As Integer, dy As Integer Private m_x As Integer, m_y As Integer Public Property Pixel() As Integer Get Pixel = GetPixel(hDC, X, Y) End Get Set(ByVal vColor As Integer) SetPixel(hDC, X, Y, vColor) End Set End Property Public ReadOnly Property hDC() As Integer Get hDC = GetWindowDC(hWnd) End Get End Property Public ReadOnly Property hWnd() As Integer Get MouseGetCursorPos() hWnd = WindowFromPoint(m_x, m_y) End Get End Property Public Property X() As Integer Get MouseGetCursorPos() X = m_x End Get Set(ByVal xPos As Integer) m_x = xPos MouseSetCursorPos() End Set End Property Public Property Y() As Integer Get MouseGetCursorPos() Y = m_y End Get Set(ByVal yPos As Integer) m_y = yPos MouseSetCursorPos() End Set End Property Private Sub MouseSetCursorPos() Dim iReturn As Integer = SetCursorPos(m_x, m_y) End Sub Private Sub MouseGetCursorPos() Dim lpPoint As POINTAPI Dim iReturn As Integer = GetCursorPos(lpPoint) m_x = lpPoint.X m_y = lpPoint.Y End Sub Private Sub MouseAction(ByVal MouseButton As enuMOUSEEVENTF, ByVal bHere As Boolean) If bHere Then dx = 0 dy = 0 mouse_event(MouseButton, dx, dy, dwData, dwExtraInfo) Else MouseButton = MouseButton Or enuMOUSEEVENTF.ABSOLUTE mouse_event(MouseButton, X, Y, dwData, dwExtraInfo) End If End Sub Public Sub MoveTo(ByVal xPos As Integer, ByVal yPos As Integer, Optional ByVal nCount As Integer = 100) Dim i, oX, oY As Integer Dim iScale As Double oX = X oY = Y dx = xPos - oX dy = yPos - oY For i = 1 To nCount iScale = i / nCount m_x = oX + dx * iScale m_y = oY + dy * iScale MouseSetCursorPos() Application.DoEvents() Next End Sub Public Sub Drag(Optional ByVal bHere As Boolean = True) ButtonDown(enuMouseButton.Left, bHere) End Sub Public Sub Drop(Optional ByVal bHere As Boolean = True) ButtonUp(enuMouseButton.Left, bHere) End Sub Public Sub Click(Optional ByVal MouseButton As enuMouseButton = enuMouseButton.Left, Optional ByVal bHere As Boolean = True) Dim iClick As Integer iClick = MouseButton * enuMouseAction.MouseClick MouseAction(iClick, bHere) ' ButtonDown MouseButton, bHere ' ButtonUp MouseButton, bHere End Sub Public Sub DoubleClick(Optional ByVal MouseButton As enuMouseButton = enuMouseButton.Left, Optional ByVal bHere As Boolean = True) Click(MouseButton, bHere) Click(MouseButton, bHere) End Sub Public Sub ButtonDown(Optional ByVal MouseButton As enuMouseButton = enuMouseButton.Left, Optional ByVal bHere As Boolean = True) MouseAction(MouseButton, bHere) End Sub Public Sub ButtonUp(Optional ByVal MouseButton As enuMouseButton = enuMouseButton.Left, Optional ByVal bHere As Boolean = True) Dim iUp As Integer iUp = MouseButton * enuMouseAction.MouseUp MouseAction(iUp, bHere) End Sub End Class