Drawing in the window)

Source: Internet
Author: User

Graphical device interface (GDI) and device description table (DC)

Graphical device interface (GDI)
MS-DOS manyProgramData is directly transferred to the video storage area or printer port,
The disadvantage of this approach is that the corresponding driver needs to be provided for each display card or printer type.
Windows provides an abstract interface called the graphical device interface (GDI ).
Windows has provided drivers for various display cards and printers,
In this way, our program does not need to care about the type of the display card and printer connected to the system.
Our program can deal with hardware by calling the GDI function,
Various GDI functions automatically refer to the data structure called the device environment (CDC.
Windows automatically maps the device environment structure to the corresponding physical device, and provides correct input and output commands.

Device description table (DC)

In Windows, the output of each program must be limited to its own window.
GDI uses a simple mechanism to ensure that every program drawing in a window follows this rule.
This mechanism is the device description table (DC );

When a Windows program draws a picture on a screen, printer, or other device,
It does not directly output pixels to the device, but instead draws a picture to the "display plane" in the logical sense represented by the device description table.

The device description table is a data structure that is deeply embedded in windows,
It contains all the Description fields required by GDI for display of flat conditions, including connected physical devices and various status information.

Device description table object
When you use MFC to compile a Windows program, the device description table plays a more prominent role.
In addition to being a bridge to various devices, the device description table object also encapsulates the GDI function that the program uses to generate the output.
In MFC, you do not need to capture the handle of the device description table or call the number of GDI output letters. At least you do not need to directly capture and call them,
Instead, you can create a device description table object and call its member functions to draw a picture.

Device description table handle
Before drawing on a plane, the windows program obtains the device description table handle from GDI,
Then, the handle is returned to GDI every time the output function is called.
One way to obtain the device description table in the MFC application is to call cwnd: getdc,
It returns a pointer to the CDC object in the Windows Device description table.
After drawing, use cwnd: releasedc to release the pointer. (Note: In the onpaint handler, you do not need to call these two functions)

CDC derived class
To avoid the trouble of getting and releasing the device description table,
MFC provides some CDC Derived classes, such as cpaintdc, cclientdc, and cwindowdc, which are designed to be directly instantiated.
The constructor and destructor of each class call the corresponding function to capture and release the device description table,
The device description table is convenient and simple.

For example:
Cpaintdc DC (this );
// Do some drawing
The pointer sent to the class constructor determines the window to which the device description table belongs.

Device description table attributes
When you use the CDC output function to draw images on the screen,
Some of the output features are not specified during the function call process (using the default system property drawing ),
However, you can use the device description table to obtain or obtain new properties.
MFC provides functions for obtaining and setting these attributes. We can use these functions to conveniently change the default attributes when drawing.
For example:
CDC: settextcolor // set text color
CDC: gettextcolor // obtain the text color
CDC: setbkcolor // set the background color
CDC: setbkmode // sets the background mode.
CDC: setmapmode // sets the ing mode.
CDC: setrop2 // set the drawing mode
CDC: moveTo // current location
CdCl: SelectObject // current paint brush, current paint brush, current font

(If you want to ignore the background color, you can set the background to "Transparent", DC. setbkmode (transparent );)

SelectObject Function
The CDC function that is most commonly used to define the properties of the device description table is SelectObject.
For example:
Cpen pen (ps_solid, 2, RGB (0,192, 0 ));
Cpen * poldpen = Dc-> SelectObject (& pen); // selects the new CDC object into the device description table, and saves the old CDC object
DC-> ellipse (0, 0, 100,100 );
DC. SelectObject (poldpen );//

Drawing mode and setrop2
When GDI outputs a pixel to a logical plane, it does not simply output the pixel color.
In contrast, it can combine the color of the output pixel with the color of the pixel on the target location through a series of Boolean operations.
The logical relationship used is determined by the current drawing mode of the device description table.
Use CDC: setrop2 (set raster operation to) to change the drawing mode.
The default drawing mode is r2_copypen, which copies the pixel point to the display plane.

Ing mode

Default ing mode
Fixed proportion ing mode
Programmable ing mode

In short, the ing mode is the property of the device description table, used to determine the conversion method from the logical coordinate value to the device coordinate value.

Default ing mode

The default ing mode is mm_text, which is in pixels. The X axis is positive to the left, and the Y axis is downward. The default coordinate origin is in the upper left corner.

Fixed proportion ing mode

Fixed proportion ing modes include mm_lometric, mm_himetric, mm_loenglish, mm_hienglish, and mm_twips.
Their default coordinate origins are all in the upper left corner. The difference is that the physical size of each logical unit is different.
The logical units used are 0.1mm, 0.01mm, 0.01 inch, 0.001 inch, 1/1440 inch (0.0007 inch ).

Variable proportional ing mode (Programmable ing mode)

For the ing mode with variable proportions, you can define the size of a logical unit, which can be any size.
It can also change the size with the environment change. There are two ing modes: mm_isotropic and mm_anisotropic.
The size of the logical unit is equal to the ratio of the window range and the window range.
The difference between the two lies in that the measurement units of the X and Y axes must be the same, while the latter does not.

Programmable ing mode
Mm_isotropic and mm_anisotropic are the methods by which the user decides to convert the logical coordinate value to the device coordinate value,
It is determined by the user how many device units (CM, mm, M, etc.) a logical unit is equal to, rather than by windows.
So it is called the programmable ing mode;
Mm_isotropic and mm_anisotropic ing modes are most commonly used when the image output size is adjusted proportionally according to the window size.

Differences between mm_isotropic and mm_anisotropic ing Modes
In the former, the X and Y directions have the same scaling factor, while the latter two directions can be scaled separately;

Example:
Crect rect;
Getclientrect (& rect );
DC. setmapmode (mm_isotropic );
DC. set0000wext (500,500); // sets the window range. The logic size of the window is 500 units × 500 units in height.
DC. setviewportext (rect. Width (), rect. Height (); // you can specify the range of the view.
DC. ellipse (0, 0, 500,500 );

Note: the size of the window is measured in logical units, and the size of the window is measured in device units or pixels.
Note
Mm_text is the default ing mode. The origin is in the upper left corner of the window, the positive direction of the X axis is right, and the positive direction of the Y axis is downward,
And a logical unit corresponds to a pixel under the device coordinate

In other ing modes, the origin is unchanged, but the Y axis is flipped to bring forward, and the logical unit is converted to the actual distance in proportion, instead of the number of workers.
Note that when using the metric ing mode (non-default ing mode), Y coordinates must be negative values to make the output visible.
For example:
DC. rectangle (200,100,); // draw in default mode
DC. setmapmode (mm_loenglish); // change the ing mode
DC. rectangle (200, 100,-); // draw

The logical coordinate value is transmitted to the CDC output function.
The device coordinate value refers to the corresponding pixel position in the window.

Coordinate Systems in Windows

1. screen coordinates

A coordinate system that describes physical devices (displays, printers, and so on,
The coordinate origin is in the upper left corner of the screen, the X axis is right, and the Y axis is downward. The measurement unit is pixel.
The original point, axis direction, and measurement unit cannot be changed.

2. device coordinates (also known as physical coordinates)

Device coordinates are a coordinate system that describes the forms displayed or printed on the screen and printer.
The default coordinate origin is in the upper left corner of the customer zone. The X axis is positive to the right, and the Y axis is downward.
The unit of measurement is pixel. The source and axis direction can be changed, but the measurement unit cannot be changed.

3. Logical coordinates

Logical coordinates are the coordinate system used to control the display and print in the program.
The coordinate system is closely related to the defined ing mode. The default ing mode is mm_text.
We can change the default behavior of the coordinate system by setting different ing modes.

Note:
First, remember two points:
1. The (0, 0) Point of the device coordinate is always the upper left corner of the customer zone.
2. All the points we specify during plotting are logical coordinate points.

Coordinate Conversion
Call CDC: lptodp to convert the logical coordinate to the device coordinate;
High-use CDC: dptolp can convert the device coordinates into logical coordinates;

When is the coordinate system used?

    • It can be considered that all member functions of CDC Use logical coordinates as parameters.
    • It can be considered that all the member functions of cwnd use device coordinates as their parameters.
    • Device coordinates should be considered in all selected test operations. The device coordinates should be used for the region definition.
      Some functions such as crect: ptinrect ensure correct results only when the device coordinate parameters are used.
    • Save some values that require long-term use using logical or physical coordinates.
      If you use logical coordinates to save the coordinates of a point, the coordinates of the point will no longer be valid as long as you scroll through the window.
    • The coordinates obtained by clicking an event are device coordinates!

Move Origin
CDC: setviewportorg () Move the origin of the viewport
CDC: setwindwosorg () mobile window Origin
Under normal circumstances, you can only use one of them. Using both of them will make a mess.

Get device information
Obtain system resolution
Cclientdc DC (this );
Int Cx = Dc. getdevicecaps (horzres );
Int Cy = Dc. getdevicecaps (vertres );

Drawing with GDI

The CDC class of MFC encapsulates the relevant GDI functions in the class member functions.
You can conveniently call these functions in the device description table object or by pointing to the device description table object.

Draw line
DC. moveTo (); // you can specify the current position.
DC. lineto (100,100); // specify the end point of a line

Circle
DC. ellipse (0, 0, 100,100 );
Crect rect (0, 0, 100,100 );
DC. ellipse (rect );

Draw a rectangle
DC. rectangle (100,100,); // draw a rectangle with a right angle

GDI paint brush and cpen class
Windows Draws straight lines and curves with the paint brush in the currently selected device description table,
And use rectangle, ellipse, and other graphics to generate a graphic frame drawn by the function.
By default, the paint brush draws a black solid line with pixel width.
If you want to change the line drawing method, you need to create a GDI paint brush and select the device description table from the CDC: selectojbect.

MFC uses cpen to represent the GDI paint brush.

How to Create a paint brush
The simplest way to create a paint brush is to construct a cpen object and send all the parameters used to define the paint brush to this object.
Cpen pen (ps_solid, 1, RGB (255, 0, 0 ));

The second method for creating a paint brush is to construct a Chinese cpen object with initialization and call cpen: createpen:
Cpen pen;
Pen. createpen (ps_solid, 1, RGB (255, 0, 0 ));

The third method for creating a paint brush is to construct an uninitialized cpen object and fill in the parameters that describe the paint brush feature in the logpen structure.
And then call cpen: createpenindirect to generate the paint brush.
Cpen pen;
Logpen LP;
LP. lognstyle = ps_solid; // Style
LP. lopnwidth. x = 1; // width
LP. lopncolor = RGB (255, 0, 0); // color
Pen. createpenindirect (& LP );

From the paint brush definition above, we can see that defining a paint brush requires three features: style, width, and color.
For more information about the paint brush style, see msdn.

GDI paint brush and cbrush
By default, closed images drawn by rectangle, ellipse, and other CDC functions are filled with white pixels.
You can change the fill color of the image by creating a GDI paint brush and selecting it into the device description table before drawing.
The cbrush class of MFC is encapsulated with a GDI image brush.
Paint brushes have three basic types: monochrome, shadow, and pattern.
Fill in the monochrome paint brush.
The Shadow Line paint brush uses a pre-defined cross line pattern to fill the graph shape.
The pattern painter uses bitmap to fill the image.

The method for creating a monochrome paint brush is similar to creating a paint brush, as shown below:
Cbrush brush (RGB (255, 0, 0 ));
Or
Cbrush brush;
Brush. createsolidbrush (RGB (255, 0, 0 ));

Create a line paint brush with shadow
Cbrush brush (hs_diagcross, RGB (255, 0, 0); // shadow index, colorref Value
Or
Cbrush brush;
Brush. createhatchbrush (hs_diagcross, RGB (255, 0, 0 ));
Hs_diagcross is one of the six shadow line styles available. For other styles, see the msdn

Note:
When using a Shadow Line paint brush, unless you use CDC: setbkcolor to change the current background color of the device description table,
Or use the CDC: setbkmode to change the background mode opaque to transparent to disable background filling,
Otherwise, Windows uses the Default background palladium (white) to fill in the blank area between the shadow colored lines.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.