WINDOWS several coordinate systems

Drawing in the window

Graphics Device Interface (GDI) and the device context (DC)

Graphics Device Interface (GDI)
of many MS-DOS programs directly to the video store or transport the printer data port, 
the disadvantage of this approach is the need to provide the appropriate display driver for each printer or card type.
Windows provides a abstract interface called Graphics Device Interface (GDI).
Windows already provides a variety of display driver card and printer,
so that our program can not care about the type of graphics card connected to the system and the printer.
Our program can call GDI functions and hardware to deal with,
and all kinds of GDI function will automatically reference data structure is called a device context (CDC) of.
Windows environment, the device automatically mapped to a corresponding physical structure of the device, and will provide the proper input and output commands.

Device Context (DC)

In a Windows environment, the output of each program must be limited to its own window.
GDI uses a simple mechanism to ensure that all procedures drawing in the window follow this rule.
This mechanism is the device context (the DC);

When Windows Paint program on the screen, printer, or other device,
it is not the pixel output directly to the device, but the mapping to "display plane" up logical sense indicated by the device context.

Device context is a data structure resides deep in Windows,
which contains all the fields described with respect to the case where the display plane GDI needs, including a wide variety of physical devices coupled to and status information.

Device context objects
when using MFC preparation of Windows programs, device context with a more prominent role.
In addition to the various devices as a bridge leading to the outside, device context object also encapsulates GDI function program for generating output.
In MFC, you do not capture device context handle and call the GDI output functions, at least do not have to capture and direct calls,
but by creating a device context object and call its member function to draw.

Device context handle
before drawing on a plane, Windows program acquiring device context handle from the GDI,
and returns a handle to the GDI function each time the output finish GDI calls.
A way to get the device context of the MFC application is to call CWnd :: GetDC,
it returns a pointer to a pointer indicating the CDC target Windows device context.
In the drawing is complete, use CWnd :: ReleaseDC release pointer (Note: In the OnPaint handler, without displaying calling these two functions)

CDC derived class
in order to avoid trouble acquiring and releasing brought by the device description,
the MFC CDC provides some derived classes, such as CPaintDC, CClientDC, CWindowDC These classes are designed to be instantiated directly.
The constructor and destructor of each class calling the corresponding function capture and release device context,
so that the device will have to use a very simple and convenient description.

For example:
CPaintDC DC (the this);
// Drawing the Do some
pointer passed to the constructor of the class identified by the device description window belongs.

Device context attributes
when using CDC output functions to draw on the screen,
some of the characteristics of the output is not predetermined (use the default drawing attributes) during the call function,
but we may be obtained or derived through the device description table itself new settings Attributes.
MFC provides a get and set functions for these attributes, we can use these functions to easily change the default attributes of the drawing.
For example:
CDC :: SetTextColor // Set the text color
CDC :: GetTextColor // get the text color
CDC :: SetBkColor // set the background color
CDC :: SetBkMode // set the background mode
CDC :: SetMapMode // set the mapping mode
CDC :: CDC :: SetROP2 // set the drawing mode
CDC :: MoveTo // current position
CDCL :: SelectObject // current pen, current brush, the current font

(If you want to ignore the background color, you can set the background to "Transparent", dc.SetBkMode (TRANSPARENT);)

SelectObject function
most commonly used to define the CDC device context attribute functions are SelectObject.
For example:
a CPen PEN (the PS_SOLID, 2, the RGB (0,192,0));
a CPen pOldPen * = dc-> the SelectObject (& PEN); // CDC object the new selected into the device context, while preserving the old CDC object
dc- > Ellipse (0,0,100,100);
dc.SelectObject (pOldPen); //

And SetROP2 drawing mode
when the GDI pixel output to the logical plane, it is not simply the output of the color pixel.
Instead, a series of Boolean operations can be output color pixels on the color pixel and the target position together with the synthesis.
It uses logic device context is determined by the current drawing mode.
Use CDC :: SetROP2 (Set Raster Operation To ) to change the drawing mode.
The default mode is the drawing R2_COPYPEN, pixels will be copied onto the display plane.

Mapping mode

The default mapping mode
fixed ratio mode mapping
programmable mapping mode

Briefly, the mapping mode is the attribute of the device context for determining values ​​from the logic coordinate to the device coordinate value conversion method.

