Share via


Retrieving the Device Context for a Surface

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

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.

Code Example

The following code example demonstrates how to retrieve a GDI-compatible device context for a surface by calling the IDirectDrawSurface::GetDC method for that surface.

Note

To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

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

Note that the code calls the IDirectDrawSurface::ReleaseDC method when the surface's device context is no longer needed.

This step is required, because the IDirectDrawSurface::GetDC method uses an internal version of the IDirectDrawSurface::Lock method to lock the surface. The surface remains locked until the IDirectDrawSurface::ReleaseDC method is called.