DirectX SDK

Retrieving the Device Context for a Surface

[C++]

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 IDirectDrawSurface7::GetDC method for that surface. The following example shows how this might be done:

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

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

[Visual Basic]

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. It is possible to retrieve a GDI-compatible device context for a surface by calling the DirectDrawSurface7.GetDC method for that surface.

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