Practical Web Programming

Friday, November 16, 2007

How To Limit The Cursor Movement in VB6

Limiting the cursor movement in a certain area (Ex: a form) can be restrictive to your users, but sometime certain application requires it. This codes show how to limit the cursor movement within a form or any control in Visual Basic 6.

To test it, just open a Visual Basic project with a form in it. Copy the sourcecodes and paste to the declaration section of the form. Then put two command buttons in the form.

You can limit the cursor movement within any control in Visual Basic. Just change the 'Me.hwnd' with the control's hwnd property. Ex: Command1.hWnd

'-->API Declaration
Private Declare Sub ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINT)
Private Declare Sub ClipCursor Lib "user32" (lpRect As Any)
Private Declare Sub OffsetRect Lib "user32" _
(lpRect As RECT, ByVal X As Long, ByVal Y As Long)
Private Declare Sub GetClientRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT)

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

Private Sub Command1_Click()
Dim Client As RECT
Dim Up As POINT

ClientToScreen Me.hwnd, Up
GetClientRect Me.hwnd, Client
OffsetRect Client, Up.X, Up.Y
Up.X = Client.Left
Up.Y = Client.Top
ClipCursor Client
End Sub

Private Sub Command2_Click()
ClipCursor ByVal 0&
End Sub

Private Sub Form_Load()
Command1.Caption = "&Limit Cursor"
Command2.Caption = "&Remove Cursor Limit"
End Sub

0 comments:

Recent Post