GDI +

zhaozj2021-02-08  291

In June 2001, Microsoft Released GDI (Miss GDI Plus). This technology is first applied to WindowxXp, but can be used in all Windows systems and requires GDIPLUS.DLL support.

GDI is the subsystem of the Microsoft Windows XP operating system that is responsible for displaying information on screens and printers. GDI is an application programming interface (API) that is exposed through a set of C classes.

As its name suggests, GDI is the successor to GDI, the graphics device interface included with earlier versions of Windows. Windows XP supports GDI for compatibility with existing applications, but programmers of new applications should use GDI for all their graphics needs because GDI optimizes many Of The Capabilities of GDI and Also Provides Additional Features.

A graphics device interface, such as GDI , allows application programmers to display information on a screen or printer without having to be concerned about the details of a particular display device. The application programmer makes calls to methods provided by GDI classes and those methods in turn Make The Appropriate Calls to Specific Device Drivers. GDI Insulates The Application From The Graphics Hardware, And It Is this Insulation That Allows Developers to create Device-Independent Applications.

Introduce some new features:

Gradient brushes

GDI expands on GDI by providing linear gradient and path gradient brushes for filling shapes, paths, and regions. Gradient brushes can also be used to draw lines, curves, and paths. When you fill a shape with a linear gradient brush, the color gradually changes as you move across the shape. For example, suppose you create a horizontal gradient brush by specifying blue at the left edge of a shape and green at the right edge. When you fill that shape with the horizontal gradient brush, it will gradually change from blue to green as you move from its left edge to its right edge. Similarly, a shape filled with a vertical gradient brush will change color as you move from top to bottom. The following illustration shows an ellipse filled with a horizontal gradient brush and a region filled with a diagonal gradient brush.When you fill a shape with a path gradient brush, you have a variety of options for specifying how the colors change as you move from one portion of the shape to another. One option is to have a center color and a boundary color so that the pixels change gradually from one color to the other as you move from the middle of the shape towards the outer edges. The following illustration shows a path (created from a pair of Bézier Splines) Filled with a path gradient brush.

Cardinal SPlines

GDI supports cardinal splines, which are not supported in GDI. A cardinal spline is a sequence of individual curves joined to form a larger curve. The spline is specified by an array of points and passes through each point in that array. A cardinal spline passes smoothly (no sharp corners) through each point in the array and thus is more refined than a path created by connecting straight lines. The following illustration shows two paths, one created by connecting straight lines and one created as a cardinal spline.Persistent path Objects

In GDI, a path belongs to a device context, and the path is destroyed as it is drawn. With GDI , drawing is performed by a Graphics object, and you can create and maintain several Path objects that are separate from the Graphics object. A .

Transformations and the Matrix Object

GDI provides the Matrix object, a powerful tool that makes transformations (rotations, translations, and so on) easy and flexible. A matrix object works in conjunction with the objects that are transformed. For example, a Path object has a Transform method that receives the address of a Matrix object as an argument. A single 3 × 3 matrix can store one transformation or a sequence of transformations. The following illustration shows a path before and after a sequence of two transformations (first scale, then rotate).

Scalable Regions

GDI expands greatly on GDI with its support for regions. In GDI, regions are stored in device coordinates, and the only transformation that can be applied to a region is a translation. GDI stores regions in world coordinates and allows a region to undergo any transformation (Scaling, For Example) That Can Be Stored In a Transformation Matrix. The Following Illustration Shows A Region Before Andreat A Sequence of Three Transformations: Scale, Rotate, And Translate.alpha Blending

Note that in the previous figure, you can see the untransformed region (filled with red) through the transformed region (filled with a hatch brush). This is made possible by alpha blending, which is supported by GDI . With alpha blending, you can specify the transparency of a fill color. A transparent color is blended with the background color-the more transparent you make a fill color, the more the background shows through. The following illustration shows four ellipses that are filled with the same color (red) At Different Transparency Levels.

Support for multiple image format

GDI Provides The Image, Bitmap, And Metafile Classes, Which Allow You to Load, Save and Manipulate Images in A Variety of Formats. The Following Formats Aresu Supported:

BMP GIF JPEG EXIF ​​PNG TIFF ICON WMF EMF

Programming is getting simpler:

Device Contexts, Handles, And Graphics Objects

