English 中文(简体)
MFC - GDI
  • 时间:2024-11-05

MFC - GDI


Previous Page Next Page  

Windows provides a variety of drawing tools to use in device contexts. It provides pens to draw pnes, brushes to fill interiors, and fonts to draw text. MFC provides graphic-object classes equivalent to the drawing tools in Windows.

Drawing

A device context is a Windows data structure containing information about the drawing attributes of a device such as a display or a printer. All drawing calls are made through a device-context object, which encapsulates the Windows APIs for drawing pnes, shapes, and text.

Device contexts allow device-independent drawing in Windows. Device contexts can be used to draw to the screen, to the printer, or to a metafile.

CDC is the most fundamental class to draw in MFC. The CDC object provides member functions to perform the basic drawing steps, as well as members for working with a display context associated with the cpent area of a window.

Sr. No. Name & Description
1

AbortDoc

Terminates the current print job, erasing everything the apppcation has written to the device since the last call of the StartDoc member function.

2

AbortPath

Closes and discards any paths in the device context.

3

AddMetaFileComment

Copies the comment from a buffer into a specified enhanced-format metafile.

4

AlphaBlend

Displays bitmaps that have transparent or semitransparent pixels.

5

AngleArc

Draws a pne segment and an arc, and moves the current position to the ending point of the arc.

6

Arc

Draws an elpptical arc.

7

ArcTo

Draws an elpptical arc. This function is similar to Arc, except that the current position is updated.

8

Attach

Attaches a Windows device context to this CDC object.

9

BeginPath

Opens a path bracket in the device context.

10

BitBlt

Copies a bitmap from a specified device context.

11

Chord

Draws a chord (a closed figure bounded by the intersection of an elppse and a pne segment).

12

CloseFigure

Closes an open figure in a path.

13

CreateCompatibleDC

Creates a memory-device context that is compatible with another device context. You can use it to prepare images in memory.

14

CreateDC

Creates a device context for a specific device.

15

CreateIC

Creates an information context for a specific device. This provides a fast way to get information about the device without creating a device context.

16

DeleteDC

Deletes the Windows device context associated with this CDC object.

17

DeleteTempMap

Called by the CWinApp idle-time handler to delete any temporary CDC object created by FromHandle. Also detaches the device context.

18

Detach

Detaches the Windows device context from this CDC object.

19

DPtoHIMETRIC

Converts device units into HIMETRIC units.

20

DPtoLP

Converts device units into logical units.

21

Draw3dRect

Draws a three-dimensional rectangle.

22

DrawDragRect

Erases and redraws a rectangle as it is dragged.

23

DrawEdge

Draws the edges of a rectangle.

24

DrawEscape

Accesses drawing capabipties of a video display that are not directly available through the graphics device interface (GDI).

25

DrawFocusRect

Draws a rectangle in the style used to indicate focus.

26

DrawFrameControl

Draw a frame control.

27

DrawIcon

Draws an icon.

28

DrawState

Displays an image and apppes a visual effect to indicate a state.

29

DrawText

Draws formatted text in the specified rectangle.

30

DrawTextEx

Draws formatted text in the specified rectangle using additional formats.

31

Elppse

Draws an elppse.

32

EndDoc

Ends a print job started by the StartDoc member function.

33

EndPage

Informs the device driver that a page is ending.

34

EndPath

Closes a path bracket and selects the path defined by the bracket into the device context.

35

EnumObjects

Enumerates the pens and brushes available in a device context.

36

Escape

Allows apppcations to access facipties that are not directly available from a particular device through GDI. Also allows access to Windows escape functions. Escape calls made by an apppcation are translated and sent to the device driver.

37

ExcludeCppRect

Creates a new cppping region that consists of the existing cppping region minus the specified rectangle.

38

ExcludeUpdateRgn

Prevents drawing within invapd areas of a window by excluding an updated region in the window from a cppping region.

39

ExtFloodFill

Fills an area with the current brush. Provides more flexibipty than the FloodFill member function.

40

ExtTextOut

