Platform SDK: DirectX

Step 3: Manage Exclusive Access to the Mouse

[C++]

This topic pertains only to application development in Visual Basic. See DirectInput C/C++ Tutorials.

[Visual Basic]

The ScrawlB sample demonstrates using the mouse with the exclusive foreground cooperative level. At this level, Windows does not track the mouse while it is acquired by the application. There are three major consequences for the application:

ScrawlB handles the first responsibility by keeping a private record of mouse movements, adjusted by a user-defined sensitivity value, and displaying an icon that serves as a cursor for drawing.

ScrawlB loses acquisition of the mouse involuntarily whenever the application window moves to the background. When it comes to the foreground again, the application automatically reacquires the mouse by calling DirectInputDevice.Acquire in the MouseMove event handler, which is called whenever Windows sends a mouse message to the application. This happens as soon as the system cursor moves over the client window, or the client window gains the focus under the system cursor. Once ScrawlB reacquires the mouse, of course, no more Windows mouse messages are sent, so the Form_MouseMove method is not called in response to subsequent mouse events.

In order to let the user navigate the context menu, or to display the system cursor for some other purpose such as resizing the window, ScrawlB calls the DirectInputDevice.Unacquire method whenever the user opens the menu. When the menu is closed, Form_MouseMove is called and the mouse is reacquired, unless the user has chosen Suspend from the menu, in which case a flag is set. This flag prevents Form_MouseMove from reacquiring, so that the user can continue using the system cursor.

The sample application demonstrates one more technique for releasing the mouse. If the user opens the system menu by pressing ALT+SPACE, Windows sends a WM_ENTERMENULOOP message. This message is intercepted by a subclassed window procedure, which then unacquires the mouse. As long as Windows is using the system cursor for navigating the menu or allowing the user to move or resize the window, it sends no mouse messages to frmCanvas, so the application doesn't attempt to reacquire the mouse in Form_MouseMove. A similar technique could be used for intercepting other Windows messages such as WM_ACTIVATE or WM_ACTIVATEAPP, so that the application could fully control acquisition and unacquisition of the mouse in response to gaining and losing the focus.

Next: Step 4: Retrieve Buffered Data from the Mouse