Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Graphics.GetHDC method

Applies to: desktop apps only

The Graphics::GetHDC method gets a handle to the device context associated with this Graphics object.




This method has no parameters.

Return value


Type: HDC

This method returns a handle to the device context associated with this Graphics object.


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+.


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  
   // Ok to call methods on g again.
   g->DrawLine(&pen, 240, 10, 340, 60);  


Minimum supported client

Windows XP, Windows 2000 Professional

Minimum supported server

Windows 2000 Server


GDI+ 1.0


Gdiplusgraphics.h (include Gdiplus.h)





See also

FromHDC Methods
Graphics Constructors
Changes in the Programming Model



Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2014 Microsoft. All rights reserved.