Microsoft DirectX 8.1 (Visual Basic)

Step 1: Setting Up the Mouse

First steps in setting up the mouse for use under Microsoft® DirectInput® are similar to those taken in Tutorial 1: Using the Keyboard. In the ScrawlB sample, initialization takes place in Sub Main after some global declarations.

Public objDX As New DirectX8
Public objDI As DirectInput8
.
.
Set objDIDev = objDI.CreateDevice("guid_SysMouse")
Call objDIDev.SetCommonDataFormat(DIFORMAT_MOUSE)
Call objDIDev.SetCooperativeLevel(frmCanvas.hwnd, _
        DISCL_FOREGROUND Or DISCL_EXCLUSIVE)
 

As opposed to the instance of the keyboard, DirectInput takes exclusive control of the mouse. The result is that as long as the application has the mouse in the acquired state, Microsoft Windows® does not generate mouse messages or display the system cursor. Note that DISCL_EXCLUSIVE must be combined with DISCL_FOREGROUND; it is not possible for an application to have exclusive access to the mouse and also receive input when it loses the focus.

Because the ScrawlB sample application is taking full responsibility for the mouse, it must also track the position of its private cursor, and it must scale movement of the cursor to movements of the mouse. The following global variables are used to store the cursor coordinates (in pixels relative to the upper left corner of the main form) and movement scaling.

Public g_cursorx As Long
Public g_cursory As Long
Public g_Sensitivity
 

In Sub Main, the application sets the buffer size so that it can receive buffered data, using the DirectInputDevice8.SetProperty method.

Dim diProp As DIPROPLONG
diProp.lHow = DIPH_DEVICE
diProp.lObj = 0
diProp.lData = BufferSize    ' BufferSize is a constant

Call objDIDev.SetProperty("DIPROP_BUFFERSIZE", diProp)

You are now ready to specify how mouse events will trigger data retrieval in Step 2: Setting Up Notifications