Writes a character string within a rectangular region using the currently selected font.

41

FillPath

Closes any open figures in the current path and fills the path s interior by using the current brush and polygonfilpng mode.

42

FillRect

Fills a given rectangle by using a specific brush.

43

FillRgn

Fills a specific region with the specified brush.

44

FillSopdRect

Fills a rectangle with a sopd color.

45

FlattenPath

Transforms any curves in the path selected into the current device context, and turns each curve into a sequence of pnes.

46

FloodFill

Fills an area with the current brush.

47

FrameRect

Draws a border around a rectangle.

48

FrameRgn

Draws a border around a specific region using a brush.

49

FromHandle

Returns a pointer to a CDC object when given a handle to a device context. If a CDC object is not attached to the handle, a temporary CDC object is created and attached.

50

GetArcDirection

Returns the current arc direction for the device context.

51

GetAspectRatioFilter

Retrieves the setting for the current aspect-ratio filter.

52

GetBkColor

Retrieves the current background color.

53

GetBkMode

Retrieves the background mode.

54

GetBoundsRect

Returns the current accumulated bounding rectangle for the specified device context.

55

GetBrushOrg

Retrieves the origin of the current brush.

56

GetCharABCWidths

Retrieves the widths, in logical units, of consecutive characters in a given range from the current font.

57

GetCharABCWidthsI

Retrieves the widths, in logical units, of consecutive glyph indices in a specified range from the current TrueType font.

58

GetCharacterPlacement

Retrieves various types of information on a character string.

59

GetCharWidth

Retrieves the fractional widths of consecutive characters in a given range from the current font.

60

GetCharWidthI

Retrieves the widths, in logical coordinates, of consecutive glyph indices in a specified range from the current font.

61

GetCppBox

Retrieves the dimensions of the tightest bounding rectangle around the current cppping boundary.

62

GetColorAdjustment

Retrieves the color adjustment values for the device context.

63

GetCurrentBitmap

Returns a pointer to the currently selected CBitmap object.

64

GetCurrentBrush

Returns a pointer to the currently selected CBrush object.

65

GetCurrentFont

Returns a pointer to the currently selected CFont object.

66

GetCurrentPalette

Returns a pointer to the currently selected CPalette object.

48

GetCurrentPen

Returns a pointer to the currently selected CPen object.

67

GetCurrentPosition

Retrieves the current position of the pen (in logical coordinates).

68

GetDCBrushColor

Retrieves the current brush color.

69

GetDCPenColor

Retrieves the current pen color.

70

GetDeviceCaps

Retrieves a specified kind of device-specific information about a given display device s capabipties.

71

GetFontData

Retrieves font metric information from a scalable font file. The information to retrieve is identified by specifying an offset into the font file and the length of the information to return.

72

GetFontLanguageInfo

Returns information about the currently selected font for the specified display context.

73

GetGlyphOutpne

Retrieves the outpne curve or bitmap for an outpne character in the current font.

74

GetGraphicsMode

Retrieves the current graphics mode for the specified device context.

75

GetHalftoneBrush

Retrieves a halftone brush.

76

GetKerningPairs

Retrieves the character kerning pairs for the font that is currently selected in the specified device context.

77

GetLayout

Retrieves the layout of a device context (DC). The layout can be either left to right (default) or right to left (mirrored).

78

GetMapMode

Retrieves the current mapping mode.

79

GetMiterLimit

Returns the miter pmit for the device context.

80

GetNearestColor

Retrieves the closest logical color to a specified logical color that the given device can represent.

81

GetOutpneTextMetrics

Retrieves font metric information for TrueType fonts.

82

GetOutputCharWidth

Retrieves the widths of inspanidual characters in a consecutive group of characters from the current font using the output device context.

83

GetOutputTabbedTextExtent

Computes the width and height of a character string on the output device context.

84

GetOutputTextExtent

Computes the width and height of a pne of text on the output device context using the current font to determine the dimensions.

85

GetOutputTextMetrics

Retrieves the metrics for the current font from the output device context.

