Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Sub GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT)
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private CursorPoint As POINTAPI
Private DialogRect As RECT
'移动窗体到鼠标附近
Public Sub MoveDialogToCursor(ByRef MoveForm As Form)
GetCursorPos CursorPoint
GetClientRect MoveForm.hwnd, DialogRect
MoveWindow MoveForm.hwnd, CursorPoint.x + 10, CursorPoint.y + 10, DialogRect.right - DialogRect.left, DialogRect.bottom - DialogRect.top, 0
If MoveForm.left + MoveForm.Width > Screen.Width - 10 Then
MoveForm.left = Screen.Width - MoveForm.Width - 10
End If
If MoveForm.top + MoveForm.Height > Screen.Height - 10 Then
MoveForm.top = Screen.Height - MoveForm.Height - 10
End If
GetClientRect MoveForm.hwnd, DialogRect
If DialogRect.right < CursorPoint.x And DialogRect.bottom < CursorPoint.y Then
MoveWindow MoveForm.hwnd, CursorPoint.x - (DialogRect.right - DialogRect.left) - 10, CursorPoint.y - (DialogRect.bottom - DialogRect.top) - 10, DialogRect.right - DialogRect.left, DialogRect.bottom - DialogRect.top, 0
End If
End Sub
Private Declare Sub GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT)
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Type RECT
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
Private CursorPoint As POINTAPI
Private DialogRect As RECT
'移动窗体到鼠标附近
Public Sub MoveDialogToCursor(ByRef MoveForm As Form)
GetCursorPos CursorPoint
GetClientRect MoveForm.hwnd, DialogRect
MoveWindow MoveForm.hwnd, CursorPoint.x + 10, CursorPoint.y + 10, DialogRect.right - DialogRect.left, DialogRect.bottom - DialogRect.top, 0
If MoveForm.left + MoveForm.Width > Screen.Width - 10 Then
MoveForm.left = Screen.Width - MoveForm.Width - 10
End If
If MoveForm.top + MoveForm.Height > Screen.Height - 10 Then
MoveForm.top = Screen.Height - MoveForm.Height - 10
End If
GetClientRect MoveForm.hwnd, DialogRect
If DialogRect.right < CursorPoint.x And DialogRect.bottom < CursorPoint.y Then
MoveWindow MoveForm.hwnd, CursorPoint.x - (DialogRect.right - DialogRect.left) - 10, CursorPoint.y - (DialogRect.bottom - DialogRect.top) - 10, DialogRect.right - DialogRect.left, DialogRect.bottom - DialogRect.top, 0
End If
End Sub