Retrieving the Device Context for a Surface

If you want to modify the contents of a DirectDraw surface object by using GDI functions, you must retrieve a GDI-compatible device context handle. This could be useful if you wanted to display text in a DirectDraw surface by calling the DrawText Win32 function, which accepts a handle to a device context as a parameter. It is possible to retrieve a GDI-compatible device context for a surface by calling the IDirectDrawSurface4::GetDC method for that surface. The following example shows how this might be done:

// For this example the lpDDS4 variable is a valid pointer
// to an IDirectDrawSurface4 interface.
 
    HDC     hdc;
    HRESULT HR;
 
    hr = lpDDS4->GetDC(&hdc);
    if(FAILED(hr))
        return hr;
 
    // Call DrawText, or some other GDI
    // function here.
 
    lpDDS4->ReleaseDC(hdc);
 

Note that the code calls the IDirectDrawSurface4::ReleaseDC method when the surface's device context is no longer needed. This step is required, because the IDirectDrawSurface4::GetDC method uses an internal version of the IDirectDrawSurface4::Lock method to lock the surface. The surface remains locked until the IDirectDrawSurface4::ReleaseDC method is called.