February 12, 2001

Set Mouse Cursor Position

VERSION 5.00
Begin VB.Form SetMousePointer
Caption = "Form2"
ClientHeight = 3195
ClientLeft = 60
ClientTop = 345
ClientWidth = 4680
LinkTopic = "Form2"
ScaleHeight = 3195
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 525
Left = 2670
TabIndex = 1
Top = 1260
Width = 1245
End
Begin VB.CommandButton Command1
Caption = "ClickMe to see"
Height = 525
Left = 720
TabIndex = 0
Top = 1260
Width = 1245
End
End
Attribute VB_Name = "SetMousePointer"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
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

Public Sub SnapMouse(ByVal hwnd As Long)

Dim lpRect As RECT

GetWindowRect hwnd, lpRect

SetCursorPos lpRect.Left + (lpRect.Right - lpRect.Left) \ 2, _
lpRect.Top + (lpRect.Bottom - lpRect.Top) \ 2

End Sub

Private Sub Command1_Click()
SnapMouse (Command2.hwnd)
End Sub