1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

Software Solution for Engineers and Scientist Episode 8 pptx

90 341 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Software Solution for Engineers and Scientist Episode 8 PPTX
Trường học University of Science and Technology
Chuyên ngành Software Engineering
Thể loại Lecture Presentation
Năm xuất bản 2024
Thành phố Hanoi
Định dạng
Số trang 90
Dung lượng 425,98 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

For example, the GDI function GetTextColor re-trieves the current text color from the device context, while the functionSetTextColor is used to change the text color attribute.. 23.2.2 D

Trang 1

Device context operations belong to two types: those that obtain informationand those that set attributes For example, the GDI function GetTextColor() re-trieves the current text color from the device context, while the functionSetTextColor() is used to change the text color attribute Although these func-tions are sometimes referred to as get- and set-types, the function names do notalways start with these words For example, the SelectObject() function is used toboth get and set the attributes of pens, brushes, fonts, and bitmaps.

Graphics applications often need to obtain information regarding the devicecontext For example, a program may need to know the screen resolution or thenumber of display colors One of the most useful functions for obtaining informa-tion regarding the capabilities of a device context is GetDeviceCaps() The call toGetDeviceCaps() requires two parameters: the first one is the handle to the devicecontext, and the second one is an index value that identifies the capability beingqueried Table 23.1 lists some of the most useful information returned by thisfunction

Table 23.1

Information Returned by GetDeviceCaps()

DRIVERVERSION Version number of device driver

TECHNOLOGY Any one of the following:

DT_RASDISPLAY Raster displayDT_RASPRINTER Raster printerDT_RASCAMERA Raster cameraDT_CHARSTREAM Character stream

DT_DISPFILE Display fileHORZSIZE Width of the physical screen (millimeters)

VERTSIZE Height of the physical screen (millimeters)

HORZRES Width of the screen (pixels)

VERTRES Height of the screen (raster lines)

LOGPIXELSX Number of pixels per logical inch along the screen

width

LOGPIXELSY Number of pixels per logical inch along the screen

height

BITSPIXEL Number of color bits per pixel

NUMBRUSHES Number of device-specific brushes

NUMPENS Number of device-specific pens

NUMFONTS Number of device-specific fonts

NUMCOLORS Number of entries in the color table, if the

device has a color depth of no more than 8 bitsper pixel Otherwise, –1 is returned

ASPECTX Relative width of a device pixel used for line

Trang 2

Table 23.1

Information Returned by GetDeviceCaps() (continued)

CLIPCAPS Flag indicating clipping capabilities of the

device Value is 1 if the device can clip to arectangle Otherwise, it is 0

SIZEPALETTE Number of entries in the system palette

NUMRESERVED Number of reserved entries in the system palette

COLORRES Actual color resolution of the device, in bits per

pixel

PHYSICALWIDTH For printing devices: the width of the physical

page, in device units

PHYSICALHEIGHT For printing devices: the height of the physical

page, in device units

PHYSICALOFFSETX For printing devices: the distance from the left

edge of the physical page to the left edge of theprintable area, in device units

PHYSICALOFFSETY For printing devices: the distance from the top

edge of the physical page to the top edge of theprintable area, in device units

RASTERCAPS Value that indicates the raster capabilities of

the device, as follows:

RC_BANDING Requires banding support

RC_BITBLT Capable of transferring bitmaps.RC_BITMAP64 Supports bitmaps larger than

RC_PALETTE Palette-based device

RC_SCALING Capable of scaling

RC_STRETCHBLT Capable of performing the

CC_NONE Does not support curves

CC_CIRCLES Device can draw circles

CC_PIE Device can draw pie wedges.CC_CHORD Device can draw chord arcs

CC_ELLIPSES Device can draw ellipses

CC_WIDE Device can draw wide borders.CC_STYLED Device can draw styled borders.CC_WIDESTYLED Device can draw wide and styled

borders

CC_INTERIORS Device can draw interiors

CC_ROUNDRECT Device can draw rounded

rectangles

(continues)

Trang 3

LC_NONE Does not support lines.

LC_POLYLINE Device can draw a polyline

LC_MARKER Device can draw a marker

LC_POLYMARKER Device can draw multiple

markers

LC_WIDE Device can draw wide lines.LC_STYLED Device can draw styled lines.LC_WIDESTYLED Device can draw lines that

are wide and styled

LC_INTERIORS Device can draw interiors

POLYGONALCAPS Indicates the polygon capabilities of the device,

as follows:

PC_NONE Does not support polygons

PC_POLYGON Device can draw alternate-fill

Are wide and styled

PC_INTERIORS Device can draw interiors

TEXTCAPS Indicates the text capabilities of the device, as

TC_SF_X_YINDEP Device can scale independently

in the x- and y-directions

TC_SA_DOUBLE Device is capable of doubled

character for scaling

TC_SA_INTEGER Device uses integer multiples

only for character scaling

TC_SA_CONTIN Device uses any multiples for

exact character scaling

(continues)

Trang 4

TC_IA_ABLE Device can italicize.

TC_UA_ABLE Device can underline

TC_SO_ABLE Device can draw strikeouts

TC_RA_ABLE Device can draw raster fonts.TC_VA_ABLE Device can draw vector fonts.TC_SCROLLBLT Device cannot scroll using a

bit-block transfer

23.2.2 DC Info Demonstration Program

The program named DCI_DEMO, located in the DC Info Demo project folder on thebook's on-line software package, shows how to obtain device context information.The menu labeled "DC Info" contains commands for displaying the most used generaldevice context capabilities, the device driver version, as well as the specific line andcurve drawing capabilities Figure 23.1 shows the various menu commands in theDCI_DEMO program

Figure 23.1 Screen Snapshots of the DC Info Program

Trang 5

The Capabilities command in the DC Info menu displays the device context ues for some of the most used elements returned by the GetDeviceCaps() func-tion To simplify the programming, the data required during processing is stored

val-in a header file named DC_Caps.h, which can be found val-in the project directory.The header file is formatted as follows:

// Header file for DC Info Demo project

// Contains array of structures

#define LINES ((int) (sizeof DCcaps / sizeof DCcaps [0]))

HORZSIZE, "HORZSIZE", "Width (in mm):",

VERTSIZE, "VERTSIZE", "Height (in mm):",

HORZRES, "HORZRES", "Width (in pixels):",

by dividing the number of entries in the structure by the number of elements ineach entry This coding allows us to change the number of entries in the arraywithout having to change the loop

// Obtain and display DC capabilities

