windows的绘图工具

  1. 画笔CPen
  2. 画刷CBrush
  3. 调色板CPalette

画笔通常具有宽度 样式和颜色3中属性

构造函数

  1. CPen( );
  2. CPen( int nPenStyle, int nWidth, COLORREF crColor );
  3. CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL );

当使用第一种构造函数时,还得继续调用一下函数

  • CPen::CreatePen
  • BOOL CreatePen( int nPenStyle, int nWidth, COLORREF crColor );
  • BOOL CreatePen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL );
  • BOOL CreatePenIndirect( LPLOGPEN lpLogPen );

涉及的结构体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef struct tagLOGPEN {  /* lgpn */
UINT lopnStyle;
POINT lopnWidth;
COLORREF lopnColor;
} LOGPEN;
typedef struct tagLOGBRUSH {
UINT lbStyle;
COLORREF lbColor;
LONG lbHatch;
} LOGBRUSH, *PLOGBRUSH;
typedef struct tagPOINT {
LONG x;
LONG y;
} POINT;

例子

  1. 1
    2
    CPen pen;
    pen.CreatePen(PS_SOLID,1,RGB(0,0,225));
  2. 1
    CPen *pen1=new CPen(PS_SOLID,1,RGB(0,0,225))

画刷通常具有填充色 填充图案和填充样式3种属性

构造函数

  • CBrush( );
  • CBrush( COLORREF crColor );
  • CBrush( int nIndex, COLORREF crColor );
  • CBrush( CBitmap* pBitmap );

If you use the constructor with no arguments, you must initialize the resulting CBrush object with
CreateSolidBrush,
CreateHatchBrush,
CreateBrushIndirect,
CreatePatternBrush, or
CreateDIBPatternBrush.

例子

  1. 1
    2
    CBrush brush1;   // Must initialize!
    brush1.CreateSolidBrush(RGB(0, 0, 255)); // Blue brush.
1
2
CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0));
CBrush brush3(HS_DIAGCROSS, RGB(0, 255, 0));
  1. 1
    2
    3
    CBitmap bmp;
    bmp.LoadBitmap(IDB_BRUSH);
    CBrush brush4(&bmp);

绘画函数


画点

CDC::SetPixel

1
2
COLORREF SetPixel( int x, int y, COLORREF crColor );
COLORREF SetPixel( POINT point, COLORREF crColor );

CDC::SetPixelV

1
2
BOOL SetPixelV(int x, int y, COLORREF crColor);
BOOL SetPixelV( POINT point, COLORREF crColor );

画线

CDC::MoveTo

(获取当前点CDC::GetCurrentPosition)

1
2
CPoint MoveTo( int x, int y );
CPoint MoveTo( POINT point );

Moves the current position to the point specified by x and y (or by point).

CDC::LineTo

1
2
BOOL LineTo( int x, int y );
BOOL LineTo( POINT point );

Draws a line from the current position up to, but not including, the point specified by x and y (or point). The line is drawn with the selected pen. The current position is set to x,y or to point.


椭圆和弧

画椭圆

CDC::Ellipse

1
2
BOOL Ellipse( int x1, int y1, int x2, int y2 );
BOOL Ellipse( LPCRECT lpRect );

Draws an ellipse. The center of the ellipse is the center of the bounding rectangle specified by x1, y1, x2, and y2, or lpRect. The ellipse is drawn with the current pen, and its interior is filled with the current brush.

画椭圆弧

CDC::Arc

1
2
BOOL Arc( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );
BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

Since an arc is not a closed figure, it is not filled.

画带弦的椭圆弧

CDC::Chord

1
2
BOOL Chord( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );
BOOL Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

The chord is drawn by using the selected pen and filled by using the selected brush.

画一条椭圆弧并且弧的两个端点与圆心连线

CDC::Pie

1
2
BOOL Pie( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4 );
BOOL Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd );

Draws a pie-shaped wedge by drawing an elliptical arc whose center and two endpoints are joined by lines. The pie-shaped area is filled with the current brush


线段、曲线

画连续的线段(从数组中的第一个点开始)

CDC::Polyline

1
BOOL Polyline( LPPOINT lpPoints, int nCount );

Return Value
Nonzero if the function is successful; otherwise 0.

Parameters
lpPoints
Points to an array of POINT structures or CPoint objects to be connected.
nCount
Specifies the number of points in the array. This value must be at least 2.

画连续的线段(会从当前的点开始)

CDC::PolylineTo

1
BOOL PolylineTo( const POINT* lpPoints, int nCount );

A line is drawn from the current position to the first point specified by the lpPoints parameter by using the current pen.

画贝塞尔曲线

CDC::PolyBezier

1
2
3
4
5
BOOL PolyBezier( const POINT* lpPoints, int nCount );

CDC::PolyBezierTo
```C
BOOL PolyBezierTo( const POINT* lpPoints, int nCount );

The first spline is drawn from the current position to the third point by using the first two points as control points.

画多组连接的线段

CDC::Polyline

1
2
BOOL Polyline( const POINT* lpPoints, const DWORD* lpPolyPoints, int nCount );
参数

lpPoints
Points to an array of variables specifying the number of points in the lpPoints array for the corresponding polygon. Each entry must be greater than or equal to 2.

绘任意多边形

CDC::Polygon

1
BOOL Polygon( LPPOINT lpPoints, int nCount );

矩形

填充矩形

CDC::Rectangle

1
2
BOOL Rectangle( int x1, int y1, int x2, int y2 );
BOOL Rectangle( LPCRECT lpRect );

Draws a rectangle using the current pen. The interior of the rectangle is filled using the current brush.

画一个带圆角的矩形

CDC::RoundRect

1
2
BOOL RoundRect( int x1, int y1, int x2, int y2, int x3, int y3 );
BOOL RoundRect( LPCRECT lpRect, POINT point );

用指定的颜色填充矩形

CDC::FillSolidRect

1
2
void FillSolidRect( LPCRECT lpRect, COLORREF clr );
void FillSolidRect( int x, int y, int cx, int cy, COLORREF clr );

Remarks
Call this member function to fill the given rectangle with the specified solid color.
FillSolidRect is very similar to CDC::FillRect; however, FillSolidRect uses only solid colors (indicated by the COLORREF parameter), while FillRect takes a brush and therefore can be used to fill a rectangle with a solid color, a dithered color, hatched brushes, or a pattern. FillSolidRect usually is faster than FillRect.

使用指定的画刷填充矩形

CDC::FillRect

1
void FillRect( LPCRECT lpRect, CBrush* pBrush );

Remarks
Call this member function to fill a given rectangle using the specified brush. The function fills the complete rectangle, including the left and top borders, but it does not fill the right and bottom borders.

使用指定的画刷填充矩形,可以指定样式

CDC::ExtFloodFill

1
BOOL ExtFloodFill( int x, int y, COLORREF crColor, UINT nFillType );

Remarks
Fills an area of the display surface with the current brush. This member function offers more flexibility than FloodFill because you can specify a fill type in nFillType.

使用当前画刷填充显示区域

CDC::FloodFill

1
BOOL FloodFill( int x, int y, COLORREF crColor );

Remarks
Fills an area of the display surface with the current brush. The area is assumed to be bounded as specified by crColor. The FloodFill function begins at the point specified by x and y and continues in all directions to the color boundary.

Only memory-device contexts and devices that support raster-display technology support the FloodFill member function.