VB Code:
  1. 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
  2. Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
  3. Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, lpRect As RECT) As Long
  4. Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
  5.  
  6. Type RECT
  7.     left As Long
  8.     top As Long
  9.     right As Long
  10.     bottom As Long
  11. End Type
  12.  
  13. Private Const SWP_NOMOVE = 2
  14. Private Const SWP_NOSIZE = 1
  15. Private Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
  16. Private Const HWND_TOPMOST = -1
  17. Private Const HWND_NOTOPMOST = -2
  18.  
  19. Private Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) As Long
  20.   On Error Goto ErrRtn
  21.    If (Topmost) Then
  22.       SetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
  23.    Else
  24.       SetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
  25.       SetTopMostWindow = False
  26.    End If
  27.    Exit Function
  28. ErrRtn:
  29. MsgBox "Error in SetTopMostWindow " & Err & " " & Error, vbExclamation + vbOKCancel
  30. End Function
  31.  
  32. Private Sub Form_Load()
  33. Dim r As RECT
  34. Dim retval As Long
  35. 'To make a form always on top
  36. SetTopMostWindow Me.hwnd, TRUE
  37. '(SetTopMostWindow Me.hwnd, FALSE would prevent window from being TopMost)
  38. 'To prevent any action on any other window, confine the mouse to the topmost form, using this
  39. retval = GetWindowRect(Form1.hwnd, r)
  40. retval = ClipCursor(r)
  41. End Sub