If you have written programs using GDI (the graphics device interface included in previous versions of Windows), you are familiar with the idea of ​​a device context (DC). A device context is a structure used by Windows to store information about the capabilities of a particular display device and attributes that specify how items will be drawn on that device. A device context for a video display is also associated with a particular window on the display. First you obtain a handle to a device context (HDC), and then you pass that handle as an argument to GDI functions that actually do the drawing. you also pass the handle as an argument to GDI functions that obtain or set the attributes of the device context.When you use GDI , you do not have to be as concerned with handles and device contexts as you do when you use GDI. you simply create a Graphics object and then invoke its methods in the familiar object-oriented style-myGraphicsObject.DrawLine (parameters). The Graphics object is at the core of GDI just as the device context is at the core of GDI. The device context and the Graphics object play similar roles, but there are some fundamental differences between the handle-based programming model used with device contexts (GDI) and the object- Oriented Model Used with graphics Objects (GDI ).

The Graphics object, like the device context, is associated with a particular window on the screen and contains attributes (for example, smoothing mode and text rendering hint) that specify how items are to be drawn. However, the Graphics object is not tied to a pen, brush, path, image, or font as a device context is. For example, in GDI, before you can use a device context to draw a line, you must call SelectObject to associate a pen object with the device context. This is referred to as selecting the pen into the device context. All lines drawn in the device context will use that pen until you select a different pen. With GDI , you pass a Pen object as an argument to the DrawLine method of the Graphics class. You can use a Different Pen Object in Each of a Series of Drawline Calls without Having to Associate A Given Pen Object with a graphics Object.two Ways to Draw A Line

THE FOLLOWING TWO EXAMPLES EACH DRAW A Red Line of Width 3 from Location (20, 10) To Location (200, 100). The First Example Calls GDI, AND The Second Calls GDI THROUGH THE C Class Interface.

Drawing a line with GDI

To draw a line with GDI, you need two objects:.. A device context and a pen You get a handle to a device context by calling BeginPaint, and you get a handle to a pen by calling CreatePen Next, you call SelectObject to select THE PEN INTO The Device Context. You set the pen position to (20, 10) BY CALLING MOVETOEX AND THEN DRAW a LINE from That Pen Position To (200, 100) by calling lineto. Note That MoveToex and lineto Both Receive HDC (The Handle to the device context) as an argument.

HDC HDC;

Paintstruct PS;

HPEN HPEN

HPEN HPENOLD;

HDC = BeginPaint (HWND, & PS);

HPEN = Createpen (PS_SOLID, 3, RGB (255, 0, 0)); HPENOLD = (HPEN) SelectObject (HDC, HPEN);

MoveToex (HDC, 20, 10, NULL);

LINETO (HDC, 200, 100);

SelectObject (HDC, HPENOLD);

DeleteObject (HPEN);

Endpaint (hwnd, & ps);

Drawing a line with GDI AND THE C Class Interface

To draw a line with GDI and the C class interface, you need a Graphics object and a Pen object. Note that you do not ask Windows for handles to these objects. Instead, you use constructors to create an instance of the Graphics class ( a Graphics object) and an instance of the Pen class (a Pen object). Drawing a line involves calling the DrawLine method of the Graphics class. The first parameter of the DrawLine method is a pointer to your Pen object. This is a simpler and .

HDC HDC;

Paintstruct PS;

Pen * mypen;

Graphics * MyGraphics;

HDC = BeginPaint (HWND, & PS);

Mypen = new Pen (Color (255, 255, 0, 0), 3);

MYGRAPHICS = New Graphics (HDC);

MyGraphics-> Drawline (Mypen, 20, 10, 200, 100);

DELETE MYGRAPHICS;

Delete mypen;

Endpaint (hwnd, & ps);

Pens, Brushes, Paths, Images, And Fonts As Parameters

The preceding examples show that Pen objects can be created and maintained separately from the Graphics object, which supplies the drawing methods. Brush, Path, Image, and Font objects can also be created and maintained separately from the Graphics object. Many of the drawing methods provided by the Graphics class receive a Brush, Path, Image, or Font object as an argument. For example, the address of a Brush object is passed as an argument to the FillRectangle method, and the address of a Path object is passed as an argument to the DrawPath method. Similarly, addresses of Image and font objects are passed to the DrawImage and DrawString methods. This is in contrast to GDI where you select a brush, path, image, or font into the device context and then pass a handle To the device context as an argument to a drawing function.method overloading

. Many of the GDI methods are overloaded; that is, several methods share the same name but have different parameter lists For example, the DrawLine method of the Graphics class comes in the following forms:

