绘制直线、矩形、曲线和多边形

参考:《精通GDI+编程》

Graphics类分别对应提供了不同的函数,都需要至少创建两个对象,

一个Graphics对象和一个画笔对象。

画笔对象存储了所有要绘制项目的属性,如线条宽度与颜色。

画直线:DrawLIne()

0、Graphics构造函数

    Graphics(IN HDC hdc);
    Graphics(IN HDC hdc, IN HANDLE hdevice);
    Graphics(IN HWND hwnd, IN BOOL icm = FALSE);
    Graphics(IN Image* image);

hdc:设备环境句柄

hWnd:窗口句柄

icm:是否使用色彩配置文件校正色彩

image:图像对象

hdevice:设备句柄

1、绘制直线

	 DrawLine(const Pen* pen, const Point& pt1,	const Point& pt2);
	 DrawLine(const Pen* pen, REAL x1, REAL y1, REAL x2, REAL y2);
	 DrawLine(const Pen* pen, const PointF& pt1, const PointF& pt2);
	 DrawLine(const Pen* pen, INT x1, INT y1, INT x2, INT y2);
	 DrawLines(const Pen* pen,	const Point* points, INT count);
	 DrawLines(const Pen* pen,	const PointF* points, INT count);

 

注:
typedef float REAL;

class PointF
{
public:
//...
    REAL X;
    REAL Y;
};

例子:

	Graphics graphics(this->m_hWnd);
	Pen blackPen(Color(0,0,0), 3);
	//定义点的位置
	Point point1(10, 10);
	Point point2(10, 100);
	Point point3(50, 50);
	Point point4(50, 140);
	Point point[4] = {point1, point2, point3, point4};
	graphics.DrawLines(&blackPen, point, 4);

2、绘制矩形

    DrawRectangle(const Pen* pen, const RectF& rect);
    DrawRectangle(const Pen* pen, REAL x, REAL y, REAL width, REAL height);    
    DrawRectangle(const Pen* pen, const Rect& rect); 
    DrawRectangle(const Pen* pen, INT x, INT y, INT width, INT height);
    DrawRectangles(const Pen* pen, const Rect* rects, INT count);
    DrawRectangles(const Pen* pen, const RectF* rects, INT count);

实例:

         Graphics graphics(this->m_hWnd);
	Pen blackPen(Color(0,0,0), 3);
	RectF rect1(10.0f, 10.0f, 100.0f, 50.0f);
	RectF rect2(40.0f, 40.0f, 100.0f, 50.0f);
	RectF rect3(80.0f, 4.0f, 100.0f, 50.0f);
	RectF rect[3] = {rect1, rect2, rect3};
	graphics.DrawRectangles(&blackPen, rect, 3);

3、绘制曲线

曲线比较少用,就简单了解下

DrawCurve()

DrawClosedCurve() 闭合曲线

DrawBezier() 贝塞尔曲线

例子

	CDC *pDC = GetDC();
	Graphics graphics(pDC->m_hDC);
	Pen GreenPen(Color::Green, 3);

	Point point1(100, 100);
	Point point2(200, 200);
	Point point3(300, 100);
	Point point4(400, 200);
	Point point[4] = {point1, point2, point3, point4};
	graphics.DrawCurve(&GreenPen, point, 4);

4、绘制多边形

     DrawPolygon( const Pen* pen,  const PotF* pots,  T count);
     DrawPolygon( const Pen* pen,  const Pot* pots,  T count);

例子:

	CDC *pDC = GetDC();
	Graphics graphics(pDC->m_hDC);
	Pen GreenPen(Color::Green, 3);

	Point point1(100, 100);
	Point point2(200, 100);
	Point point3(300, 100);
	Point point4(500, 200);
	Point point[4] = {point1, point2, point3, point4};
	graphics.DrawPolygon(&GreenPen, point, 4);

5、绘制其他几何图形

 绘制弧线:

    DrawArc(const Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle);
    DrawArc(const Pen* pen, const RectF& rect, REAL startAngle, REAL sweepAngle);
    DrawArc(const Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle);
    DrawArc(const Pen* pen, const Rect& rect, REAL startAngle, REAL sweepAngle);

例子:

	CDC *pDC = GetDC();
	Graphics graphics(pDC->m_hDC);
	Pen GreenPen(Color::Green, 3);
	Pen RedPen(Color::Red, 3);
	Rect rect(10,10,200,100);
	REAL startAngle = 0.0f; //度为单位
	REAL sweepAngle = -60.0f;

	//绘制弧线
	graphics.DrawArc(&GreenPen, rect, startAngle, sweepAngle);
	//绘制边框
	graphics.DrawRectangle(&RedPen, rect);

绘制扇形:

    DrawPie(const Pen* pen, const RectF& rect, REAL startAngle, REAL sweepAngle);
    DrawPie(const Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle);
    DrawPie(const Pen* pen, const Rect& rect, REAL startAngle, REAL sweepAngle);
    DrawPie(const Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle);

例子:

	CDC *pDC = GetDC();
	Graphics graphics(pDC->m_hDC);
	Pen GreenPen(Color::Green, 3);
	Pen RedPen(Color::Red, 3);
	Rect rect(10,10,200,100);
	REAL startAngle = 0.0f; //度为单位
	REAL sweepAngle = -60.0f;

	//绘制扇形
	graphics.DrawPie(&GreenPen, rect, startAngle, sweepAngle);
	//绘制边框
	graphics.DrawRectangle(&RedPen, rect);

 

 

 

转载自:https://blog.csdn.net/s110600824s/article/details/8672887

You may also like...

退出移动版