devxlogo

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&

size=3>
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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist