Microsoft DirectX 8.0 (C++)

Implementing Capture Focus

The concept of capturing focus is integral to creating lobbyable game applications and lobby applications with Microsoft® DirectPlay® Voice support. If your game application does not properly implement focus capture, it is possible that voice communication will not function if your game was launched from a lobby application.

Before the DirectPlay voice session is created, there are several initialization steps to be completed:

// Create a DirectPlay voice client interface
hr = CoCreateInstance( CLSID_DirectPlayVoiceClient,
                       NULL, 
                       CLSCTX_INPROC_SERVER,
                       IID_IDirectPlayVoiceClient, 
                       (LPVOID*) &m_pVoiceClient )

// Set the hwndAppWindow member to the window that will have
// focus when your game application is running
DVSOUNDDEVICECONFIG dvSoundDeviceConfig;

dvSoundDeviceConfig.hwndAppWindow = hWnd;
// Set dwFlags to DVSOUNDCONFIG_STRICTFOCUS so that only application
// windows that are in the foreground will gain focus
dvSoundDeviceConfig.dwFlags = DVSOUNDCONFIG_STRICTFOCUS;

// Connect to the voice session
hr = m_pVoiceClient->Connect( &dvSoundDeviceConfig, 
                              pdvClientConfig, 
                              DVFLAGS_SYNC )

Once the application is connected to the session, message will be received by your DirectPlay voice callback. The two messages which you must handle to properly implement capture focus are DVMSGID_GAINFOCUS and DVMSGID_LOSTFOCUS.

HRESULT CALLBACK DirectPlayVoiceClientMessageHandler(
                     LPVOID lpvUserContext,
                     DWORD dwMessageType,
                     LPVOID lpMessage )
{
    switch( dwMessageType )
    {
     case DVMSGID_GAINFOCUS:
        // the game application has gained focus in the system
            break;
        case DVMSGID_LOSTFOCUS:
        // the game application has lost focus in the system
            break;
    }
  ...
}

If recording is muted, focus will be lost. DirectPlay voice will send a DVMSGID_LOSTFOCUS message to your callback. When recording is unmuted, focus will be regained, with the exception that applications using DVSOUNDCONFIG_STRICTFOCUS will only gain focus is the application window is in the foreground.

DVCLIENTCONFIG dvClientConfig;
dvClientConfig.dwFlags = DVCLIENTCONFIG_RECORDMUTE;

// Mute recording, DVMSGID_LOSTFOCUS is sent to DirectPlay
// voice callback function
m_pVoiceClient->SetClientConfig(dvClientConfig);