GDI+ Setup

The GDI+ code that we have written uses a default Win32 project and creates a window with a single line in it that looks like this.

Part 1

We begin with a basic default Win32 project that we created. This is just the Win32 project with the code that is generated by Visual Studio when use the "Win32 Project" template. We named the project "Win32GDIPlusSetup" in the New Project dialog when we created it.

Near the top of the file, we added the lines 7-11 after the header file includes. The lines 7-8 are two more headers that we are including. Line 9 is using directive to tell the compiler to add the namespace and scope resolution operator to type names for the GDI plus types. For example, we use the types Gdiplus::Color and Gdiplus::Pen. The using directive allows us to the use them without the scope resolution operaton and namepsace name. Finally, line 11 links the library "Gdiplus.lib" after compilation.

This is the code that we added for part 1.

// 1. GDI Plus headers, namespace, and library
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment (lib,"Gdiplus.lib")
  	

Part 2

Next, we add this code at lines 38-41, just inside the main function. This is standard initialization code for GDI Plus. It is necessary that we have this before any drawing takes place.

This is the code that we added for part 2.

// 2. Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR           gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
  	

Part 3

A little later, toward the end of the main function, we added code to shutdown GDI Plus just before the program exits. This code is added at lines 66-67.

This is the code that we added for part 3.

// 3. GDI Plus Shutdown
GdiplusShutdown(gdiplusToken);
  	

Part 4

At lines 130-139, we added a function that does all of the drawing in response to a call to OnPaint(). The function takes in a handle to the device context for the window. This is an HDC type argument.

At line 133, we define a GDI Plus Graphics object. The Graphics is always needed before we draw in GDI Plus. Next, we set the smoothing mode at line 135 to high quality. This makes it so that the line will look nice when it iis drawn.

At line 137, we create a GDI Plus Pen object using a four-channel color designation that is ordered ARGB, or alpha, red, green, and blue. The second argument is width of the pen, which is set to one pixel. In line 138, we use this pen to draw a line from (75, 100) to (400, 200).

This is the code that we added for part 4.

// 4. GDI Plus drawing - draw a line
VOID OnPaint(HDC hdc)
{
	Graphics graphics(hdc);
	// This makes the drawing smooth, rather than jagged.
	graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
	// Color channels ARGB and line width
	Pen qPen(Color(255, 0, 0, 255), 1.0);
	graphics.DrawLine(&qPen, 75, 100, 400, 200);
}
  	

Part 5

Finally, inside the WndProc() function, we added lines 179-180. Line 179 is just a comment, and line 180 makes a call to OnPaint() whenever the windows receives a message to repaint itself.

This is the code that we added for part 5.

// 5. GDI Plus - Drawing function
OnPaint(hdc);
  	

    Summary: The full code that we have added for each part is shown here without comments.

  1. GDI Plus headers, namespace, and library
    #include <objidl.h>
    #include <gdiplus.h>
    using namespace Gdiplus;
    #pragma comment (lib,"Gdiplus.lib")
      	
  2. GDI Plus Initialization
    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR           gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
      	
  3. GDI Plus Shutdown
    GdiplusShutdown(gdiplusToken);
      	
  4. GDI Plus Drawing
    VOID OnPaint(HDC hdc)
    {
    	Graphics graphics(hdc);
    	// This makes the drawing smooth, rather than jagged.
    	graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
    	// Color channels ARGB and line width
    	Pen qPen(Color(255, 0, 0, 255), 1.0);
    	graphics.DrawLine(&qPen, 75, 100, 400, 200);
    }
      	
  5. GDI Plus Drawing Function Call
    OnPaint(hdc);