Visual C ++. NET GDI + programming foundation

xiaoxiao2021-03-06  114

GDI provides a large number of methods drawn from simple to complex graphics, and we can construct more complex graphics through operations of paths and regions, which are extremely useful in CAD. Of course, we need to get some basic content before drawing, such as coordinate space, brush and painting, etc. Coordinate space and its transformations in view and windows are always performed in a two-dimensional coordinate system. There are a variety of representations of the coordinates in accordance with different ways of action, and various different coordinates can be converted. 1. World coordinate system, equipment coordinate system and page coordinate system GDI provide us with three coordinate space: world coordinate system, page coordinate system and equipment coordinate system. The world coordinate system is a Cartesian coordinate system-independent of the application for the application to perform graphic input and output. Usually, we can define a self-coordinate system based on your needs and convenience, this coordinate system is called the user coordinate system. For example, the coordinates of Drawline (& NewPen, 20, 10, 200, 100); are based on this user coordinate system, and the pixels are used by default. The equipment coordinate system refers to the coordinates of the display device or the coordinate system of the printing device, which is characterized by in units of pixel points on the device. For views in the window, the origin of the device coordinate is incremented from left to right in the upper left corner of the client area, and the Y coordinate is incremented by the Y coordinate. Since the resolution of the device is different, the physical location of the same coordinate value may be different. As is a square having a side length of 100, the size of the display is 640 x 480 and 800 x 600 is not the same. The page coordinate system refers to a coordinate system in a certain mapping mode. The so-called mapping refers to the transformation of the world coordinate system through some way. By default, the device coordinates and page coordinates are consistent. 2. Coordinate maps and coordinate original settings In order to ensure that the results of printing or display are not affected by the device, GDI defines some mapping methods and properties to determine the relationship between device coordinates and page coordinates. These mapping methods and properties are: setPageUnit and getPageUnit These two attribute functions are used to set and get the actual metrics corresponding to each unit. It can usually have the following values: Unitdisplay - 1/75 inches per page; Unitpixel - 1 pixel per page unit, at this time, the page coordinates are the same as the device coordinates; UnitPoint - Each page unit is 1/72 inches; Unitinch - Each page unit is 1 inches; UnitDocument - per page unit is 1/300 inches; Unitmillimeter - each page unit is 1 mm. For example, or modify the drawing code in the ex_gdiplusdlg example:

CPaintDC dc (this); using namespace Gdiplus; Graphics graphics (dc.m_hDC); graphics.SetPageUnit (UnitMillimeter); Pen newPen (Color (255, 0, 0), 3); HatchBrush newBrush (HatchStyleCross, Color (255, 0 , 255, 0), Color (255, 0, 0, 255); Graphics.drawRectangle (& NewPen, 50, 50, 100, 60); Graphics.FillRectangle (& newbrush, 50, 50, 100, 60); The width is 3, and the upper left corner vertex coordinates and size units of the rectangle are millimeters, and the results are shown in the figure. These two attribute functions of SetPagescale and GetPagescale GDI are used to set up and get the zoom ratio of the page. For example, when the drawing code above is:

... graphics.SetPageUnit (Unitmillime); graphics.setpagescale ((real) 0.1); Pen Newpen (255, 0, 0), 3); ... code, REAL is a definition of a floating point type. The result of the above code is shown in Figure 2. Figure 2 TranslationTransform GDI TranslateTransform method is used to change the origin position of the coordinate, such as TranslaTransform (100, 50) to move the coordinate original to points (100, 50). The brush brush is used to draw a graphics tool for various straight lines and curves, and GDI PEN class provides a rich method for brush. In general, we can specify the color and width of the brush through its constructor, which is defined as follows: Pen (const color & color, real width); where the color is used to specify the brush color, width is used to specify the brush width. REAL is a FLOAT type definition, and Color is a color class of GDI , which can specify an ArgB color type, or use GDI predefined color values, or even convert ColorRef to Color type colors. For example, the following code is a brush that creates a width of 3, color is blue: Pen Newpen (Color (255, 0, 0, 255), 3); Pen Newpen (Color (0, 0, 255), 3 ); // When the color is only three informants, the color ALPHA component value is 255. Pen Newpen (Color :: Blue, 3); ColorRef crref = RGB (0, 0, 255); Color Color; Color.SetFromColorref (crref); Pen Newpen (Color, 3); In addition to color, GDI PEN class also Provide SetDashStyle and SetDashPattern methods to set up a predefined style and custom type of the brush. Among them, the predefined style can be: Dashstylesolid, DashstyleDash, DashstyleDot, DashstyleDashdot, DashstyleDashdotdot, DashstyleDotdot, and DashStylecustom (Customized Type). For example, the following code is shown in Figure 7.6:

