codice:
Option Explicit
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hWnd As Long, ByVal hWndInsertAfter As Long, _
ByVal X As Long, ByVal Y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hWnd As Long, _
lpRect As RECT) As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hWnd As Long, _
lpPoint As Any) As Long
Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
Const HWND_TOPMOST = -1
Const HWND_NOTTOPMOST = -2
Const SWP_NOSIZE = 1
Const SWP_NOMOVE = 2
Private flags As Integer
Private lResult As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Sub ClipMouseToWindow(Optional ByVal hWnd As Long)
Dim rcTarg As RECT
If hWnd Then
'clip
GetClientRect hWnd, rcTarg
ClientToScreen hWnd, rcTarg
ClientToScreen hWnd, rcTarg.Right
ClipCursor rcTarg
Else
'no clip
ClipCursor ByVal 0&
End If
End Sub
Private Sub Form_Load()
flags = SWP_NOSIZE Or SWP_NOMOVE
lResult = SetWindowPos(Form1.hWnd, HWND_TOPMOST, 0, 0, 0, 0, flags)
ClipMouseToWindow Me.hWnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
ClipMouseToWindow
End Sub
è importantissimo che metti anche la routine dell'unload