DirectX SDK

Step 1: Set Up the Mouse

[C++]

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

[Visual Basic]

First steps in setting up the mouse for use under 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 DirectX7
Public objDI As DirectInput
.
.
.
Set objDIDev = objDI.CreateDevice("guid_SysMouse")
Call objDIDev.SetCommonDataFormat(DIFORMAT_MOUSE)
Call objDIDev.SetCooperativeLevel(frmCanvas.hwnd, _
        DISCL_FOREGROUND Or DISCL_EXCLUSIVE)
 

This time the device takes exclusive control of the device. The result is that as long as the application has the mouse in the acquired state, 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 over full responsibility for the mouse, it must also track the position of its private cursor and also 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
 

Back in Sub Main, the application sets the buffer size so that it can receive buffered data, using the DirectInputDevice.SetProperty method:

Dim diProp As DIPROPLONG
diProp.lHow = DIPH_DEVICE
diProp.lObj = 0
diProp.lData = BufferSize    ' BufferSize is a constant
diProp.lSize = Len(diProp)
Call objDIDev.SetProperty("DIPROP_BUFFERSIZE", diProp)

Next: Step 2: Set Up Notifications