Di bawah ini merupakan procedure VB6 untuk membatasi gerak pointer mouse pada objek tertentu yang memilliki hwnd (handle window).
Option Explicit
Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Declare Sub GetClientRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT)
Private Declare Sub ClientToScreen Lib "user32" (ByVal hWnd As Long, lpPoint As POINT)
Private Declare Sub OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long)
Private Type RECT
left As Integer
top As Integer
right As Integer
bottom As Integer
End Type
Private Type POINT
x As Long
y As Long
End Type
Public Sub LimitCursorMovement(ctl As Object)
Dim client As RECT
Dim upperleft As POINT
Dim lHwnd As Long
On Error Resume Next
lHwnd = ctl.hWnd
If lHwnd = 0 Then Exit Sub
GetClientRect ctl.hWnd, client
upperleft.x = client.left
upperleft.y = client.top
ClientToScreen ctl.hWnd, upperleft
OffsetRect client, upperleft.x, upperleft.y
ClipCursor client
End Sub
Public Sub ReleaseLimit()
ClipCursor ByVal 0&
End Sub
Contoh penggunaan procedureVB6 membatasi pointer mouse
Private Sub Command1_Click()
Command1.Caption = IIf(Command1.Caption = "Set Limit", "Release", "Set Limit")
If Command1.Caption = "Set Limit" Then
ReleaseLimit
Else
LimitCursorMovement Command1
End If
End Sub
Private Sub Form_Load()
Command1.Caption = "Set Limit"
End Sub