11

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?

bdonlan
  • 224,562
  • 31
  • 268
  • 324
samoz
  • 56,849
  • 55
  • 141
  • 195

5 Answers5

15

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();
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
11

CDC class has operator HDC() defined which allows the compiler to convert a CDC object to HDC implicitly. Hence if you have CDC* and a function which takes HDC then you just dereference the pointer and send it to the function.

Naveen
  • 74,600
  • 47
  • 176
  • 233
3

CDC is a C++ class which - to a reasonable approximation - encapsulates an HDC, which is a handle to a device context.

The documenation which you link to describes a conversion operator, which is a C++ construct that classes can supply to allow implicit conversion from an instance of a class to some other type. In this case the implicit conversion results in the underlying handle (HDC) which the CDC instance encapsulates.

You can perform the conversion by using a CDC instance anywhere were it needs to be converted to an HDC.

Most simply:

void f( const CDC& cdc )
{
    HDC hdc = cdc;

    // use hdc here
}
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2
HDC hDC = dc;
bdonlan
  • 224,562
  • 31
  • 268
  • 324
2

Just assign it.

CDC cdc = something.
HDC hdc = cdc;
if (hdc != 0)
{
  //success...
}
ChrisW
  • 54,973
  • 13
  • 116
  • 224