Finding a Surface with a Device Context
You can retrieve a pointer to a surface's IDirectDrawSurface interface from the device context for the surface by calling the IDirectDraw::GetSurfaceFromDC method.
This functionality might be very useful for applications or ActiveX® controls, that are commonly given a device context to draw into at run-time, but could benefit by exploiting the functionality exposed by the IDirectDrawSurface interface.
A device context might identify memory that is not associated with a DirectDraw object, or the device context might identify a surface for another DirectDraw object entirely. If the device context does not identify a surface that was not created by that DirectDraw object, the method fails, returning DDERR_NOTFOUND.
Code Example
The following code example shows what a very simple scenario might look like.
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 hdc variable is a valid
// handle to a video memory device context, and the
// lpDD4 variable is a valid IDirectDraw interface pointer.
LPDIRECTDRAWSURFACE lpDDS;
HRESULT hr;
hr = lpDD->GetSurfaceFromDC(hdc, &lpDDS);
if(SUCCEEDED(hr)) {
// Use the surface interface.
}
else if(DDERR_NOTFOUND == hr) {
OutputDebugString(HDC not from this DirectDraw surface\n);
}
Send Feedback on this topic to the authors