Restrict Mouse Movement to a Specific Region

Problem

You’d like to be able to restrict mouse-cursor movement to certain areas of the current form. You think it would help users of your application if the mouse stays where it needs to be until they’re done with it. How can you limit mouse movement in Access?

Solution

The Windows API’s ClipCursor subroutine will limit the movement of the mouse to a single form or region on a form, as you’ll see in this solution.

To try out this technique, load and run the frmClip form from 11-04.MDB. This form, shown in Figure 11-5, limits the mouse movement to the area of the form once you click the large button. If you click the button again or close the form, code attached to either event frees the mouse cursor to move anywhere on the screen. If you move the form, Windows frees the mouse cursor for you.

frmClip limits mouse movement to the area of the form

Figure 11-5. frmClip limits mouse movement to the area of the form

To use this technique in your own applications, follow these steps:

  1. Import the module basClipCursor from 11-04.MDB. This module contains the function declarations and user-defined types that you’ll need.

  2. To limit the mouse to a single form, you’ll need to get the form coordinates and tell Windows to use those coordinates as limits for the mouse. To do this, you’ll need code something like the following (because this code fragment uses Me, it must be in a form’s module, not a global module): ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.