Understanding Device Contexts
Most applications need to display some type of data in their windows. You'd think that,
because Windows is a device-independent operating system, creating window displays
would be easier than luring a kitten with a saucer of milk. However, it's exactly
Windows' device independence that places a little extra burden on a programmer's
shoulders. Because you can never know in advance exactly what type of devices may be
connected to a user's system, you can't make many assumptions about display capabilities.
Functions that draw to the screen must do so indirectly through something called a
Although device independence forces you, the programmer, to deal with data displays
indirectly, it helps you by ensuring that your programs run on all popular devices. In
most cases, Windows handles devices for you through the device drivers that users have
installed on the system. These device drivers intercept the data that the application
needs to display and then translates the data appropriately for the device on which it
will appear, whether that's a screen, a printer, or some other output device.
To understand how all this device independence works, imagine an art teacher trying to
design a course of study appropriate for all types of artists. The teacher creates a
course outline that stipulates the subject of a project, the suggested colors to be used,
the dimensions of the finished project, and so on. What the teacher does not stipulate is
the surface on which the project will be painted or the materials needed to paint on
that surface. In other words, the teacher stipulates only general characteristics. The
details of how these characteristics are applied to the finished project are left to each
specific artist.
For example, an artist using oil paints will choose canvas as his drawing surface and oil
paints, in the colors suggested by the instructor, as the paint. On the other hand, an
artist using watercolors will select watercolor paper and will, of course, use
watercolors instead of oils for paint. Finally, the charcoal artist will select the
appropriate drawing surface for charcoal and will use a single color.
The instructor in this scenario is much like a Windows programmer. The programmer has
no idea who may eventually use the program and what kind of system that user may
have. The programmer can recommend the colors in which data should be displayed and
the coordinates at which the data should appear, for example, but it's the device driver -
the Windows artist - who ultimately decides how the data appears.
A system with a VGA monitor may display data with fewer colors than a system with a
Super VGA monitor. Likewise, a system with a monochrome monitor displays the data in
only a single color. High-resolution monitors can display more data than lowerresolution
monitors. The device drivers, much like the artists in the imaginary art
school, must take the display requirements and fine-tune them to the device on which
the data will actually appear. And it's a data structure known as a device context that
links the application to the device's driver.
A device context (DC) is little more than a data structure that keeps track of the
attributes of a window's drawing surface. These attributes include the currently
selected pen, brush, and font that will be used to draw onscreen. Unlike an artist, who
can have many brushes and pens with which to work, a DC can use only a single pen,
brush, or font at a time. If you want to use a pen that draws wider lines, for example,
you need to create the new pen and then replace the DC's old pen with the new one.
Similarly, if you want to fill shapes with a red brush, you must create the brush and
A window's client area is a versatile surface that can display anything a Windows
program can draw. The client area can display any type of data because everything
displayed in a window - whether it be text, spreadsheet data, a bitmap, or any other type
of data - is displayed graphically. MFC helps you display data by encapsulating
Windows' GDI functions and objects into its DC classes.
No comments:
Post a Comment