Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The Graphics::GetHDC method gets a handle to the device context associated with this Graphics object.
Syntax
HDC GetHDC();
Return value
Type: HDC
This method returns a handle to the device context associated with this Graphics object.
Remarks
Each call to the Graphics::GetHDC method of a Graphics object should be paired with a call to the Graphics::ReleaseHDC method of that same Graphics object. Do not call any methods of the Graphics object between the calls to Graphics::GetHDC and Graphics::ReleaseHDC. If you attempt to call a method of the Graphics object between Graphics::GetHDC and Graphics::ReleaseHDC, the method will fail and will return ObjectBusy.
Any state changes you make to the device context between Graphics::GetHDC and Graphics::ReleaseHDC will be ignored by GDI+ and will not be reflected in rendering done by GDI+.
Examples
The following function uses GDI+ to draw an ellipse, then uses GDI to draw a rectangle, and finally uses GDI+ to draw a line. The function's one parameter is a pointer to a GDI+ Graphics object. The code calls the Graphics::DrawEllipse method of that Graphics object to draw an ellipse. Next, the code calls the Graphics::GetHDC method to obtain a handle to the device context associated with the Graphics object. The code draws a rectangle by passing the device context handle to the GDI Rectangle function. The code calls the Graphics::ReleaseHDC method of the Graphics object and then uses the Graphics object to draw a line.
VOID Example_GetReleaseHDC(Graphics* g)
{
   Pen pen(Color(255, 0, 0, 255));
   g->DrawEllipse(&pen, 10, 10, 100, 50);  // GDI+
   
   HDC hdc = g->GetHDC();
   
      // Make GDI calls, but don't call any methods
      // on g until after the call to ReleaseHDC.
      Rectangle(hdc, 120, 10, 220, 60);  // GDI  
   g->ReleaseHDC(hdc);
   
   // Ok to call methods on g again.
   g->DrawLine(&pen, 240, 10, 340, 60);  
}
Requirements
| Requirement | Value | 
|---|---|
| Minimum supported client | Windows XP, Windows 2000 Professional [desktop apps only] | 
| Minimum supported server | Windows 2000 Server [desktop apps only] | 
| Target Platform | Windows | 
| Header | gdiplusgraphics.h (include Gdiplus.h) | 
| Library | Gdiplus.lib | 
| DLL | Gdiplus.dll |