The property of a device object determines the drawing method. When drawing using the GDI function, the color, size, and position of the drawing and text are determined by the current property of the device object, applications can use the GDI function to change the current property of a device object. This chapter describes some of the main properties of a device object and related functions used to change these properties. other properties of a device object are described in subsequent sections.
3.1 graphic Device Interface
In chapter 2, we have been using graphical device interfaces (GDI), One of the main goals of GDI is to support the creation of device-independent graphical output on output devices (such as monitors and printers. Windows graphics are mostly processed by functions in GDI. EXE (a module in Windows, called the GDI module. The GDI module controls output devices by calling routines in driver modules of different devices. For example, a display device driver is used to access hardware related to a video display. With GDI, Windows can determine what the driver can do, and because the application only deals with GDI. In this way, GDI isolates applications from different output devices so that applications can work on any graphics output devices that support Windows.
Graphics output devices can be divided into two groups: grating devices and vector devices. The grating device represents an image as a dot (pixel) pattern. Such output devices include display devices, dot matrix printers, and laser printers. Vector devices use line segments to draw images, such as mappers. Windows's GDI is a graphical language that isolates specific hardware features. Although the output device uses pixels to represent images, GDI can be used as an advanced vector graphics system or a low-level pixel operation.
When writing a Windows application, programmers do not have to worry about the color. If a color used in the application cannot be represented by the display, for Windows, you can either select the most direct pure color for the application (display the color that the device can represent) or mix several pure colors to represent this color. When a program developed on a color display runs on a monochrome display, Windows uses grayscale to represent the color. The application can also determine the characteristics of the output device in the program, such as the number of colors that can be expressed, the size of the display area of the device, and so on, so as to maximize the hardware capabilities.
3.2 device object attributes
A device object has many current attributes that determine how a GDI function works on a device object. For example, when using the TextOut () function, you only need to describe the handle of the device object, the starting coordinate of the draw character, text and text length in the function, you do not need to describe the font, text color, background color, and Character spacing, because these features are determined by the properties of the device object. Each device object has a default attribute. You can use the GDI function to change one of these attributes. Table 3-1 lists the default values for each property of a display device. When you use functions such as GetDC () and BeginPaint () to obtain a display device object for the first time, the property of this object has the default value.
Attribute |
Default Value |
View Origin |
(0, 0) |
Area range |
(1, 1) |
Window Origin |
(0, 0) |
Window range |
(1, 1) |
Background Color |
White |
Background |
OPAQUE |
Bitmap |
Any value |
Brush |
WHITE_BRUSH |
Brush Origin |
(0, 0) |
Cropping Area |
User area/invalid rectangle area/subwindow Area |
Color palette |
DEFAULT_PALETTE |
|
|
Attribute |
Default Value |
Current position of the pen |
(0, 0) |
Pen color |
BLACK_PEN |
Text color |
Black |
Device Origin |
Upper left corner of the user area |
Drawing Method |
R2_COPYPEN |
Font |
SYSTEM_FONT |
Character Spacing |
0 |
Ing method |
MM_TEXT |
Polygon filling mode |
ALTERNATE |
Relative absolute Coordinate |
ABSOLUTE |
Scaling Mode |
BLACKONWHITE |
|
In later sections of this chapter, we will introduce some device attributes, and other attributes will be described in later sections.
3.3 device coordinate system
To locate and draw graphical objects on the output device, a coordinate system must be introduced. The coordinates used by different types of devices in Windows are called device coordinates. They use the Cartesian coordinate system. In these device coordinate systems, units are represented by the number of pixels (referred to as device units ). The value on the X axis increases from left to right, and the value on the Y axis increases from top to bottom, as shown in Figure 3-1.
|
Figure 3-1 Windows Device Coordinate System |
This section uses a video display device as an example to describe the device coordinate system of Windows. Many of the content is also suitable for hard copy output devices such as printers.
In Windows, the video display device is a shared device, that is, the display device can display the output information of multiple applications at the same time. To protect the information displayed by a program from being damaged by other programs, Windows restricts the scope of application output information by viewing the display area as a different device object.
An application can obtain three different display device object handles. The device objects identified by each handle represent different areas on the screen. We can regard the objects identified by these three handles as three different abstract display devices with the device coordinate system shown in 3-1, but for different abstract devices, the coordinate origins are different. In this way, when the GetDC function (for example, TextOut () is called using different device object handles with the same starting coordinate, the information is displayed in different positions.
The first abstract device is a user area object, and its coordinate origin is in the upper left corner of the user area. The device coordinate system that locates the region is called the user coordinate system. The handle obtained using the GetDC () or BeginPaint () function is the handle that identifies the user zone. When this handle is used to identify the region's handle as a parameter of the GDI function, the coordinate value used by the GDI function is relative to the user's coordinate system.
The second abstract device is a full-window object. It includes the title bar, menu bar, scroll bar, and window frame. The coordinate system for locating this area is called the full-window coordinate system, and the upper left corner of its origin. Use the GetWindowsDC () function to obtain the handle of the device object, and then use the full-window coordinate system to draw in the area. Applications generally do not draw in this area.
The third abstract device is the entire Screen Object. Its coordinate origin is in the upper left corner of the screen. The device coordinate system located in this area is called the screen coordinate system. Statement:
HDC hDC = CreateDC ("DISPLAY", NULL );
You can obtain the handle of the device object. The coordinates used by the GDI function using the handle are relative to the screen coordinate system.
These three coordinate systems allow the program to draw images in different regions. For example, because the user zone coordinate system is used, even if the window is moved to another location on the screen, the coordinate value of the information displayed in the user zone remains the same as that in the user coordinate system.
The coordinate system solves the problem of locating the Display object. However, the coordinate system established in pixel units does not conform to the user's (or Programmer) habits, and