USING NAMESPACE GDIPLUS; Graphics Graphics (PDC-> M_HDC); Pen Pen (255, 0, 0, 255), 15); Pen.SetDash; Graphics.drawline (& Pen, 0, 50, 400, 150 ); pen.setdash; graphics.drawline (& Pen, 0, 80, 400, 180); Pen.SetDashStyle (DashstyleDashdot); Graphics.Drawline (& Pen, 0, 110, 400, 210); however, in engineering In the application, the predefined style brush sometimes does not meet the actual needs, but must define some line types, which requires the setDashpattern function. The prototype of setdashpattern is as follows:

Status setdashpattern (const real * dasharray, int count); where Dasharray is an array containing short-drawn and intervals, and count represents the size of the array. Note that the length and interval length in the Dasharray is paired, for example, the following code is a brush using a custom type, as shown in Figure 7.7. Real dashvals [4] = {2, // Down length is 22, // Interval is 215, // Down length is 152}; // Interval is 2 PEN PEN (Color (255, 0, 0, 0), 5); Pen.SetdashPattern (Dashvals, 4); Graphics.drawline (& Pen, 5, 20, 405, 200); It is to be explained that the GDI PEN class also provides setStartCap and setEndCap methods to set up a straight start end and Terminate the style. For example, the following code is shown in Figure 7.8.

USING NAMESPACE GDIPLUS; Graphics Graphics (PDC-> M_HDC); Pen Pen (255, 0, 0, 255), 15); Pen.SetStartCap (Linecapflat); Pen.seetendcap (Linecapsquare); Graphics.Drawline (& Pen, 50, 50, 250, 50); pen.SetStartCap (LineCapRound); pen.SetEndCap (LineCapRoundAnchor); graphics.DrawLine (& pen, 50, 100, 250, 100); pen.SetStartCap (LineCapDiamondAnchor); pen.SetEndCap (LineCapArrowAnchor ); graphics.drawline (& Pen, 50, 150, 250); brush and gradient paintings for specifying filled features, GDI provides Solidbrush and Hatchbrush classes for filling color and hatching brushes. You can create a brush through their constructor, and the prototype of its constructor is as follows:

Solidbrush; Hatchbrush (HatchStyle HatchStyle, Const Color & Forecolor, Const Color & Backcolor); where ForeColor and BackColor are used to specify the background color of the shadow line color and fill, the background color can be not specified. HatchStyle is used to specify the style of the shadow line, which can be such a predefined style: HatchStyhorizontal (horizontal), HatchstyleverTical (vertical line), HatchstyleForWardDiagonal, HatchstylebackWardDiagonal (lower slash), HatchstyleCross (cross), Hatchstylecross (cross) HatchstyleDiagonalCross (crossline), etc. Of course, there are many styles such as HatchStyle30Percent (30% filled), HatchStylesoliddiamond, etc., here is not one here. Due to the previous example, the use of this simple brush has been introduced, so it is focused on the creation and use of the gradient brush. GDI provides the LineargradientBrush and PathGradientBrush class to create a straight gradient and path gradient brush. The straight gradation refers to a transition (gradient) using two colors in a rectangular area, and the transition direction can be horizontal, vertical, and diagonal direction. The prototype of the LineargradientBrush constructor is as follows:

