Dal's Programming Course - Lesson 02

Learn how to draw in a window.

In the old days of Windows, a programmer would draw into a window by using the "GDI". GDI stands for the "Graphical Device Interface". It is still necessary to know a little bit about the GDI, especially what a "DC" is. A DC is an abbreviation for "Device Context". The "context" in which you draw is maintained by Windows. A context remembers stuff like what color you are drawing with, how you want your lined joined (butted, rounded), what font you are using, and way more complicated drawing attributes.

As you should be aware by now, almost all "things" that Windows manages for the programmer is done with "Handles". Handles are somewhat like C-pointers, except that you cannot "see" what they point to. Handles are really a token (actually an integer number) to hand back and forth to Windows so that Windows knows what item or construct that you are referring to.

DCs are no different. A handle to a DC is called a "HDC". You must obtain an "HDC" from windows to do any drawing.

In the new days of windows programming, the best way to draw directly in a window is to use the "Gdiplus" library. Gdiplus is a library that uses Object Oriented ("OO") methods. Before you can work with the library (after your program starts up) you must initialize it. Then, before you can do any drawing in a window, you must obtain a HDC, and pass it to an object of the Gdiplus library. Once these two tasks are accomplished, you can concentrate on using the objects from Gdiplus to do all your drawing.

Materials for this Lesson: Download.
Homework

Item

Time

Description

1 5 Mins Figure out how to read about Gdiplus in the documentation. Make sure you can get to the C version of the documentation.
2 30 Mins Read about the following objects in Gdiplus.
a. Graphics
b. Pen
c. Solidbrush
d. Rect
3 10 Mins Figure out how to link Gdiplus into your program... You will have to include the Gdiplus library. This is done in the Properties section for your project, in Solution Explorer.
4 30 Mins Using Gdiplus, modify mcBasicWindow to draw a blue square in the center of your windows. Do this by creating a routine called Paint() that is a member of mcBasicWindow. Call Paint() from your WindowProc() routine when it process the WM_PAINT windows message. Pass an HRC to Paint. Note that when you write the code, you will either have to use the scope resolution operator (::) or a "using namespace" statement when calling GDI functions. Note also, that you must call the Gdiplus initialization routines. Design a good place in your program where this will be done once.