The Graphic Device Interface for the MFC
(Page 1 of 4 )
Here we are at the third article. After revising the way Windows works through two of the most used Visual Studio tools for MFC programmers, it is time to learn something concrete. Throughout this article, the object discussed will be the GDI (Graphic Device Interface). Here we can expect to learn about the following things: printing text to the client area, some mouse input-related information, and the CPen and CBrush classes.
The world is our property. We can do anything with a little sacrifice and devotion. I can say that if you've arrived here, then you are already one step closer to achieving your goals and dreams. However, if you haven’t read the first two segments, I strongly recommend suspending this reading session and searching for the articles that describe the basic things required to continue our journey in the world of the Microsoft Foundation Class library They were also published here in the ASP Free, so finding them shouldn’t be a problem.
As promised, we will get some real MFC code. But before that, we need to go over the concept of a device: how it works, and how we can use it to our benefit. It will only be the basic knowledge. Advanced devices are beyond the purpose of this article.
General Concepts of the GDI
Windows is a graphic operating system, so it shouldn’t surprise you that everything is drawn to the screen, even the text. To simplify this process, Windows has implemented a library of functions that allow for different usage of the “graphical pictures” in a multitude of ways. As everything is an image for Windows, you must call for every modification on the screen in one of the GDIs.
Beyond the functions required for transferring the data to the screen, a GDI interface also holds a set of objects for the drawing process. This is made up of the following components:
Device context – A set of attributes that define the surface on which the images and the objects are shown.
Pen – Object for driving a line.
Brush – An object for filling a contour present on the screen.
Font – Doha, Character style…
Bitmap – One object containing an image is coming up.
Palette – Another object containing a set of colors that will be used for showing an image on the screen.
All of these help create the final image drawn to your screen. Therefore, we can conclude that a device context is a collection of attributes that will, eventually, determine the way images are shown within a window. A DC (device context) has lots of attributes, but most of them aren’t related to your trouble because when you get the DC, Windows automatically initializes them.
Of course, you can change them manually. For example, the color of the Pen will always be black. If you want to write text with a purple color, you will have to change this attribute. It can easily be done because, as I said before, all is created for you. Should you want to customize its particularities, then that’s your only task.
A DC must always have a full set of instruments; you can’t eliminate some instruments from it. But you can switch between them. So when you want to go back to the default GDI object, all you do is change it with the default instrument. Also a DC represents the borders of a specific window. Each time you draw outside of it, Windows automatically makes a fast resize to avoid this “drawing outside the box” scenario. This way you are limited only to drawing in the client area, not outside of it.
This entire GDI story is integrated in a series of classes for the sole purpose of simplifying its usage. The biggest of them is the CDC class. The MFC library derives a few classes from it:
CClientDC – Context for assuring access to the client area of a window. This should be used as a response to any of the messages, excluding one: the WM_PAINT message.
CPaintDC - The reason for the prior exception is this class, as this was designed for that individual message, so it must be used only for it.
CMetaFileDC - A context that holds a metafile and is under Windows. It is a file that contains a list of commands for the fast reproduction of a picture.
CWindowDC – The context that gives you access to the entire window, not just the client area.
We’ve just finished the necessary theoretical knowledge! Should you have trouble understanding the DCs, have no fear, as everything will become clear with the example presented on the next page; we’ll learn to write text on the client area.
Next: Practice and Text >>
More BrainDump Articles
More By Gabor Bernat