LinearGradientBrush (Point & point1, Point & point2, Color & color1, Color & color2); LinearGradientBrush (Rect & rect, Color & color1, Color & color2, REAL angle, BOOL isAngleScalable); LinearGradientBrush (Rect & rect, Color & color1, Color & Color2, LineargradientMode Mode); where POINT1 and POINT2 are used to specify the left upper corner and lower right corner coordinates of the rectangular area, and Color1 and Color2 are used to specify the color of the gradient start and termination. RECT is used to specify the size and position of a rectangular area, and the angle is used to specify the direction angle of the gradient, and the positive value is clockwise. Isanglescalable is a parameter that is about to abolish. mode is used to specify the gradient method, it may be LinearGradientModeHorizontal (horizontal direction), LinearGradientModeVertical (vertical direction), LinearGradientModeForwardDiagonal (from lower left to upper right diagonal direction) and LinearGradientModeBackwardDiagonal (upper left to lower right diagonal direction). It should be noted that Point and RECT are GDI new data types, which are basically the same as those of the MFC's CPOINT and CRECT classes, but they cannot be mixed with each other. The path gradient brush is to fill a closed path with a gradient color. A path can be constructed from a series of straight lines and curves or other objects. The path gradient is a central color gradient mode, which performs color gradient from the center point of the path. The prototype of the PathGradientBrush constructor is as follows:

PathGradientBrush; PathGradientBrush (Const Point * Points, INT Count, WrapMode WrapMode); where Path is used to specify a path pointer, Points and counts used to specify a series of linear endpoints of the composition path and their Size, WrapMode is an option to specify the populated surrounding mode. A surrounding mode is used to decide whether to fill in the area outside the area and all regions. By default, its value is WrapModeClamp, which is filled in the area. The following code illustrates the use of the above two gradient brush:

Graphics graphics; graphicspath path; // Constructs a path path.addellipse (50, 50, 200, 100); // Use the path to construct a paintientbrush pthgrbrush (& PATH); // Put the center color Set as blue pthgrbrush.setcentercolor (Color (255, 0, 0, 255)); // Set the color around the path for bluebar, but the alpha value is 0Color colors [] = {color (0, 0, 0, 255) )}; INT count = 1; pthGrBrush.SetSurroundColors (colors, & count); graphics.FillRectangle (& pthGrBrush, 50, 50, 200, 100); LinearGradientBrush linGrBrush (Point (300, 50), Point (500, 150), Color (255, 255, 0, 0), // Red Color (255, 0, 0, 255)); // Blue Graphics.FillRectangle (& Lingrbrush, 300, 50, 200, 100); the result is shown in Figure 7.9 . It should be noted that the brush and painting can also be created with a picture. For example, the following code is shown in Figure 7.10. Graphics graphics (pDC-> m_hDC); Image image (Limage.jpg); TextureBrush tBrush (& image); Pen texturedPen (& tBrush, 10); graphics.DrawLine (& texturedPen, 25, 25, 325, 25); tBrush.SetWrapMode ( WrapModetileflipxy); Graphics.FillRectangle (& TBRUSH, 25, 100, 300, 200); Graphic Geometry Transformation Graphic Transformation is typically generating new graphics after geometry of graphics. Common two-dimensional graphics transformations have translation, proportion, symmetry, rotation, error, etc. The most effective means of graphics geometry is to use matrix transform. GDI has such matrix Matrix, which provides us with many transformation methods, such as invert (transpose), multiply, Rotate, etc. . For example, the following code is MATRIX :: Rotate an example, and the result is shown in Figure 7.11.