for (i = 0 ; i < LINES ; i++) {

TextOut (hdc, cxChar, cyChar * (1 + i),

SetTextAlign (hdc, TA_RIGHT | TA_TOP) ;

TextOut (hdc, cxChar + 16 * cxCaps + 40 * cxChar,

cyChar * (1 + i), szBuffer,

Trang 6

vari-tion that is part of the third call to TextOut() In this case the iIndex element in thearray is used as the second parameter to the call The wsprintf() function takes care

of converting and formatting the integer value returned by GetDeviceCaps() into adisplayable string

Obtaining and displaying the driver version is much simpler The coding is as lows:

fol-// Get driver version

_itoa(GetDeviceCaps(hdc, DRIVERVERSION),

szVersion + 16, 10);

// Initialize rectangle structure

SetRect (&textRect, // address of structure

2 * cxChar, // x for start

cyChar, // y for start

cxClient, // x for end

cyClient); // y for end

DrawText( hdc, szVersion, -1, &textRect,

DT_LEFT | DT_WORDBREAK);

break;

In this case we use the _itoa() function to convert the value returned byGetDeviceCaps() into a string SetRect() and DrawText() are then used to formatand display the string

Obtaining and displaying the curve drawing and line drawing capabilities of thedevice context requires different processing These values (see Table 23.1) are re-turned as bit flags associated with an index variable For example, we make the call

to GetDeviceCaps() using the index constant CURVECAPS as the second parameter.The integer returned by the call contains all the bit flags that start with the prefix CC(CurveCaps) in Figure 23.1 Code can then use a bitwise AND to test for one or more

of curve drawing capabilities The following code fragment shows one possible proach for obtaining curve-drawing capabilities:

ap-// Get curve drawing capabilities

curvecaps = GetDeviceCaps (hdc, CURVECAPS);

// Test individual bit flags and change default

// Initialize rectangle structure

SetRect (&textRect, // address of

// structure

2 * cxChar, // x for start cyChar, // y for start cxClient, // x for end cyClient); // y for end

Trang 7

DT_LEFT | DT_WORDBREAK);

break;

Each of the if statements in the processing routine tests one of the bit flags turned by GetDeviceCaps() If the bit is set, then a text string containing thewords YES or NO is moved into the display string When all the bits have been ex-amined, the message string named szCurvCaps is displayed in the conventionalmanner

re-23.2.3 Color in the Device Context

Monochrome displays are a thing of the past Virtually all Windows machines have acolor display and most of them can go up to 16.7 million displayable colors In graph-ics programming you will often have to investigate the color capabilities of a device

as well as select and manipulate colors

In Windows programming, colors are defined by the relative intensity of thered, green, and blue primary components Each color value is encoded in 8 bits,therefore, all three primary components require 24 bits Since no C++ data type isexactly 24 bits, however, the color value in Windows is stored in a type calledCOLORREF, which contains 32 bits The resulting encoding is said to be in RGBformat, where the letters stand for the red, green, and blue components, respec-tively Figure 23.2 shows the bit structure of the COLORREF type

Figure 23.2 COLORREF Bitmap

Windows provides a macro named RGB, defined in the windows.h header file;

it simplifies entering the color values into a data variable of type COLORREF Themacro takes care of inserting the zeros in bits 24 to 31, and in positioning eachcolor in its corresponding field As the name RGB indicates, the first value corre-sponds to the red primary, the second one to the green, and the third one to theblue For example, to enter a middle-gray value, in which each of the primary col-ors is set to 128, proceed as follows:

COLORREF midGray; // Variable of type COLORREF

0 for explicit RGB format

1 for palette-index format

2 for palette-relative format

Trang 8

plicit RGB value; if it is 1 then it is a palette-index value; if it is 2 then the color is apalette-relative value Using the RGB macro when creating explicit-RGB values en-sures that the high-order byte is set correctly.

Obtaining color information from the device context requires careful ation Note in Table 23.1 that the index constant NUMCOLORS is valid only if thecolor depth is no more than 8 bits per pixel The device queried in Figure 23.1 has 16bits per pixel; therefore, the NUMCOLORS value is set to –1 By the same token, theCOLORRES index constant is valid only if the device sets the RC_PALETTE bit InFigure 23.1 the value of this field is 0 The two most useful constants for obtaininggeneral color depth information are PLANES and BITPIXEL PLANES returns thenumber of color planes and BITPIXEL returns the number of bits used in encodingeach plane

consider-23.3 Graphic Objects and GDI Attributes

We should first mention that Windows graphics objects are not objects in the ject-oriented sense Windows graphics objects are pens, brushes, bitmaps, palettes,fonts, paths, and regions Of these, pens and brushes are the objects most directly re-lated to pixel and line drawing operations

ob-23.3.1 Pens

The pen graphics object determines a line's color, width, and style Windows uses thepen currently selected in the device context with any of the pen-based drawing func-tions Three stock pens are defined: BLACK_PEN, WHITE_PEN, and NULL_PEN Thedefault pen is BLACK_PEN, which draws solid black lines Applications refer to a pen

by means of its handle, which is stored in a variable of type HPEN TheGetStockObject() function is used to obtain a handle to one of the stock pens The penmust be selected into the device context before it is used, as follows:

HPEN aPen; // handle to pen

The two functions can be combined in a single statement, as follows:

SelectObject (hdc, GetStockObject (WHITE_PEN));

In this case, no pen handle variable is required SelectObject() returns the handle

to the pen previously installed in the device context This can be used to save theoriginal pen so that it can be restored later

Drawing applications sometimes require one or more custom pens, which have aparticular style, width, and color Custom pens can be created with the functionsCreatePen(), CreatePenIndirect(), and ExtCreatePen() In the CreatePen() functionthe pen's style, width, and color are passed as parameters CreatePenIndirect() uses

a s t r u c t u r e o f t y p e L O G P E N t o h o l d t h e p e n ' s s t y l e , w i d t h , a n d c o l o r.ExtCreatePen(), introduced in Windows 95, is the more powerful of the three The

Trang 9

iStyle parameter is a combination of pen type, styles, end cap style, and line join tributes The constants used in defining this parameter are listed in Table 23.2.

at-Table 23.2

Values Defined for the ExtCreatePen() iStyle Parameter

PS_GEOMETRIC Pen is geometric

PS_COSMETIC Pen is cosmetic Same as those created with

CreatePen() and CreatePenIndirect() Widthmust be 1 pixel

Pen Style

PS_ALTERNATE Windows NT: Pen sets every other pixel

(cosmetic pens only.)Windows 95: Not supported

PS_DASHDOT Pen has alternating dashes and dots

PS_DASHDOTDOT Pen has alternating dashes and double dots

PS_USERSTYLE Windows NT: Pen uses a styling array supplied by

the user

Windows 95: Not supported

PS_INSIDEFRAME Pen is solid Any drawing function that takes a

bounding rectangle, the dimensions of the figureare shrunk so that it fits entirely in the

bounding rectangle Geometric pens only

End Cap Style (only in stroked paths)

PS_ENDCAP_ROUND End caps are round

PS_ENDCAP_SQUARE End caps are square

PS_ENDCAP_FLAT End caps are flat

Join Style (only in stroked paths)

PS_JOIN_BEVEL Joins are beveled

PS_JOIN_MITER Joins are mitered when they are within the current

limit set by the SetMiterLimit() function If itexceeds this limit, the join is beveled

SetMiterLimit() is discussed in Chapter 21

PS_JOIN_ROUND Joins are round

The standard form of the ExtCreatePen() function is as follows:

HPEN ExtCreatePen (iStyle, // pen style

iWidth, // pen width

&aBrush, // pointer to a LOGBRUSH

// structure (next section) dwStyleCount,// length of next parameter

lpStyle); // dot-dash pattern array

Trang 10

The second parameter to ExtCreatePen() defines the pen's width If the pen is ageometric pen, then its width is specified in logical units If it is a cosmetic penthen the width must be set to 1.

A geometric pen created with ExtCreatePen() has brush-like attributes Thethird parameter is a pointer to LOGBRUSH The LOGBRUSH structure, described

in the following section, is defined as follows:

The fourth parameter, dwStyleCount, determines the length of the fifth ter The fifth parameter, lpStyle, is a pointer to an array of doubleword values Thefirst value in the array is the length of the first dash of a user-defined pen style, thesecond one is the length of the first space, and so on If the pen style does not con-tain the PS_USERSTYLE constant, then the fourth parameter must be zero, and thefifth parameter must be NULL Note that PS_USERSTYLE is supported in Windows

parame-NT but not in Windows 95 or later versions

The end cap styles determine the appearance of the line ends Three constantsare defined for round, square, and flat line ends The end join style determines theappearance of the connecting point of two lines Both styles are available only forgeometric pens Figure 23.3, on the following page, shows the pen styles and theeffects of the different end caps and joins

Note in Figure 23.3 that the difference between square and flat caps is that thesquare style extends the line by one-half its width The white lines in the end cap styleinsert are drawn with the white stock pen, to better show the style's effect TheNULL_PEN style creates a pen that draws with transparent ink, therefore it leaves nomark as it moves on the drawing surface This style is occasionally used in creatingfigures that are filled with a particular brush style but have no border

23.3.2 Brushes

The brush object determines the attributes used in filling a solid figure The outline ofthese figures is determined by the brush selected in the device context A brush has astyle, color, and hatch pattern There are several stock brushes: WHITE_BRUSH,

LT G R AY _ B R U S H , G R AY _ B R U S H , D K G R AY _ B R U S H , B L A C K _ B R U S H , a n dNULL_BRUSH All stock brushes are solid, that is, they fill the entire enclosed area ofthe figure The NULL_BRUSH is used to draw figures without filling the interior If asolid figure is drawn with the NULL_PEN, then it is filled but has no outline

Trang 11

Figure 23.3 Pen Syles, End Caps, and Joins

Applications refer to a brush by its handle, which is stored in a variable of typeHBRUSH The GetStockObject() function is used to obtain a handle to one of thestock brushes The brush must be selected into the device context before use, as fol-lows:

HBRUSH aBrush; // handle to brush

SelectObject (hdc, GetStockObject (WHITE_BRUSH));

In this case, no brush handle variable is required SelectObject() returns the dle to the brush previously installed in the device context This can be used to savethe original brush so that it can later be restored

han-A custom brush is created by means of the CreateBrushIndirect() function Thecall returns a handle to the brush, of type HBRUSH The only parameter is a pointer

to a structure of type LOGBRUSH which holds the brush style, color, and hatch tern The LOGBRUSH structure is also used by the ExtCreatePen() previously de-scribed Table 23.3 lists the predefined constants used for members of theLOGBRUSH structure

pat-PS_SOLID PS_DASH PS_DOT PS_DASHDOT PS_DASHDOTDOT PS_INSIDEFRAME PS_NULL

End Cap Styles:

Line Join Styles:

Trang 12

The foreground mix mode attribute of the device context, also called the drawingmode, determines how Windows combines the pen or brush color with the display sur-face when performing drawing operations The mixing is a raster operation based on aboolean function of two variables: the pen and the background For this reason it is de-scribed as a binary raster operation, or ROP2 All four boolean primitives are used insetting the mix mode: AND, OR, NOT, and XOR The function for setting the foregroundmix mode is SetROP2() GetROP2() returns the current mix mode in the device con-text The general form of the SetROP2() function is as follows:

Table 23.3

Constants in the LOGBRUSH Structure Members

BS_DIBPATTERN A pattern brush defined by a device-independent

bitmap If lbStyle is BS_DIBPATTERN, the lbHatchmember contains a handle to a packed DIB

Note: DIB stands for Device Independent Bitmap.DIBs were discussed previously

BS_DIBPATTERNPT Same as BS_DIBPATTERN but the lbHatch member

contains a pointer to a packed DIB

BS_PATTERN Pattern brush defined by a memory bitmap

DIB_PAL_COLORS The color table consists of an array of 16-bit

indices into the currently realized logicalpalette

DIB_RGB_COLORS The color table contains literal RGB values

HS_BDIAGONAL A 45-degree upward, left-to-right hatch

HS_CROSS Horizontal and vertical cross-hatch

HS_DIAGCROSS 45-degree crosshatch

HS_FDIAGONAL A 45-degree downward, left-to-right hatch

HS_HORIZONTAL Horizontal hatch

HS_VERTICAL Vertical hatch

int SetROP2(

HDC hdc, // 1

int fnDrawMode // 2

);

23.3.3 Foreground Mix Mode

The first parameter is the handle to the device context and the second parameter isone of sixteen mix modes defined by Windows The function returns the previous mixmode, which can be used to restore the original condition Table 23.4 lists the ROP2mix modes The center column shows how the pen (P) and the screen (S) pixels arelogically combined at draw time The boolean operators correspond to the symbolsused in C Figure 23.4, on the following page, shows the brush hatch patterns

Trang 13

Table 23.4

Mix Modes in SetROP2()BOOLEAN

R2_COPYPEN P Pixel is the pen color This is the

default mix mode

R2_MASKNOTPEN ~P&S Pixel is a combination of the colors

common to both the screen and theinverse of the pen

R2_MASKPEN P&S Pixel is a combination of the colors

common to both the pen and thescreen

R2_MASKPENNOT P&~S Pixel is a combination of the colors

common to both the pen and theinverse of the screen

R2_MERGENOTPEN ~P|S Pixel is a combination of the screen

color and the inverse of the pen color.R2_MERGEPEN P|S Pixel is a combination of the pen color

and the screen color

R2_MERGEPENNOT P|~S Pixel is a combination of the pen color

and the inverse of the screen color

R2_NOT ~S Pixel is the inverse of the screen

color

R2_NOTCOPYPEN ~P Pixel is the inverse of the pen color

R2_NOTMASKPEN ~(P&S) Pixel is the inverse of the color

R2_NOTMERGEPEN ~(P|S) Pixel is the inverse of the _MERGEPEN

color

R2_NOTXORPEN ~(P^S) Pixel is the inverse of the R2_XORPEN

color

R2_XORPEN P^S Pixel is a combination of the colors in

the pen and in the screen, but not inBoth

Legend:

~ = boolean NOT | = boolean OR

& = boolean AND ^ = boolean XOR

Figure 23.4 Brush Hatch Patterns

Trang 14

23.3.4 Background Modes

Windows recognizes two background modes that determine how the gaps betweendots and dashes are filled when drawing discontinuous lines, as well as with text andhatched brushes The background modes, named OPAQUE and TRANSPARENT, areset in the device context by means of the SetBkMode() function The function's gen-eral form is as follows:

The background mode affects lines that result from a pen created withCreatePen() or CreatePenIndirect(), but not by those created with ExtCreatePen()

23.3.5 Current Pen Position

Many GDI drawing functions start at a screen location known as the current pen tion, or the current position The pen position is an attribute of the device context Theinitial position of the pen is at logical coordinates (0, 0) Two functions relate directly

posi-to the current pen position: MoveToEx() and GetCurrent Position() Some drawingfunctions change the pen position as they execute The MoveToEx() function is used

to set the current pen position Its general form is as follows:

pa-The GetCurrentPositionEx() function can be used to obtain the current pen tion Its general form is as follows:

Trang 15

The second parameter is a pointer to a structure variable of type POINT that ceives the coordinates of the current pen position The function returns TRUE if itsucceeds and FALSE if it fails.

re-Drawing functions whose names contain the word "To" use and change the rent pen position, these are: LineTo(), PolylineTo(), and PolyBezierTo() Windows

cur-is not always conscur-istent in thcur-is use of the word "To", since the functionsAngleArc() and PolyDraw() also use and update the current pen position

23.3.6 Arc Direction

One start-point and one end-point on the circumference of a circle define two ent arcs: one drawn clockwise and one drawn counterclockwise The exception iswhen the start and end points coincide Figure 23.5 shows this possible ambiguity

differ-Figure 23.5 The Arc Drawing Direction

In Figure 23.5 the solid line arc is drawn counterclockwise from point A topoint B, while the dotted line arc is drawn clockwise between these same points.The SetArcDirection() function is used to resolve this problem The function'sgeneral form is as follows:

int SetArcDirection(

HDC hdc, // 1 int ArcDirection // 2 );

The second parameter is either the constant AD_CLOCKWISE, or the constantAD_COUNTERCLOCKWISE The function returns the previous arc drawing direc-tion

23.4 Pixels, Lines, and Curves

The lowest-level graphics primitives are to set a screen pixel to a particular ute and to read the attributes of a screen pixel In theory, with functions to set andread a pixel, all the other graphics operations can be developed in software For ex-ample, a line can be drawn by setting a series of adjacent pixels, a closed figure can

attrib-A

B

Trang 16

be filled by setting all the pixels within its boundaries, and so on However, in actualprogramming practice these simple primitives are not sufficient In the first place,high-level language code requires considerable overhead in performing the pixel setand read operations To draw lines and figures by successively calling these functionswould be prohibitively time-consuming On the other hand, there are cases in whichthe programmer must resort to pixel-by-pixel drawing since other higher-level func-tions are not available.

There are eleven functions in the Windows API that can be used to draw lines Forone of them, StrokePath(), we postpone the discussion until later, since we mustfirst discuss paths in greater detail Table 23.5 lists the remaining ten line-drawingfunctions

Table 23.5

Line-Drawing Functions

LineTo() A straight line from current position up to

a point Pen position is updated to line's endpoint

PolylineTo() One or more straight lines between the current

position and points in an array Pen position isused for the first line and updated to end point

of last line

Polyline() A series of straight line segments between points

defined in an array

PolyPolyLine() Multiple polylines

ArcTo() An elliptical arc updating current pen position

Arc() An elliptical arc without updating current pen

position

AngleArc() A segment of arc starting at current pen position

PolyBezier() One or more Bezier curves without updating the

current pen position

PolyBezierTo() One or more Bezier curves updating the current

pen position

PolyDraw() A set of lines and Bezier curves

StrokePath() See Chapter 21

23.4.1 Pixel Operations

Two Windows functions operate on single pixels: SetPixel() and GetPixel().SetPixel()

is used to set a pixel at any screen location to a particular color attribute GetPixel()reads the color attribute of a pixel at a given screen location The general form ofSetPixel() is as follows:

Trang 17

pa-parameter contains the pixel color in a COLORREF type structure The functionreturns the RGB color to which the pixel was set, which may not coincide with theone requested in the call because of limitations of the video hardware A fasterversion of this function is SetPixelV() It takes the same parameters but returns aboolean value that is TRUE if the operation succeeded and FALSE if it failed Inmost cases SetPixelV() is preferred over SetPixel() because of its better perfor-mance The following code fragment shows how to draw a box of 100-by-100 pix-els using the SetPixelV() function:

int x, y, i, j; // control variables

23.4.2 Drawing with LineTo()

The simplest of all line-drawing functions is LineTo() The function requires threeparameters: the handle to the device context, and the coordinates of the end points

of the line The line is drawn with the currently selected pen The start point is thecurrent pen position, for this reason LineTo() is often preceded by MoveToEx() oranother drawing function that sets the current pen position LineTo() returns TRUE

if the function succeeds and FALSE if it fails, but most often the return value is notused by code If the LineTo() function succeeds, the current pen position is reset tothe line's end point; therefore, the function can be used to draw a series of con-nected line segments

The following code fragment draws a rectangle using four lines:

HPEN bluePen4; // handle for a pen

int x, y, i, j; // local variables

.

.

.

// Create and select pen

bluePen4 = CreatePen (PS_SOLID, 4, RGB (0x00, 0x00, 0xff);

SelectObject (hdc, bluePen4);

// Set current pen position for start point

MoveToEx (hdc, 140, 140, NULL);

LineTo (hdc, 300, 140); // draw first segment

LineTo (hdc, 300, 200); // second segment

Trang 18

LineTo (hdc, 140, 140); // last segment

23.4.3 Drawing with PolylineTo()

The PolylineTo() function draws one or more straight lines between points contained

in an array of type POINT The current pen position is used as a start point and is reset

to the location of the last point in the array PolylineTo() provides an easier way ofdrawing several connected line segments, or an unfilled closed figure The functionuses the current pen Its general form is as follows:

// Create a solid red pen, 2 pixels wide

redPen2 = CreatePen (PS_SOLID, 2, RGB(0xff, 0x00, 0x00));

23.4.4 Drawing with Polyline()

The Polyline() function is similar to PolylineTo() except that it does not use or changethe current pen position Therefore, you need one more entry in the array of points todraw a figure with Polyline() since the initial position of the drawing pen cannot beused as the starting point for the first line segment The following code fragmentshows drawing a rectangle using the Polyline() function

// Create a solid red pen, 2 pixels wide

blackPen = CreatePen (PS_DASH, 1, 0);

SelectObject (hdc, blackPen);

// Fill array of points

pointsArray[0].x = 160; pointsArray[0].y = 160;

Trang 19

23.4.5 Drawing with PolyPolyline()

As the function name implies, PolyPolyline() is used to draw several groups of lines

or "polylines." Since the points array contains sets of points for more than onepolyline, the function requires an array of values that holds the number of points foreach polyline PolyPolyline(), like Polyline(), does not use or change the currentpen position The function's general form is as follows:

POINT pointsArray[10]; // array of points

DWORD vertexArray[2]; // vertices per polyline

// Fill array of points for first polyline

// Draw two polylines

PolyPolyline (hdc, pointsArray, vertexArray, 2);

Figure 23.6 shows the figures that result from executing the previous code ple The second polyline is shown in dashed lines to visually distinguish it fromthe first one However, in an actual drawing there is no way of changing pens in-side a call to PolyPolyline()

Trang 20

sam-Figure 23.6 Coordinates of Two Polylines in the Sample Code

23.4.6 Drawing with Arc()

The Arc() function draws an elliptical arc It is also used to draw circles, since the cle is a special case of the ellipse The function's general form is as follows:

The coordinates of the start and end points of the elliptical arc need not coincidewith the arc itself Windows draws an imaginary line from the center of the ellipse tothe start and end points The point at which this line (or its prolongation) intersectsthe elliptical arc is used as the start or end point If the start and end points are thesame, then a complete ellipse is drawn The following code fragment draws an ellip-tical arc:

Trang 21

Figure 23.7 Coordinates of an Elliptical Arc in Sample Code

23.4.7 Drawing with ArcTo()

ArcTo() is a version of the Arc() function that updates the current pen position to theend point of the elliptical arc This function requires Windows NT Version 3.1 or later

It is not available in Windows 95 or later The function parameters are identical tothose of the Arc() function

23.4.8 Drawing with AngleArc()

The AngleArc() function draws a straight line segment and an arc of a circle Thestraight line segment is from the current pen position to the arc's starting point Thearc is defined by the circle's radius and two angles: the starting position, in degrees,relative to the x-axis, and the angle sweep, also in degrees, relative to the starting po-sition The arc is drawn in a counterclockwise direction The function's general form

x = 250

y = 260

Trang 22

The second and third parameters are the coordinates of the center of the circle thatdefines the arc, in logical units The fourth parameter is the radius of the circle, also

in logical units The fifth parameter is the start angle in degrees, relative to the x-axis.The last parameter is the sweep angle, also in degrees, relative to the angle's startingposition Figure 23.8 shows the various elements in the AngleArc() function

Figure 23.8 AngleArc() Function Elements

The AngleArc() function is not available in Windows 95 or later; however, it can

be emulated in code Microsoft Developers Network contains the following listingwhich allows implementing the AngleArc() function in software:

BOOL AngleArc2(HDC hdc, int X, int Y, DWORD dwRadius,

float fStartDegrees, float fSweepDegrees) {

int iXStart, iYStart; // End point of starting radial line

int iXEnd, iYEnd; // End point of ending radial line

float fStartRadians; // Start angle in radians

float fEndRadians; // End angle in radians

BOOL bResult; // Function result

float fTwoPi = 2.0f * 3.141592f;

/* Get the starting and ending angle in radians */

if (fSweepDegrees > 0.0f) {

fStartRadians = ((fStartDegrees / 360.0f) * fTwoPi);

fEndRadians = (((fStartDegrees + fSweepDegrees) / 360.0f) *

/* Calculate a point on the starting radial line via */

/* polar -> cartesian conversion */

iXStart = X + (int)((float)dwRadius * (float)cos(fStartRadians));

iYStart = Y - (int)((float)dwRadius * (float)sin(fStartRadians));

/* Calculate a point on the ending radial line via */

/* polar -> cartesian conversion */

iXEnd = X + (int)((float)dwRadius * (float)cos(fEndRadians));

Trang 23

/* Draw a line to the starting point */

LineTo(hdc, iXStart, iYStart);

/* Draw the arc */

bResult = Arc(hdc, X - dwRadius, Y - dwRadius,

X + dwRadius, Y + dwRadius,

iXStart, iYStart,

iXEnd, iYEnd);

// Move to the ending point - Arc() wont do this and ArcTo()

// wont work on Win32s or Win16 */

MoveToEx(hdc, iXEnd, iYEnd, NULL);

return bResult;

}

Notice that the one documented difference between the preceding listing ofAngleArc2(), and the GDI AngleArc() function, is that if the value entered in thesixth parameter exceeds 360 degrees, the software version will not sweep the an-gle multiple times In most cases this is not a problem

The program named PXL_DEMO, in the Pixel and Line Demo project folder onthe book's on-line software package, uses the AngleArc2() function to display acurve similar to the one in Figure 23.8

23.4.9 Drawing with PolyBezier()

In mechanical drafting, a spline is a flexible edge that is used to connect severalpoints on an irregular curve Two French engineers, Pierre Bezier and Paul deCasteljau, almost simultaneously discovered a mathematical expression for aspline curve that can be easily adapted to computer representations This curve isknown as the Bezier spline or curve, since it was Bezier who first published his find-ings The Bezier curve is defined by its end points, called the nodes, and by one ormore control points The control points serve as magnets or attractors that "pull" thecurve in their direction, but never enough for the curve to intersect the controlpoint Figure 23.9 shows the elements of a simple Bezier curve

Figure 23.9 The Bezier Spline

The Bezier curve in Figure 23.9 can be generated by a geometrical method thatconsists of creating a series of progressively smaller line segments The process,

start node

Bezier curve

end node control point

(attractor)

Trang 24

sometimes called the divide and conquer method, starts by joining the half-waypoints between the nodes and the attractor, thus creating a new set of nodes and anew attractor The process continues until a sufficiently accurate approximation ofthe spline is reached Figure 23.10 shows the progressive steps in creating a Bezierspline by this method.

Figure 23.10 Divide-and-Conquer Method of Creating a Bezier Curve

In Step 1of Figure 23.10, we see the start node S1, the end node E1, and the tor A1 We first find a point midway between S1 and A1 and label it P1 Anotherpoint midway between A1 and E1 is labeled P2 Points P1 and P2 are joined by a linesegment, whose midpoint is labeled P3 In Step 2 we can see two new figures Thefirst one has nodes at S2 and E2, and the attractor at A2 The second figure hasnodes at S3 and E3, and the attractor at A3 In Step 3 we have joined the midpointsbetween the nodes and the attractors with a line segment, thus continuing the pro-cess The two new figures have their new respective nodes and attractors, so theprocess can be again repeated In Step 3 we can see how the resulting line segmentsbegin to approximate the Bezier curve in Figure 23.9

attrac-The divide and conquer process makes evident the fundamental assumption ofthe Bezier spline: the curve is in the same direction and tangent to straight linesfrom the nodes to the attractors A second assumption is that the curve never inter-sects the attractors The Bezier formulas are based on these assumptions

The Bezier curve generated by the divide and conquer method is known as a dratic Bezier In computer graphics the most useful Bezier is the cubic form In thecubic form the Bezier curve is defined by two nodes and two attractors The devel-opment of the cubic Bezier is almost identical to that of the quadratic Figure 23.11,

qua-on the following page, shows the elements of a cubic Bezier curve

Trang 25

Figure 23.11 Elements of the Cubic Bezier

The PolyBezier() function, introduced in Windows 95, draws one or more cubicBezier curves, each one defined by its nodes and two attractors The function can

be called to draw multiple Bezier curves In this case the first curve requires fourparameters, and all the other curves require three parameters This is because theend node of the preceding Bezier curve serves as the start node for the next one.PolyBezier() does not change the current pen position The Bezier curve is drawnusing the pen selected in the device context The function's general form is as fol-lows:

The first parameter is the handle to the device context The second parameter

is the address of an array of points that contains the x- and y-coordinate pairs forthe nodes and control points The third parameter is the count of the number ofpoints in the array This value must be one more than three times the number ofcurves to be drawn For example, if the PolyBezier() function is called to drawfour curves, there must be 13 coordinate pairs in the array (1 + (3 * 4)) The func-tion returns TRUE if it succeeds and FALSE otherwise

The Bezier data is stored in the array of points in a specific order In the firstBezier curve, the first and fourth entries are the nodes and the second and thirdare attractors Note that in the array the first and fourth entries are at offset 0 and

3 respectively, and the second and third entries are at offset 1 and 2 If there areother Bezier curves in the array, the first node is not explicit in the data, since itcoincides with the end node of the preceding curve Therefore, after the firstcurve, the following two entries are attractors, and the third entry is the end node.Table 23.6 shows the sequence of nodes and control points for an array with multi-ple Bezier curves

start node

first control point

second control point end node Bezier spline

Trang 26

Table 23.6

Nodes and Control Points for the PolyBezier() Function

The following code fragment shows the drawing of a Bezier curve using thePolyBezier() function:

POINTS pointsArray[4]; // Array of x/y coordinates

// Fill array of points for Bezier spline

// Entries 0 and 3 are nodes

// Entries 1 and 2 are attractors

The resulting Bezier curve is similar to the one in Figure 23.9

23.4.10 Drawing with PolyBezierTo()

The PolyBezierTo() function is very similar to PolyBezier() except that the start nodefor the first curve is the current pen position, and the current pen position is updated

to the end node of the last curve The return value and parameters are the same forboth functions In the case of PolyBezierTo() each curve is defined by three points:two control points and the end node Table 23.7, on the following page, shows the se-quence of points stored in the points array for PolyBezierTo()

23.4.11 Drawing with PolyDraw()

PolyDraw() is the most complex of the Windows line-drawing functions It creates thepossibility of drawing a series of line segments and Bezier curves, which can be joint

or disjoint PolyDraw() can be used in place of several calls to MoveTo(), LineTo(), andPolyBezierTo() functions All the figures are drawn with the pen currently selected inthe device context The function's general form is as follows:

Trang 27

Table 23.7

Nodes and Control Points for the PolyBezierTo() Function

The second parameter is the address of an array of points that contains x- andy-coordinate pairs The third parameter is an array of type BYTE that containsidentifiers that define the purpose of each of the points in the array The fourth pa-rameter is the count of the number of points in the array of points The function re-turns TRUE if it succeeds and FALSE otherwise Table 23.8 lists the constants used

to represent the identifiers entered in the function's third parameter

Table 23.8

Constants for PolyDraw() Point Specifiers

PT_MOVETO This point starts a disjoint figure The point

becomes the new current pen position

PT_LINETO A line is to be drawn from the current position to

this point, which then becomes the new currentpen position

PT_BEZIERTO This is a control point or end node for a Bezier

curve This constant always occurs in sets of three.The current position defines the start node for theBezier curve The other two coordinates are controlpoints The third entry is the end node

PT_CLOSEFIGURE The figure is automatically closed after the

PT_LINETO or PT_BEZIERTO type for this point isexecuted A line is drawn from the end point to themost recent PT_MOVETO or MoveTo() point

The PT_CLOSEFIGURE constant is combined bymeans of a bitwise OR operator with a PT_LINETO

or PT_BEZIERTO constant This indicates that thecorresponding point is the last one in a figure andthat the figure is to be closed

The PolyDraw() function is not available in Windows 95 or later Microsoft haspublished the following code for implementing the function in software:

Trang 28

CONST LPBYTE lpbTypes, // line and curve identifiers int cCount) // count of points

//*****************************

// Win95 version of PolyDraw()

// improved!

//*****************************

BOOL PolyDraw95A (HDC hdc, // handle to device context

CONST LPPOINT lppt, // array of points

CONST LPBYTE lpbTypes, // array of identifiers

int cCount) // count of points

MoveToEx(hdc, lppt[i].x, lppt[i].y, NULL);

// Store position for closed figures

lastPenx = lppt[i].x;

lastPeny = lppt[i].y;

break;

Trang 29

LineTo(hdc, lppt[i].x, lppt[i].y);

LineTo(hdc, lastPenx, lastPeny);

break;

case PT_LINETO :

LineTo(hdc, lppt[i].x, lppt[i].y);

break;

case PT_BEZIERTO | PT_CLOSEFIGURE:

// Store start points of Bezier for closing

// Close with line

LineTo(hdc, lastPenx, lastPeny);

Trang 30

// Drawing lines and Bezier curves

PolyDraw95A (hdc, pointsArray, controlArray, 17);

Figure 23.12 is an approximation of the figures that result from the previous codesample

Figure 23.12 Approximate Result of the PolyDraw() Code Sample

23.4.12 Pixel and Line Demonstration Program

The program named PXL_DEMO, located in the Pixel and Line Demo project folder ofthe book's software on-line, is a demonstration of the drawing functions discussed in

Trang 31

this chapter Pixel-level functions are used to display the point plot of a sine curve.Also, the program contains a function named DrawDot(), which uses theSetPixelV() function to draw a black screen dot by setting five adjacent pixels Thedemo program displays a pop-up menu, named Line Functions, which has menucommands for exercising LineTo(), PolyLineTo(), PolyLine(), PolyPolyLine(),Arc(), AngleArc(), PolyBezier(), and PolyDraw() Code for implementingPolyDraw() and AngleArc() in software is also included in the demo program.

Trang 32

Drawing Solid Figures

Chapter Summary

In this chapter we continue exploring the graphics functions in the Windows GDI, centrating on geometrical elements that contain an interior region, in addition to aperimeter or outline These are called solid or closed figures The interior area allowsthem to be filled with a given color, hatch pattern, or bitmap image At the same time,the perimeter of a closed figure can be rendered differently than the filled area For ex-ample, the circumference of a circle can be outlined with a 2-pixel wide black pen, andthe circle's interior filled with 1-pixel wide red lines, slanted at 45 degrees, and sepa-rated from each other by 10 pixels

con-24.0 Closed Figures

Closed figures allow several graphics manipulations For instance, a solid figure can

be used to define the output area to which Windows can perform drawing operations.This area, called the clipping region, allows you to produce unique and interestinggraphics effects, such as filling geometrical figures with text or pictures

Some closed figures are geometrically simple: a rectangle, an ellipse, or a metrical polygon More complex figures are created by combining simpler ones Aregion is an area composed of one or more rectangles, polygons, or ellipses Regionsare used to define irregular areas that can be filled, to clip output, or to establish anarea for mouse input

sym-Paths are relatively new graphics objects, since they were introduced with dows NT, and are also supported in Windows 95 and later A path is the route thedrawing instrument follows in creating a figure, or set of figures It is used to definethe outline of a graphics object After a path is created, you can draw its outline(called stroking the path), fill its interior, or both A path can also be used in clip-ping, or converted into a region Paths and regions add a powerful dimension to Win-dows graphics

Win-635

Trang 33

24.1 Closed Figure Elements

A closed figure has both a perimeter and an interior The perimeter of a closed figure

is drawn using the current pen and the GDI line-related attributes discussed inChapter 20 The interior is filled using the current brush, also partly discussed inChapter 20 There are several closed figures that can be drawn with the WindowsGDI; among them are ellipses, polygons, chords, pies, and rectangles Later in thischapter we see that the Windows names for some of these figures are not geometri-cally correct Areas bound by complex lines, such as irregular polygons, Beziercurves, and text characters, can also be filled

Like lines and curves, closed figures have attributes that determine their acteristics Most of the attributes that relate to closed figures are described inChapter 23 These include the mix mode, the background mode, the arc direction,the brush pattern, the pen styles, as well as the brush, pen, and background col-ors Two attributes that are specific to closed figures are the brush origin and thepolygon filling mode

char-24.1.1 Brush Origin

Figure 23.4, in the preceding chapter, shows the various hatch patterns that can beused with a brush Windows locates the hatch pattern in reference to coordinates(0,0) It is important to know that this origin is in device units, not in logical units.The hatch pattern is a bitmap In Windows 95 and later, the bitmap is 8-by-8 pixels InWindows NT, it can have any size The painting process consists of repeating thebitmap horizontally and vertically until the area is filled

In some cases the default origin of the bitmap produces undesirable results.This usually happens when the alignment of a filled figure does not coincide withthat of the brush hatch pattern Figure 24.1 shows two rectangles, one filled with

an unaligned hatch pattern and the other one filled with an aligned hatch pattern.The SetBrushOrgEx() function can be used to reposition the hatch bitmap inrelation to the origin of the client area The function's general form is as follows:

xOrg = xOrg % bitmap width

The third parameter is the y-coordinate of the new brush origin Its range andadjustments are the same as for the second parameter The fourth parameter is apointer to a POINT structure that stores the origin of the brush previously se-

Trang 34

lected in the device context If this information is not required, NULL can be entered

in this parameter The function returns TRUE if the operation succeeds and FALSEotherwise

Figure 24.1 Brush Hatch Patterns

A call to SetBrushOrgEx() sets the origin of the next brush that an application lects into the device context Note that the first parameter of the SetBrushOrgEx()function is the handle to the device context, and that the brush variable is nowhere

se-to be found in the parameter list Therefore, the brush origin is associated with thedevice context, not with a particular brush The origin in the device context is as-signed to the next brush created

The following code fragment shows the display of two rectangles The brush gin is changed for the second one The Rectangle() function is described later in thischapter

ori-static HBRUSH vertBrush1, vertBrush2;

vertBrush1 = CreateBrushIndirect (&brush1);

SelectObject (hdc, (HGDIOBJ)(HBRUSH) vertBrush1);

// Draw a rectangle with this brush

vertBrush2 = CreateBrushIndirect (&brush1);

SelectObject (hdc, (HGDIOBJ)(HBRUSH) vertBrush2);

// Draw a rectangle with the new brush

Rectangle (hdc, 350, 150, 502, 300);

unaligned hatch pattern aligned hatch pattern

Trang 35

The results of executing this code are similar to the rectangles in Figure 24.1.The GetBrushOrg() function can be used to retrieve the origin of the currentbrush.

Notice that Windows documentation recommends that to avoid brush ment an application should call SetStretchBltMode() with the stretching mode set

misalign-to HALFTONE before calling SetBrushOrgEx()

24.1.2 Object Selection Macros

The Windows header file windowsx.h contains four macros that can be used in lecting a pen, brush, font, or bitmap The advantage of using these macros is that theobjects are automatically typecast correctly The macros are named SelectPen(),SelectBrush(), SelectFont() and SelectBitmap() They are all defined similarly TheSelectBrush() macro is as follows:

se-#define SelectBrush (hdc, hbr) \

((HBRUSH) SelectObject ((hdc, (HGDIOBJ)(HBRUSH)(hbr)))

You can use these macros to easily produce code that is correct and more table Programs that use the object selection macros must contain the statement:

por-#include <windowsx.h>

24.1.3 Polygon Fill Mode

The polygon fill mode attribute determines how overlapping areas of complex gons and regions are filled The polygon fill mode is set with the SetPolyFillMode()function The function's general form is as follows:

of the polygon by crossing an odd number of lines, excluding the vertices This fillalgorithm is based on what is called the parity rule

The WINDING mode is based on the nonzero winding rule In the WINDINGmode, the direction in which the figure is drawn determines whether an area is to

be filled A polygon line segment is drawn either in a clockwise or a wise direction The term winding relates to the clockwise and counterclockwisedrawing of the polygon segments An imaginary line, called a ray, is extendedfrom an enclosed area in the figure, to a point distant from the figure and outside

counterof it The ray must be on a positive x-direction Every time the ray crosses a wise winding, a counter is incremented The same counter is decremented when-ever the line crosses a counterclockwise winding The winding counter isexamined when the ray reaches the outside of the figure If the winding counter isnonzero, the area is filled

Trang 36

clock-In figures that have a single interior region the fill mode is not important This isnot the case in figures that have enclosed areas A typical case is a polygon in theshape of a five-pointed star with an enclosed pentagon In this case, theALTERNATE mode does not fill the interior pentagon, while the WINDING fill modedoes In more complex figures the same rules apply, although they may not be imme-diately evident Figure 24.2 shows the results of the polygon fill modes in two differ-ent figures Recall that in the WINDING fill mode, the direction in which the linesegments are drawn is significant.

Figure 24.2 Effects of the Polygon Fill Modes

In Figure 24.2 you can see the application of the nonzero winding rule to the rior areas of a complex polygon For example, the ray from point P1 to the exterior

inte-of the figure crosses two clockwise segments (windings) Therefore, it has a

wind-ALTERNATE poligon fill mode WINDING poligon fill mode

Application of nonzero winding rule

to polygon's interior areas

Trang 37

ing value of 2 Since the winding is nonzero, the area is filled The same rule can

be applied to other points in the figure's interior, as shown in Figure 24.2

Notice that some Windows documentation states that in the WINDING mode allinterior areas of a figure are filled This oversimplification is not correct If the in-terior segments of the polygon in Figure 24.2 were drawn in the opposite direc-tion, some areas would have zero winding and would not be filled The programnamed FIL_DEMO, located in the Filled Figure Demo project folder, in the book'son-line software package, contains the menu command Polygon (2), on the DrawFigures pop-up menu, which displays a complex polygon that has an unfilled inte-rior in the WINDING mode

You can retrieve the current polygon fill mode with the GetPolyFillMode()function The only parameter to the call is the handle to the device context Thevalue returned is either ALTERNATE or WINDING

24.1.4 Creating Custom Brushes

In Chapter 23 we mentioned that a logical brush can be created with theCreateBrushIndirect() function CreateBrushIndirect() has the following generalform:

Once the brush is created, it can be selected into the device context by eithercalling the SelectObject() function or the SelectBrush() macro discussed previ-ously in this chapter In addition, you can create specific types of brushes easier

b y u s i n g t h e f u n c t i o n s C r e a t e S o l i d B r u s h ( ) , C r e a t e H a t c h B r u s h ( ) , o rCreatePatternBrush() CreateSolidBrush() is used to create a brush of a specificcolor and no hatch pattern The function's general form is as follows:

HBRUSH CreateSolidBrush (COLORREF colorref);

The only parameter is a color value in the form of a COLORREF type structure.The color value can be entered directly using the RGB macro For example, thefollowing code fragment creates a solid blue brush:

static HBRUSH solidBlueBrush;

.

.

.

Trang 38

Table 24.1

LOGBRUSH Structure Members

BS_DIBPATTERN A pattern brush defined by a device-independent

bitmap If lbStyle is BS_DIBPATTERN, the lbHatchmember contains a handle to a packed DIB

BS_DIBPATTERNPT Same a BS_DIBPATTERN but the lbHatch member

contains a pointer to a packed DIB

BS_PATTERN Pattern brush defined by a memory bitmap

DIB_PAL_COLORS The color table consists of an array of 16-bit

indices into the currently realized logical palette.DIB_RGB_COLORS The color table contains literal RGB values

HS_BDIAGONAL A 45-degree upward, left-to-right hatch

HS_CROSS Horizontal and vertical cross-hatch

HS_DIAGCROSS 45-degree crosshatch

HS_FDIAGONAL A 45-degree downward, left-to-right hatch

HS_HORIZONTAL Horizontal hatch

HS_VERTICAL Vertical hatch

CreatehatchBrush() creates a logical brush with a hatch pattern and color Thefunction's general form is as follows:

If an application requires a brush with a hatch pattern different from the ones defined in Windows, it can create a custom brush with its own bitmap In Windows

pre-95 and later, the size of the bitmap cannot exceed 8-by-8 pixels, but there is no sizerestriction in Windows NT The function's general form is as follows:

Trang 39

24.2 Drawing Closed Figures

There are seven Windows functions that draw closed figures, shown in Table 24.2

Table 24.2

Windows Functions for Drawing Closed Figures

Rectangle() Rectangle with sharp corners

RoundRect() Rectangle with rounded corners

Chord() Solid figure created by an arc on the

circumference of an ellipse connected by

a chordPie() Pie-shaped wedge created by joining the

end points of an arc on the perimeter of anellipse with the center of the arc

PolyPolygon() Series of closed polygons, possibly overlappingAll functions that draw closed figures use the pen currently selected in the de-vice context for the figure outline, and the current brush for filling the interior All

of the line attributes discussed in Chapter 20 apply to the perimeter of solid ures The programmer has control of the width of the perimeter, its line style, andits color By selecting NULL_PEN you can draw a figure with no perimeter The fill

fig-is determined by the current brush Windows approximates the color of the brushaccording to the device capabilities

This often requires manipulating dot sizes by a process called dithering.Dithering is a technique that creates the illusion of colors or shades of gray bytreating the targeted areas as a dot pattern The process takes advantage of thefact that the human eye tends to blur small spots of different color by averagingthem into a single color or shade For example, a pink color effect can be pro-duced by mixing red and white dots

T h e b r u s h c a n b e a n y o n e o f t h e s t o c k b r u s h e s : W H I T E _ B R U S H ,LTGRAY_BRUSH, GRAY_BRUSH, DKGRAY_BRUSH, BLACK_BRUSH, andNULL_BRUSH All stock brushes are solid NULL_BRUSH is used to draw figureswithout filling the interior The GetStockObject() function is used to obtain a han-dle to one of the stock brushes Since stock brushes need not be stored locally,the most common case is that the stock brush is retrieved and installed in the de-vice context at the time it is needed SelectBrush() and GetStockObject() can becombined as follows:

SelectBrush (hdc, GetStockObject (WHITE_BRUSH));

The creation and installation of custom brushes was discussed previously inthis chapter

Trang 40

24.2.1 Drawing with Rectangle()

The simplest solid-figure drawing function is Rectangle() This function draws a angle using the current pen for the outline and fills it with the current brush The func-tion's general form is as follows:

Figure 24.3 Figure Definition in the Rectangle() Function

24.2.2 Drawing with RoundRect()

The RoundRect() function draws a rectangle with rounded corners Like all the solidfigure drawing functions, it uses the current pen for the outline and fills the figure withthe current brush The function's parameter list is as follows:

Ngày đăng: 12/08/2014, 07:22

TỪ KHÓA LIÊN QUAN