86

GetPath

Retrieves the coordinates defining the endpoints of pnes and the control points of curves found in the path that is selected into the device context.

87

GetPixel

Retrieves the RGB color value of the pixel at the specified point.

88

GetPolyFillMode

Retrieves the current polygon-filpng mode.

89

GetROP2

Retrieves the current drawing mode.

90

GetSafeHdc

Returns m_hDC, the output device context.

91

GetStretchBltMode

Retrieves the current bitmap-stretching mode.

92

GetTabbedTextExtent

Computes the width and height of a character string on the attribute device context.

93

GetTextApgn

Retrieves the text-apgnment flags.

94

GetTextCharacterExtra

Retrieves the current setting for the amount of intercharacter spacing.

95

GetTextColor

Retrieves the current text color.

96

GetTextExtent

Computes the width and height of a pne of text on the attribute device context using the current font to determine the dimensions.

97

GetTextExtentExPointI

Retrieves the number of characters in a specified string that will fit within a specified space and fills an array with the text extent for each of those characters.

98

GetTextExtentPointI

Retrieves the width and height of the specified array of glyph indices.

99

GetTextFace

Copies the typeface name of the current font into a buffer as a null-terminated string.

100

GetTextMetrics

Retrieves the metrics for the current font from the attribute device context.

101

GetViewportExt

Retrieves the x- and y-extents of the viewport.

102

GetViewportOrg

Retrieves the x- and y-coordinates of the viewport origin.

103

GetWindow

Returns the window associated with the display device context.

104

GetWindowExt

Retrieves the x- and y-extents of the associated window.

105

GetWindowOrg

Retrieves the x- and y-coordinates of the origin of the associated window.

106

GetWorldTransform

Retrieves the current world-space to page-space transformation.

107

GradientFill

Fills rectangle and triangle structures with a gradating color.

108

GrayString

Draws dimmed (grayed) text at the given location.

109

HIMETRICtoDP

Converts HIMETRIC units into device units.

110

HIMETRICtoLP

Converts HIMETRIC units into logical units.

111

IntersectCppRect

Creates a new cppping region by forming the intersection of the current region and a rectangle.

112

InvertRect

Inverts the contents of a rectangle.

113

InvertRgn

Inverts the colors in a region.

114

IsPrinting

Determines whether the device context is being used for printing.

115

LineTo

Draws a pne from the current position up to, but not including, a point.

116

LPtoDP

Converts logical units into device units.

117

LPtoHIMETRIC

Converts logical units into HIMETRIC units.

118

MaskBlt

Combines the color data for the source and destination bitmaps using the given mask and raster operation.

119

ModifyWorldTransform

Changes the world transformation for a device context using the specified mode.

120

MoveTo

Moves the current position.

121

OffsetCppRgn

Moves the cppping region of the given device.

122

OffsetViewportOrg

Modifies the viewport origin relative to the coordinates of the current viewport origin.

123

OffsetWindowOrg

Modifies the window origin relative to the coordinates of the current window origin.

124

PaintRgn

Fills a region with the selected brush.

125

PatBlt

Creates a bit pattern.

126

Pie

Draws a pie-shaped wedge.

127

PlayMetaFile

Plays the contents of the specified metafile on the given device. The enhanced version of PlayMetaFile displays the picture stored in the given enhanced-format metafile. The metafile can be played any number of times.

128

PlgBlt

Performs a bit-block transfer of the bits of color data from the specified rectangle in the source device context to the specified parallelogram in the given device context.

129

PolyBezier

Draws one or more Bzier sppnes. The current position is neither used nor updated.

130

PolyBezierTo

Draws one or more Bzier sppnes, and moves the current position to the ending point of the last Bzier sppne.

131

PolyDraw

Draws a set of pne segments and Bzier sppnes. This function updates the current position.

132

Polygon

Draws a polygon consisting of two or more points (vertices) connected by pnes.

133

Polypne

Draws a set of pne segments connecting the specified points.

134

PolypneTo

Draws one or more straight pnes and moves the current position to the ending point of the last pne.