Graphics Graphics (PDC-> M_HDC); Pen Pen (255, 0, 0, 255)); Matrix Matrix; Matrix.Translate (40, 0); // Character Translation Matrix.Rotate (30, MatrixOrDerappend); / / After Rotate Graphics.SetTransform (& Matrix); Graphics.drawellipse (& Pen, 0, 0, 100, 50); It is necessary to explain that MatrixOrDeraPpend in the code is used to indicate the second matrix (if any) operation order is Set, MATRIX1 OP Matrix2, OP represents a certain operation; if MatrixOrderPrepend is MATRIX2 OP Matrix1. Settransform specifies a matrix to transform, the new coordinate point (x *, y *) result can be represented by the following formula: [x * y * 1] = [xy 1] = [M11X M21Y DX M12X In m22y dy 1], DX and DY are used to specify the pulse amount of the X and Y directions, if DX = DY = 0, then: (1) When M21 = M12 = 0, M11 = -1, M22 = 1 When there is x * = -x, y * = y, generate reflection graphically symmetrical reflection; (2) When M21 = M12 = 0, M11 = 1, M22 = -1, there is x * = x, y * = -Y, generate reflection patterns with X axis symmetrical; (3) When M21 = M12 = 0, M11 = M22 = -1, x * = -x, y * = -y, generates symmetrical Reflection pattern; (4) When M21 = M12 = 1, M11 = M22 = 0, x * = y, y * = x, generate reflection patterns symmetrical with the straight line Y = X; (5) When M21 = M12 = When the -1, m11 = m22 = 0, there is x * = -y, y * = -x, generates a reflection pattern that is symmetrical with the straight line Y = -X; (6) and when M11 = M22 = CoSQ, M21 = -m12 When = SINQ, a rotary change is performed. For example, the following code is shown in Figure 7.12. Graphics Graphics (PDC-> M_HDC); Pen Pen (Color :: Blue, 3); Graphics.Drawline (& Pen, 150, 50, 200, 8); Pen.SetColor (color :: gray); Matrix Matrix (-1,0 , 0, 1, 150, 50); // Use the first case Graphics.SetTransform (& Matrix); GRAPHICS.DRAWLINE (& Pen, 0, 0, 50, 30); where Matrix's constructor has the following definition:

Matrix (Real M11, REAL M12, REAL M21, REAL M22, REAL DX, REAL DY);, in addition to using Matrix, Graphics itself provides corresponding transformation methods, such as RotateTransform (Rotary), Scaletransform (Proportional transformation) and TranslateTransform (translation transformation), etc. Basic drawing functions In the previous example, we have used basic drawing functions such as Drawline. In addition, there are many such functions, and each drawing function has its overloaded form, which brings us a lot of convenience. Table 7.1 lists these basic draw functions. Table 1 GDI Common Basic Draw Function Draw Function Description Drawarc Draw a circular arc curve, the range is determined by the standing angle size, the size is specified by the rectangular or long wide value DrawBezier Draw a three-time Bezier curve determined by a series of values ​​DrawBeziers draw one series of cubic Bezier curve draw DrawClosedCurve spline DrawCurve a closed draw a spline DrawEllipse an elliptical profile plotted line specifies the size of the rectangle drawn by DrawLine value or a straight DrawPath aspect draw a path defined by the contour line drawing a GraphicsPath DrawPie sector (pie) contour DrawPolygon draw contour lines of a polygon DrawRectangle draw a rectangle FillEllipse fill a region FillPie fills an elliptical area FillPath filling a specified by the path of one sector (pie) region FillPolygon filled FillRectangle fill a polygon area a rectangular area FillRectangles fills a series of rectangular regions FillRegion to fill the inside of a region (region) to construct a complex area with two spline curves by the path, then fill it, the result is shown in Figure 7.13.

Graphics graphics (PDC-> M_HDC); Pen Pen (Color :: Blue, 3); Point Point1 (50, 200); Point Point2 (100, 150); Point Point3 (160, 180); Point Point4 (200, 200) Point Point 5 (230, 150); Point Point 6 (220, 50); Point Point7 (190, 70); Point Point8 (130, 220); Point CurvePoints [8] = {Point1, Point2, Point3, Point4, Point5 , point6, point7, point8}; Point * pcurvePoints = curvePoints; GraphicsPath path; path.AddClosedCurve (curvePoints, 8, 0.5); PathGradientBrush pthGrBrush (& path); pthGrBrush.SetCenterColor (Color (255, 0, 0, 255)); Color colors [] = {Color (0, 0, 0, 255)}; INT count = 1; pthGrBrush.SetSurroundColors (colors, & count); graphics.DrawClosedCurve (& pen, curvePoints, 8, 0.5); graphics.FillPath (& pthGrBrush & path);

转载请注明原文地址:https://www.9cbs.com/read-96951.html

New Post(0)