How can I get an HDC object from a CDC object?

samoz picture samoz · Aug 5, 2009 · Viewed 19.8k times · Source

I have an object, dc, of type CDC and I'd like to get an HDC object.

I read the MSDN documentation here, but don't really understand it.

Can someone provide me with a brief example/explanation on how to do this?

Answer

Kirill V. Lyadvinsky picture Kirill V. Lyadvinsky · Aug 5, 2009

When you have CDC object it will be implicitly converted to HDC when necessary:

CDC dc;
HDC hdc = dc; // HDC hdc = dc.operator HDC();

If you have pointer to CDC object then using function GetSafeHdc will look more clear:

CDC* pdc = SOME;
HDC hdc = pdc->GetSafeHdc();