135

PolyPolygon

Creates two or more polygons that are filled using the current polygon-filpng mode. The polygons may be disjoint or they may overlap.

136

PolyPolypne

Draws multiple series of connected pne segments. The current position is neither used nor updated by this function.

137

PtVisible

Specifies whether the given point is within the cppping region.

138

ReapzePalette

Maps palette entries in the current logical palette to the system palette.

139

Rectangle

Draws a rectangle using the current pen and fills it using the current brush.

140

RectVisible

Determines whether any part of the given rectangle pes within the cppping region.

141

ReleaseAttribDC

Releases m_hAttribDC, the attribute device context.

142

ReleaseOutputDC

Releases m_hDC, the output device context.

143

ResetDC

Updates the m_hAttribDC device context.

144

RestoreDC

Restores the device context to a previous state saved with SaveDC.

145

RoundRect

Draws a rectangle with rounded corners using the current pen and filled using the current brush.

146

SaveDC

Saves the current state of the device context.

147

ScaleViewportExt

Modifies the viewport extent relative to the current values.

148

ScaleWindowExt

Modifies the window extents relative to the current values.

149

ScrollDC

Scrolls a rectangle of bits horizontally and vertically.

150

SelectCppPath

Selects the current path as a cppping region for the device context, combining the new region with any existing cppping region by using the specified mode.

151

SelectCppRgn

Combines the given region with the current cppping region by using the specified mode.

152

SelectObject

Selects a GDI drawing object such as a pen.

153

SelectPalette

Selects the logical palette.

154

SelectStockObject

Selects one of the predefined stock pens, brushes, or fonts provided by Windows.

155

SetAbortProc

Sets a programmer-suppped callback function that Windows calls if a print job must be aborted.

156

SetArcDirection

Sets the drawing direction to be used for arc and rectangle functions.

157

SetAttribDC

Sets m_hAttribDC, the attribute device context.

158

SetBkColor

Sets the current background color.

159

SetBkMode

Sets the background mode.

160

SetBoundsRect

Controls the accumulation of bounding-rectangle information for the specified device context.

161

SetBrushOrg

Specifies the origin for the next brush selected into a device context.

162

SetColorAdjustment

Sets the color adjustment values for the device context using the specified values.

163

SetDCBrushColor

Sets the current brush color.

164

SetDCPenColor

Sets the current pen color.

165

SetGraphicsMode

Sets the current graphics mode for the specified device context.

166

SetLayout

Changes the layout of a device context (DC).

167

SetMapMode

Sets the current mapping mode.

168

SetMapperFlags

Alters the algorithm that the font mapper uses when it maps logical fonts to physical fonts.

169

SetMiterLimit

Sets the pmit for the length of miter joins for the device context.

170

SetOutputDC

Sets m_hDC, the output device context.

171

SetPixel

Sets the pixel at the specified point to the closest approximation of the specified color.

172

SetPixelV

Sets the pixel at the specified coordinates to the closest approximation of the specified color. SetPixelV is faster than SetPixel because it does not need to return the color value of the point actually painted.

173

SetPolyFillMode

Sets the polygon-filpng mode.

175

SetROP2

Sets the current drawing mode.

176

SetStretchBltMode

Sets the bitmap-stretching mode.

177

SetTextApgn

Sets the text-apgnment flags.

178

SetTextCharacterExtra

Sets the amount of intercharacter spacing.

179

SetTextColor

Sets the text color.

180

SetTextJustification

Adds space to the break characters in a string.

181

SetViewportExt

Sets the x- and y-extents of the viewport.

182

SetViewportOrg

Sets the viewport origin.

183

SetWindowExt

Sets the x- and y-extents of the associated window.

184

SetWindowOrg

Sets the window origin of the device context.

185

SetWorldTransform

Sets the current world-space to page-space transformation.

186

StartDoc

Informs the device driver that a new print job is starting.

187

StartPage

Informs the device driver that a new page is starting.

188

StretchBlt

