This tutorial will focus on how to create and compile an application that uses GDI+. We will be starting a project from scratch using Microsoft Visual Studio. GDI+ is a powerful, object oriented API for doing mostly 2D graphics. Unlike GDI, GDI+ is generally much easier to use, much more difficult to misuse, and in many cases can produce higher quality images than GDI. While GDI+ is considered slower than GDI, it is still perfectly acceptable for most applications.

Step 1: Create a new Win32 project

Before we begin, you should have Microsoft Visual Studio installed on your computer. I have tested these steps on Microsoft Visual Studio 2005 Professional edition and Microsoft Visual Studio 2008 express edition. To create a new project, select File->New->Project. Select C++ -> Win32. Name your project, and finish creating it. Compile and run it to make sure it works.

Step 2: Modify your stdafx.h file

Modifying this file is critical if you want to compile your program using GDI+. Open stdafx.h in your project, and comment out #define WIN32_LEAN_AND_MEAN. If this is not commented out, you will get dozens of errors. For example:

1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2440: 'initializing' : cannot convert from 'const char [37]' to 'int'
1>        There is no context in which this conversion is possible
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2146: syntax error : missing ';' before identifier 'IImageBytes'
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2470: 'IImageBytes' : looks like a function definition, but there is no parameter list; skipping apparent body
1>c:\program files\microsoft sdks\windows\v6.0a\include\gdiplusimaging.h(74) : error C2059: syntax error : 'public'

Also, add the following lines to the end of stdafx.h.

#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;

Step 3: Include the gdiplus.lib library

If you want to compile using GDI+, you’ll need to link with the GDI+ library. Right click on your project in the solution explorer window and select Properties. If you don’t have this window open, you can open it using View->Solution Explorer. Once you have the project properties dialog open, select Configuration Properties->Linker->Input. In the Additional Dependencies field, simply type gdiplus.lib. Visual Studio should already have the location of this library in its default library search paths. Note that if you have multiple project configurations, debug and release, for example, you will need to re-enter gdiplus.lib for each configuration. Click apply and OK to exit the properties window.

Step 4: Create a simple function to draw a line.

In Win32, when we want to draw something, we first need an HDC, or a handle to the device context. In this case, the window we will be drawing to is the device context. Create a function, and place it in your main project file, above your WinMain or _tWinMain function.

void MyOnPaint(HDC hdc)
	Graphics graphics(hdc);
	Pen pen(Color(255,0,0,255));

This function is very similar to what can be found in an MSDN article. In GDI+, there are many classes, including Graphics and Pen. We must first create a Graphics object using the handle to the window device context. Next, we create a pen with a color. Finally, we use the DrawLine function to draw a line on the device context. The graphics class has many member functions that you will become familiar with over time.

Step 5: Initialize GDI+

When your program first starts, you will need to initialize GDI+. If you don’t, the functions simply won’t work. Add the following lines in your WinMain or _tWinMain function.

GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

Finally, just before your program exits, you’ll want to shut down GdiPlus. I’m actually unaware of what this actually does, but Microsoft recommends it, so I will too.


Step 6: Call your draw function every time the paint function is called

Whenever an event is passed to a window, it is passed through the WndProc function. If you look in your code that was created when you created the project, you’ll notice you have a WndProc function already written for your application’s window. When a WM_PAINT command is passed, that is when you’ll want to paint on the window. There should be a comment in your code already, // TODO: Add any drawing code here…

Underneath that comment, go ahead and call your function. MyOnPaint(hdc);

Step 7: Compile your code

To compile and run your code, simply press Ctrl-F5. You should see a window pop up with a blue line drawn on it. If you see the blue line, then congratulations! You now have a working program that utilizes GDI+.