The default mapping mode

The default mapping patterns that bring MM_TEXT, it allows the unit of pixel. X-axis to the left is positive, the Y axis is positive. The default coordinate origin in the upper left corner.

Mapping fixed ratio mode

Mapping mode has a fixed ratio MM_LOMETRIC, MM_HIMETRIC, MM_LOENGLISH, MM_HIENGLISH, MM_TWIPS species.
They are the default coordinate origin in the upper left corner. Except that each logical unit corresponding physical sizes like.
The logical units used were 0.1 mm, 0.01 mm, 0.01 inches, 0.001 inches, 1/1440 inches (0.0007 inches).

Variable scale mapping mode (programmable mapping mode)

For variable ratio mode users can define their own mapping a logical unit represents the size of which can be of any size.
It may enable this size with the environment is changed. There MM_ISOTROPIC, MM_ANISOTROPIC two mapping modes.
Its size is equal to logical units and the ratio of the viewport window range.
The former requires that the difference between the two measurement units X and Y axes to be the same, but the latter is not so limited.

 

Programmable mapping mode
MM_ISOTROPIC MM_ANISOTROPIC are determined by the user and converted into a coordinate value from the logical value of the device coordinates manner,
i.e. is determined by the logical unit number of a user equipment units (cm, mm, m, etc.) is equal to, and not by Windows .
So called programmable mapping mode;
the MM_ISOTROPIC and most MM_ANISOTROPIC mapping mode according to a window size automatically adjusted in proportion to the size of the case where the output of drawing.

MM_ISOTROPIC MM_ANISOTROPIC mapping mode and the difference
former X and Y directions have the same scaling factor, which may be scaled two separate directions;

Examples:
CRect RECT;
the GetClientRect (& RECT);
dc.SetMapMode (the MM_ISOTROPIC);
dc.SetWindowExt (500,500); // set the window range, the height of the logic window size × 500 units 500 units
dc.SetViewportExt (rect.Width ( ), rect.Height ()); // set the viewport
dc.Ellipse (0,0,500,500);

Note: The size of the window in order to calculate the logical units, the size of the viewport computing device units or pixels.
Special information
MM_TEXT the default mapping mode, the origin in the upper left corner of the window, the right X-axis positive, Y-axis positive downwards,
and a logical unit corresponds to one pixel in the device coordinate

Other mapping mode origin are unchanged, but the Y-axis so that the forward flips up, and the logical unit is converted into a scaled size of the actual distance, rather than pixels.
Note that, when using the metric mapping mode (non-default mapping mode), the output is visible, Y coordinates must be negative.
For example:
dc.Rectangle (0,0,200,100); // default mode drawing
dc.SetMapMode (MM_LOENGLISH); // change the mapping mode
dc.Rectangle (0,0,200, -100); // draw

CDC transferred to the output function is a logical coordinates.
It refers to a device coordinate values of the corresponding pixel position in the window.

 


Windows of several coordinate systems

1, the screen coordinates

Screen coordinates describe the physical devices (monitor, printer, etc.) a coordinate system,
the coordinate origin in the upper left of the screen, X-axis is positive to the right, the Y axis is positive. Unit of measurement is pixels.
Origin, the coordinate axis, units of measurement are not able to change.

2, device coordinates (also called physical coordinates)

Device coordinate system of a coordinate display is described in printed form or in the screen and printer.
The default coordinate origin is in the upper left corner of its client area. X-axis is positive to the right, the Y axis is positive.
Unit of measurement is pixels. The origin and the coordinate axis may be changed, but can not change the unit of measure.

3, the logical coordinates

Logical coordinates is displayed in the control program, print coordinate systems used.
The coordinate system is closely related to the definition of the mapping mode. The default mapping mode is MM_TEXT.
We can change the default behavior of the coordinate system by setting different mapping modes.

Note:
First of all, we should remember two things:
1. Device coordinates (0, 0) point is always the top left corner of the client area. 
2. We specified when plot points are all logical coordinates.

Coordinate conversion
call CDC :: LPtoDP logical coordinates may be converted to device coordinates;
a high CDC :: DPtoLP with device coordinates can be converted to logical coordinates;