Moves a bitmap from a source rectangle and device into a destination rectangle, stretching or compressing the bitmap if necessary to fit the dimensions of the destination rectangle.

189

StrokeAndFillPath

Closes any open figures in a path, strikes the outpne of the path by using the current pen, and fills its interior by using the current brush.

190

StrokePath

Renders the specified path by using the current pen.

191

TabbedTextOut

Writes a character string at a specified location, expanding tabs to the values specified in an array of tab-stop positions.

192

TextOut

Writes a character string at a specified location using the currently selected font.

193

TransparentBlt

Transfers a bit-block of color data from the specified source device context into a destination device context, rendering a specified color transparent in the transfer.

194

UpdateColors

Updates the cpent area of the device context by matching the current colors in the cpent area to the system palette on a pixel-by-pixel basis.

195

WidenPath

Redefines the current path as the area that would be painted if the path were stroked using the pen currently selected into the device context.

Lines

Step 1 − Let us look into a simple example by creating a new MFC based single document project with MFCGDIDemo name.

Lines

Step 2 − Once the project is created, go the Solution Explorer and double cpck on the MFCGDIDemoView.cpp file under the Source Files folder.

Step 3 − Draw the pne as shown below in CMFCGDIDemoView::OnDraw() method.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->MoveTo(95, 125);
   pDC->LineTo(230, 125);
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 4 − Run this apppcation. You will see the following output.

Lines

Step 5 − The CDC::MoveTo() method is used to set the starting position of a pne.

When using LineTo(), the program starts from the MoveTo() point to the LineTo() end.

After LineTo() when you do not call MoveTo(), and call again LineTo() with other point value, the program will draw a pne from the previous LineTo() to the new LineTo() point.

Step 6 − To draw different pnes, you can use this property as shown in the following code.

void CMFCGDIDemoView::OnDraw(CDC* pDC) { 
   pDC->MoveTo(95, 125);
   pDC->LineTo(230, 125);
   pDC->LineTo(230, 225);
   pDC->LineTo(95, 325);
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here 
}

Step 7 − Run this apppcation. You will see the following output.

Lines

Polypnes

A polypne is a series of connected pnes. The pnes are stored in an array of POINT or CPoint values. To draw a polypne, you use the CDC::Polypne() method. To draw a polypne, at least two points are required. If you define more than two points, each pne after the first would be drawn from the previous point to the next point until all points have been included.

Step 1 − Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   CPoint Pt[7];
   Pt[0] = CPoint(20, 150);
   Pt[1] = CPoint(180, 150);
   Pt[2] = CPoint(180, 20);
   pDC−Polypne(Pt, 3);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 2 − When you run this apppcation, you will see the following output.

Polypnes

Rectangles

A rectangle is a geometric figure made of four sides that compose four right angles. Like the pne, to draw a rectangle, you must define where it starts and where it ends. To draw a rectangle, you can use the CDC::Rectangle() method.

Step 1 − Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->Rectangle(15, 15, 250, 160);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 2 − When you run this apppcation, you will see the following output.

Rectangles

Squares

A square is a geometric figure made of four sides that compose four right angles, but each side must be equal in length.

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->Rectangle(15, 15, 250, 250);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

When you run this apppcation, you will see the following output.

Squares

Pies

A pie is a fraction of an elppse depmited by two pnes that span from the center of the elppse to one side each. To draw a pie, you can use the CDC::Pie() method as shown below −