STATUS DRAWLINE (in Const Pen * Pen,

In Real X1,

In Real Y1,

In Real X2,

IN real Y2);

STATUS DRAWLINE (in Const Pen * Pen,

In Const Pointf & Pt1,

In Const Pointf & PT2);

STATUS DRAWLINE (in Const Pen * Pen,

INT X1,

INT Y1,

INT X2,

IN INT Y2);

STATUS DRAWLINE (in Const Pen * Pen,

In Const Point & Pt1,

In Const Point & PT2);

All four of the DrawLine variations above receive a pointer to a Pen object, the coordinates of the starting point, and the coordinates of the ending point. The first two variations receive the coordinates as floating point numbers, and the last two variations receive the coordinates as integers. The first and third variations receive the coordinates as a list of four separate numbers, while the second and fourth variations receive the coordinates as a pair of Point (or PointF) objects.No More Current Position

Note that in the DrawLine methods shown previously both the starting point and the ending point of the line are received as arguments. This is a departure from the GDI scheme where you call MoveToEx (hdc, x1, y1, NULL) to set the current pen Position Followed by Lineto (HDC, X2, Y2) to Draw A line start at (x1, y1) AND ending at (x2, y2). GDI AS A WHOLE HAS ABANDONED The Notion of current position.

Separate Methods for Draw and Fill

GDI is more flexible than GDI when it comes to drawing the outlines and filling the interiors of shapes like rectangles. GDI has a Rectangle function that draws the outline and fills the interior of a rectangle all in one step. The outline is drawn with the currently SELECTED PEN, AND THE Interior Is Filled with the currently successd brush.

Hbrush = CreateHatchbrush (HS_CROSS, RGB (0, 0, 255));

HPEN = CREATEPEN (PS_SOLID, 3, RGB (255, 0, 0));

SelectObject (HDC, Hbrush);

SELECTOBJECT (HDC, HPEN);

Rectangle (HDC, 100, 50, 200, 80);

GDI has separate methods for drawing the outline and filling the interior of a rectangle. The DrawRectangle method of the Graphics class has the address of a Pen object as one of its parameters, and the FillRectangle method has the address of a Brush object as one of ITS parameters.hatchbrush * myhatchbrush = new hatchbrush (

HatchStylecross,

Color (255, 0, 255, 0),

Color (255, 0, 0, 255));

Pen * mypen = new Pen (Color (255, 255, 0, 0), 3);

MyGraphics.FillRectangle (MyHatchbrush, 100, 50, 100, 30);

MyGraphics.drawRectangle (Mypen, 100, 50, 100, 30);

Note that the FillRectangle and DrawRectangle methods in GDI receive arguments that specify the rectangle's left edge, top, width, and height. This is in contrast to the GDI Rectangle function, which takes arguments that specify the rectangle's left edge, right edge, top, .. and bottom Also note that the constructor for the Color class in GDI has four parameters The last three parameters are the usual red, green, and blue values; the first parameter is the alpha value, which specifies the extent to which the color being Drawn is bededed with the background color.

Constructing Regions

GDI provides several functions for creating regions: CreateRectRgn, CreateEllpticRgn, CreateRoundRectRgn, CreatePolygonRgn, and CreatePolyPolygonRgn You might expect the Region class in GDI to have analogous constructors that take rectangles, ellipses, rounded rectangles, and polygons as arguments, but that is not the. case. The Region class in GDI provides a constructor that receives a Rect object reference and another constructor that receives the address of a Path object. If you want to construct a region based on an ellipse, rounded rectangle, or polygon, you can easily do so by creating a Path object (that contains an ellipse, for example) and then passing the address of that Path object to a Region constructor.GDI makes it easy to form complex regions by combining shapes and paths. The Region class has Union and Intersect Methods That You Can Use to Augment Another Region. One Nice Feature of The GDI Scheme Is That A Path Object IS Not Destro yed when it is passed as an argument to a Region constructor. In GDI, you can convert a path to a region with the PathToRegion function, but the path is destroyed in the process. Also, a Path object is not destroyed when its address is passed as an argument to a Union or Intersect method, so you can use a given path as a building block for several separate regions. This is shown in the following example. Assume that onePath is a pointer to a path object (simple or complex) That Has Already Been Initialized.

Region region1 (RECT1);

Region region2 (Rect2);

Region1.union (OnePath);

Region2.INTERSECT (OnePath);

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

New Post(0)