VBGood网站全文搜索 Google

首页 - 经验之谈 - 停止鼠标活动
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:9500, 日期:2001-04-19
Snap the Mouse

Snapping the mouse to a certain position on the screen is a great little trick.

It can help suggest a command button to the user or simply make your program easier to use. And this groovy code snippet shows you how to do just that...

To use, simply call the SnapMouse method, passing the hWnd of the object you want to centre the mouse over. You抣l find that most objects come with a .hWnd property.


SnapMouse (Command2.hWnd)Code

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