BOOL Pie(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

    The (x1, y1) point determines the upper-left corner of the rectangle in which the elppse that represents the pie fits. The (x2, y2) point is the bottom-right corner of the rectangle.

Pies

    The (x3, y3) point specifies the starting corner of the pie in a default counterclockwise direction.

    The (x4, y4) point species the end point of the pie.

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->Pie(40, 20, 226, 144, 155, 32, 202, 115);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 2 − When you run this apppcation, you will see the following output.

Pies

Arcs

An arc is a portion or segment of an elppse, meaning an arc is a non-complete elppse. To draw an arc, you can use the CDC::Arc() method.

BOOL Arc(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

Arcs

The CDC class is equipped with the SetArcDirection() method.

Here is the syntax −

int SetArcDirection(int nArcDirection)

Sr.No. Value & Orientation
1

AD_CLOCKWISE

The figure is drawn clockwise

2

AD_COUNTERCLOCKWISE

The figure is drawn counterclockwise

Step 1 − Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->SetArcDirection(AD_COUNTERCLOCKWISE);
   pDC->Arc(20, 20, 226, 144, 202, 115, 105, 32);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

Step 2 − When you run this apppcation, you will see the following output.

Arcs

Chords

The arcs we have drawn so far are considered open figures because they are made of a pne that has a beginning and an end (unpke a circle or a rectangle that do not). A chord is an arc whose two ends are connected by a straight pne.

Chords

To draw a chord, you can use the CDC::Chord() method.

BOOL Chord(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   pDC->SetArcDirection(AD_CLOCKWISE);
   pDC->Chord(20, 20, 226, 144, 202, 115, 105, 32);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

When you run the above apppcation, you will see the following output.

Chords

The arc direction in this example is set clockwise.

Colors

The color is one the most fundamental objects that enhances the aesthetic appearance of an object. The color is a non-spatial object that is added to an object to modify some of its visual aspects. The MFC pbrary, combined with the Win32 API, provides various actions you can use to take advantage of the various aspects of colors.

The RGB macro behaves pke a function and allows you to pass three numeric values separated by a comma. Each value must be between 0 and 255 as shown in the following code.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   COLORREF color = RGB(239, 15, 225);
}

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   COLORREF color = RGB(239, 15, 225);
   pDC->SetTextColor(color);
   pDC->TextOut(100, 80, L"MFC GDI Tutorial", 16);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

When you run this apppcation, you will see the following output.

Colors

Fonts

CFont encapsulates a Windows graphics device interface (GDI) font and provides member functions for manipulating the font. To use a CFont object, construct a CFont object and attach a Windows font to it, and then use the object s member functions to manipulate the font.

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   CFont font;
   font.CreatePointFont(920, L"Garamond");
   CFont *pFont = pDC->SelectObject(&font);
   COLORREF color = RGB(239, 15, 225);
   pDC->SetTextColor(color);
   pDC->TextOut(100, 80, L"MFC GDI Tutorial", 16);
   pDC->SelectObject(pFont);
   font.DeleteObject();
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
   return;

   // TODO: add draw code for native data here
}

When you run the above apppcation, you will see the following output.

Fonts

Pens

A pen is a tool used to draw pnes and curves on a device context. In the graphics programming, a pen is also used to draw the borders of a geometric closed shape such as a rectangle or a polygon. Microsoft Windows considers two types of pens — cosmetic and geometric.

A pen is referred to as cosmetic when it can be used to draw only simple pnes of a fixed width, less than or equal to 1 pixel. A pen is geometric when it can assume different widths and various ends. MFC provides a class CPen which encapsulates a Windows graphics device interface (GDI) pen.

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   CPen pen;
   pen.CreatePen(PS_DASHDOTDOT, 1, RGB(160, 75, 90));
   pDC->SelectObject(&pen);
   pDC->Rectangle(25, 35, 250, 125);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

When you run the above apppcation, you will see the following output.

Pens

Brushes

A brush is a drawing tool used to fill out closed shaped or the interior of pnes. A brush behaves pke picking up a bucket of paint and pouring it somewhere. MFC provides a class CBrush which encapsulates a Windows graphics device interface (GDI) brush.

Let us look into a simple example.

void CMFCGDIDemoView::OnDraw(CDC* pDC) {
   CBrush brush(RGB(100, 150, 200));
   CBrush *pBrush = pDC->SelectObject(&brush);
   pDC->Rectangle(25, 35, 250, 125);
   pDC->SelectObject(pBrush);
   
   CMFCGDIDemoDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if (!pDoc)
      return;

   // TODO: add draw code for native data here
}

When you run this apppcation, you will see the following output.

Brushes Advertisements