Step 3: Setting the Mouse Behavior

Before it can gain access to the mouse, your application must set the mouse device's behavior using the IDirectInputDevice::SetCooperativeLevel method. This method accepts the handle to the window to be associated with the device. In Scrawl, the DISCL_EXCLUSIVE flag is included to ensure that this application is the only one that can have exclusive access to the device. This flag is combined with DISCL_FOREGROUND because Scrawl is not interested in what the mouse is doing when another application is in the foreground.

The following code from Scrawl.cpp attempts to set the device's cooperative level. If this attempt fails, an error message is displayed and FALSE is returned.

hr = g_pMouse->SetCooperativeLevel(hwnd,
               DISCL_EXCLUSIVE | DISCL_FOREGROUND);
 
if (FAILED(hr)) {
    Complain(hwnd, hr, "SetCooperativeLevel(SysMouse)");
    return FALSE;
}