可试试这个API函数,它可捕获鼠标,把鼠标移动事件传给窗口。
HWND SetCapture( HWND hWnd // handle of window to receive mouse capture
);
如果只想得到鼠标的位置,可在程序中调用下面这个函数。
BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position
);
HWND SetCapture( HWND hWnd // handle of window to receive mouse capture
);
如果只想得到鼠标的位置,可在程序中调用下面这个函数。
BOOL GetCursorPos( LPPOINT lpPoint // address of structure for cursor position
);
X As Long
Y As Long
End TypeType RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End TypeDeclare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
form1.frmOption ExplicitPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim B As RECT
Dim A As POINTAPI
GetCursorPos A
GetWindowRect Form1.hwnd, B
'Lblx = X / Screen.TwipsPerPixelX
Lblx = A.X - B.Left
Lbly = A.Y - B.TopEnd Sub
929提供的函数SetCapture是不能监测另一个程序的窗口鼠标位置的。介绍加上三个GetCapture:取得目前拥有鼠标Capture的窗口hWnd,GetCursorPos取得鼠标的位置,WindowFromPoint取得某一位置底下的窗口hWnd。Type POINTAPI
x As Long
y As Long
End TypeDeclare Function SetCapture Lib "user32" (ByVal hWnd As Long) As Long
Declare Function ReleaseCapture Lib "user32" () As Long
Declare Function GetCapture Lib "user32" () As LongDeclare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long接下来在窗体中放一个Timer控件,定时判断鼠标所在位置的窗口hWnd是否等于拥有鼠标Capture的窗口hWnd,如果不是,则表示鼠标移到另外的程序中。这时就可以从利用Timer事件得到GetCursorPos的鼠标数据来继续程序控制了。
Form_Load事件添加
SetCapture Me.hWnd
Form_Unload事件添加
ReleaseCapture
Time1_Timer事件
Dim hCursorWnd As Long, point As POINTAPI
GetCursorPos point
hCursorWnd = WindowFromPoint(point.x, point.y)
If GetCapture <> 0 And GetCapture <> hCursorWnd Then
’添加程序
ReleaseCapture
End If