Confining Your Mouse to a Form

Confining Your Mouse to a Form

With Modal forms there is a need to restrict the movement of the mouse to the form until user closes the form. This can be achieved by using the ClipCursor API. ClipCursor API takes one parameter, the rectangular area of type RECT to which the mouse will be confined.

 Public Sub Clip()1Dim iRect as RECT' first populate the iRect with Form's dimensions & then call the ClipCursor APIWith iRect  .Left = Form1.Left  .Top = Form1.Top  .Right = Form1.Left + Form1.Width  .Bottom = Form1.Top + Form1.HeightEnd WithClipCursor iRectEnd Sub' and finally in Form's Unload event call ClipCursor again to release the mouse :-ClipCursor 0&

Note that RECT expects dimensions of the form in pixels, as is the case with all APIs. Set the form’s ScaleMode to vbPixels before calling the ClipCursor API.


Share the Post: