100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

MFC and GDI+

How to integrate GDI+ into an MFC application for anti-aliased vector graphics, alpha-blended bitmaps, and richer image format support alongside classic GDI.

GraphicsIntermediate10 min readJul 10, 2026
Analogies

Why Reach for GDI+ from MFC

GDI+ is a separate, C++-object-based graphics library (Gdiplus::Graphics, Gdiplus::Pen, Gdiplus::Bitmap) layered on top of GDI, offering features GDI lacks natively: anti-aliased line and shape drawing (SmoothingMode), alpha-blended compositing for translucent overlays, gradient brushes, and built-in decoding of PNG/JPEG/GIF/TIFF via Gdiplus::Image without hand-rolled codecs. MFC doesn't provide first-class GDI+ wrapper classes the way it does for GDI, so applications typically construct a Gdiplus::Graphics object directly from an existing HDC obtained through CDC.

🏏

Cricket analogy: Like switching from a standard bat to a lighter, more balanced custom-weighted bat for the finer touch shots a batter like AB de Villiers plays, GDI+ offers finer control — anti-aliasing, alpha blending — than the coarser tools of classic GDI.

Bootstrapping GDI+ in an MFC Project

GDI+ requires explicit initialization via GdiplusStartup and matching GdiplusShutdown, typically wired into CWinApp::InitInstance and ExitInstance using a Gdiplus::GdiplusStartupInput structure and a stored ULONG_PTR token. You must also link gdiplus.lib and include <gdiplus.h> after <windows.h>, and because the Gdiplus namespace defines a Gdiplus::Color and other symbols that can collide with MFC/ATL macros like min/max, projects commonly wrap the include with NOMINMAX defined or use the Gdiplus:: prefix explicitly.

🏏

Cricket analogy: Like a stadium's floodlights needing a formal power-up sequence before a day-night match can begin and a shutdown sequence afterward, GDI+ requires GdiplusStartup before any drawing and GdiplusShutdown when the app exits.

Drawing with Graphics, Pen, and SolidBrush

Inside OnDraw, you construct a Gdiplus::Graphics object from the CDC's HDC (Graphics graphics(pDC->GetSafeHdc())), set SmoothingMode to SmoothingModeAntiAlias for smooth curves, and then draw using Gdiplus::Pen and Gdiplus::SolidBrush objects that accept a Gdiplus::Color with an alpha channel — the fourth ARGB component — enabling true translucency that plain GDI brushes cannot express since COLORREF has no alpha.

🏏

Cricket analogy: Like a broadcaster overlaying a semi-transparent score bug on top of live footage so the pitch remains visible underneath, Gdiplus::Color's alpha channel lets a SolidBrush paint a translucent overlay that blends with what's beneath it.

cpp
// In CMyApp::InitInstance()
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);

// In CMyApp::ExitInstance()
Gdiplus::GdiplusShutdown(m_gdiplusToken);

// In CMyView::OnDraw(CDC* pDC)
using namespace Gdiplus;
Graphics graphics(pDC->GetSafeHdc());
graphics.SetSmoothingMode(SmoothingModeAntiAlias);

Pen pen(Color(255, 0, 102, 204), 2.0f);
SolidBrush brush(Color(120, 255, 165, 0)); // alpha = 120: translucent fill

graphics.FillEllipse(&brush, 20, 20, 180, 120);
graphics.DrawEllipse(&pen, 20, 20, 180, 120);

Mixing GDI and GDI+ Safely

Because GDI+ operates on the same HDC as GDI, you can freely interleave classic CDC calls with GDI+ drawing in the same OnDraw as long as you respect ordering and object lifetime: create the Gdiplus::Graphics object, do your GDI+ drawing, and let it go out of scope (or destroy it) before continuing with unrelated GDI calls on that same DC to avoid state conflicts. A frequent mistake is holding a Gdiplus::Graphics object alive across a SelectObject call on the underlying DC, or constructing Graphics from an HDC that gets released before the Graphics object is destroyed.

🏏

Cricket analogy: Like two different commentary teams — the TV feed and radio feed — describing the same live match without stepping on each other's audio, GDI and GDI+ can both draw to the same HDC as long as their operations don't overlap carelessly.

A Gdiplus::Graphics object constructed from a raw HDC does not take ownership of that handle. If you release or destroy the underlying CDC (for example, by letting a CClientDC go out of scope) while the Graphics object is still alive, subsequent GDI+ calls on it become invalid. Always ensure the Graphics object's lifetime is fully nested inside the DC's lifetime.

  • GDI+ is a separate, more expressive graphics API offering anti-aliasing, alpha blending, gradients, and built-in image codecs.
  • GDI+ requires explicit GdiplusStartup/GdiplusShutdown, typically wired into CWinApp::InitInstance/ExitInstance.
  • A Gdiplus::Graphics object is constructed from an existing HDC obtained via CDC::GetSafeHdc.
  • SmoothingModeAntiAlias enables smooth, anti-aliased rendering of curves and diagonal lines.
  • Gdiplus::Color's ARGB alpha channel enables true translucency that plain COLORREF-based GDI brushes cannot express.
  • GDI and GDI+ can share the same HDC but their operations should be cleanly sequenced, not interleaved carelessly.
  • A Graphics object must not outlive the DC/HDC it was constructed from.

Practice what you learned

Was this page helpful?

Topics covered

#MFCMicrosoftFoundationClassesStudyNotes#MicrosoftTechnologies#MFCAndGDI#MFC#GDI#Reach#Bootstrapping#StudyNotes#SkillVeris#ExamPrep