When what coordinate system

  • You may think that all members of the CDC function are in logical coordinates as parameters
  • CWnd can think of all the member functions to device coordinates as its argument
  • All selected test operation should consider device coordinates. Defining area should be used in device coordinates.
    Some functions like CRect :: PtInRect like only when using the device coordinates will ensure correct results
  • Some values will require long-term use of logical coordinates or physical coordinates to save.
    If the logical coordinates to save the coordinates of a point, then as long as the user to scroll through window at the coordinates of the point will no longer be valid.
  • The resulting mouse click event coordinates are device coordinates!

Move the origin
CDC :: SetViewportOrg () moves the origin of the viewport 
origin CDC :: SetWindwosOrg () to move the window
under normal conditions, only one of them will engage simultaneously using two mentioned mess.

Information obtaining device
obtains the system resolution of
the CClientDC DC (the this);
int = CX dc.GetDeviceCaps (the HORZRES);
int = CY dc.GetDeviceCaps (VERTRES);

Drawing with GDI

GDI function package the MFC CDC class member functions related to the class.
In the description of the device object table or may conveniently call these functions by pointing device context object pointer

Draw lines
dc.MoveTo (0,0); // Set the current position
dc.LineTo (100,100); // the specified end position of a line

Circle
dc.Ellipse (0,0,100,100);
CRect RECT (0,0,100,100);
dc.Ellipse (RECT);

Videos rectangular
dc.Rectangle (0,0,100,100); // draw a rectangle with angled

GDI pen and CPen classes
currently selected into the device context brush drawing lines and curves, Windows with
and the function to generate the pattern drawn with the Rectangle, Ellipse, and other graphics painted border inserts.
The default paint brush is a pixel wide black solid line.
If you want to change the way of drawing a line, you need to create a GDI brush by the CDC :: SelectOjbect it selected device context

CPen MFC class represented by GDI pen.

The method of creating brush
easiest way to create a brush is configured CPen object and custom brush parameters are used to transmit the object
CPen pen (PS_SOLID, 1, RGB (255,0,0));

The second method is to construct a brush to create a Chinese CPen initialization of object and call the CreatePen :: CPen:
CPen PEN;
pen.CreatePen (the PS_SOLID, 1, RGB (255,0,0));

The third method is configured to create a brush CPen not initialized objects, filling the parameters describing the characteristics of the brush structure LOGPEN
, then call CPen :: CreatePenIndirect generating brush
CPen PEN;
LOGPEN LP;
lp.lognStyle = the PS_SOLID; // Style
lp.lopnWidth.x = 1; // width
lp.lopnColor = RGB (255,0,0); // color
pen.CreatePenIndirect (& lp);

From the above definitions of the brush, the brush needs to define three characteristics: style, width and color.
Brush style instructions, see the MSDN

GDI brush and CBrush class
default, the Rectangle, Ellipse, and other functions CDC closed figure drawn filled with white pixels.
By creating GDI Paint brush and before it is selected into the device context, the pattern can change the fill color.
The MFC class encapsulates GDI CBrush brush.
There are three basic types of brushes: color, shaded and patterned.
Monochrome brush filling monochrome.
Hatched brush fill pattern using a good cross line pattern defined in advance.
A brush pattern bitmap pattern fill.

Create method creates monochromatic brush similar to the brush, as follows:
the CBrush Brush (the RGB (255,0,0));
or
the CBrush Brush;
brush.CreateSolidBrush (the RGB (255,0,0));

Create hatched brushes
CBrush brush (HS_DIAGCROSS, RGB (255,0,0 )); // shadows index, COLORREF value
or
the CBrush Brush;
brush.CreateHatchBrush (HS_DIAGCROSS, the RGB (255,0,0));
HS_DIAGCROSS is one of the six hatching styles to choose from, and other styles, see the MSDN

Note:
When hatched brush to fill, unless you change the device context using the CDC :: SetBkColor the current background color,
or use CDC :: SetBkMode into the background mode OPAQUE TRANSPARENT, prohibit background fill,
or to default on WIndows bACKGROUND palladium (white) filling the space between the shadow color line.

 

Original Source: http://blog.programfan.com/article.asp?id=19140

Reproduced in: https: //my.oschina.net/iwuyang/blog/198618

Guess you like

Origin blog.csdn.net/weixin_33763244/article/details/91897355