11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8. DC, Pen, Brush and Palette<br />

Chapter 8<br />

DC, Pen, Brush and Palette<br />

8.0 Device Context & GDI Objects<br />

Starting from this chapter, we are going to study topics on GDI (Graphics Device Interface)<br />

programming.<br />

Situation<br />

GDI is a standard interface between the programmer and physical devices. It provides many functions<br />

that can be used to output various objects to the hardware (e.g. a display or a printer). GDI is very<br />

important because, as a programmer, we may want our applications to be compatible with as many<br />

peripherals as possible. For example, almost every application need to write to display, and many<br />

applications also support printer output. The problem here is that since a program should be able to run on<br />

different types of devices (low resolution displays, high resolution displays with different color depth, etc.),<br />

it is impossible to let the programmer know the details of every device and write code to support it<br />

beforehand.<br />

The solution is to introduce GDI between the hardware and the programmer. Because it is a standard<br />

interface, the programmer doesn’t have to have any knowledge on the hardware in order to operate it. As<br />

long as the hardware supports standard GDI, the application should be able to execute correctly.<br />

Device Context<br />

As a programmer, we do not output directly to hardware such as display or printer. Instead, we output<br />

to an object that will further realize our intention. This object is called device context (DC), it is a<br />

Windows object that contains the detailed information about hardware. When we call a standard GDI<br />

function, the DC implements it according to hardware attributes and configuration.<br />

Suppose we want to put a pixel at specific logical coordinates on the display. If we do not have GDI,<br />

we need the following information of the display in order to implement this simple operation:<br />

1) Video memory configuration. We need this information in order to convert logical coordinates to<br />

physical buffer address.<br />

1) Device type. If the device is a palette device, we need to convert a RGB combination to an index to the<br />

color table and use it to specify a color. If the device is a non-palette device, we can use the RGB<br />

combination directly to specify a color.<br />

Because the actual devices are different form one type to another, it is impossible for us to gather<br />

enough information to support all the devices in the world. So instead of handling it by the programmer,<br />

GDI functions let us use logical coordinates and RGB color directly, the conversion will be implemented<br />

by the device driver.<br />

200

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!