GDI+学习之 ------ 画线、区域填充、写字

《精通GDI编程》里的代码,在学习过程中对它加以总结,以防以后用到,所有代码都是在MFC 单文档中实现的,写在View::OnDraw(CDC */*pDC*/)中

画线/边框(Pen)

1、画单线-------DrawLine

[cpp] view
plain
copy

  1. Pen pen(Color(255,0,0,0),3);
  2. PointF L_PTStart(0,0);
  3. PointF L_PTEnd(100,10);
  4. graphics.DrawLine(&pen,L_PTStart,L_PTEnd);

2、连接线--------DrawLines

[cpp] view
plain
copy

  1. Pen blackPen(Color(255, 0, 0, 0), 3);
  2. PointF point1(10.0f, 10.0f);
  3. PointF point2(10.0f, 100.0f);
  4. PointF point3(200.0f, 50.0f);
  5. PointF point4(250.0f, 80.0f);
  6. PointF points[4] = {point1, point2, point3, point4};
  7. PointF* pPoints = points;
  8. graphics.DrawLines(&blackPen, pPoints, 4);

讲解:points数组中的每个点都是连接线上的转折点,DrawLines会把它们按照顺序一个个连接起来

3、画矩形-----DrawRectangle,只画边框,不画背景色

[cpp] view
plain
copy

  1. Pen blackPen(Color(255,255, 0, 0), 3);
  2. Rect rect(0, 0, 100, 100);
  3. graphics.DrawRectangle(&blackPen, rect);

4、一次画多个矩形----DrawRectangles

[cpp] view
plain
copy

  1. Pen blackPen(Color(255, 0, 255, 0), 3);
  2. // 定义三个矩形
  3. RectF rect1(0.0f, 0.0f, 50.0f, 60.0f);
  4. RectF rect2(60.0f, 70.0f, 70.0f, 100.0f);
  5. RectF rect3(100.0f, 0.0f, 50.0f, 50.0f);
  6. RectF rects[] = {rect1, rect2, rect3};
  7. //RectF是对Rect的封装
  8. graphics.DrawRectangles(&blackPen, rects, 3);

5、画曲线-----DrawCurve

[cpp] view
plain
copy

  1. Pen greenPen(Color::Green, 3);
  2. PointF point1(100.0f, 100.0f);
  3. PointF point2(200.0f, 50.0f);
  4. PointF point3(400.0f, 10.0f);
  5. PointF point4(500.0f, 100.0f);
  6. PointF curvePoints[4] = {
  7. point1,
  8. point2,
  9. point3,
  10. point4};
  11. PointF* pcurvePoints = curvePoints;
  12. // 画曲线
  13. graphics.DrawCurve(&greenPen, curvePoints, 4);
  14. //画连接点和直线连接线
  15. SolidBrush redBrush(Color::Red);
  16. graphics.FillEllipse(&redBrush, Rect(95, 95, 10, 10));//画连接点
  17. graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));
  18. graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));
  19. graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));
  20. Pen redPen(Color::Red, 2);
  21. graphics.DrawLines(&redPen,curvePoints,4);//画连接线

注意:这里为了比较画曲线与画直线连接线的区别,我用绿色画的曲线,用红色画的直线连接线,同时画出了连接点,大家可以比较一下。

6、画闭合曲线

[cpp] view
plain
copy

  1. Pen greenPen(Color::Green, 3);
  2. PointF point1(100.0f, 100.0f);//开始点
  3. PointF point2(200.0f, 50.0f);
  4. PointF point3(400.0f, 10.0f);
  5. PointF point4(500.0f, 100.0f);
  6. PointF point5(600.0f, 200.0f);
  7. PointF point6(700.0f, 400.0f);
  8. PointF point7(500.0f, 500.0f);//结束点
  9. PointF curvePoints[7] = {
  10. point1,
  11. point2,
  12. point3,
  13. point4,
  14. point5,
  15. point6,
  16. point7};
  17. PointF* pcurvePoints = curvePoints;
  18. //画闭合曲线
  19. graphics.DrawClosedCurve(&greenPen, curvePoints, 7);
  20. //画连接点
  21. SolidBrush redBrush(Color::Red);
  22. SolidBrush startBrush(Color::Blue);
  23. SolidBrush endBrush(Color::Black);
  24. graphics.FillEllipse(&startBrush, Rect(95, 95, 10, 10));
  25. graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));
  26. graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));
  27. graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));
  28. graphics.FillEllipse(&redBrush, Rect(595, 195, 10, 10));
  29. graphics.FillEllipse(&redBrush, Rect(695, 395, 10, 10));
  30. graphics.FillEllipse(&endBrush, Rect(495, 495, 10, 10));

注意:蓝色点是开始点,黑色点是结束点

7、画多边形-----DrawPolygon,既然能画闭合的曲线,肯定也有闭合的直线,当然闭合的直线也就是所谓的多边形

[cpp] view
plain
copy

  1. Pen blackPen(Color(255, 0, 0, 0), 3);
  2. //创建点数组,DrawPolygon会按这些点的顺序逐个连接起来
  3. PointF point1(100.0f, 100.0f);
  4. PointF point2(200.0f, 130.0f);
  5. PointF point3(150.0f, 200.0f);
  6. PointF point4(50.0f, 200.0f);
  7. PointF point5(0.0f, 130.0f);
  8. PointF points[5] = {point1, point2, point3, point4, point5};
  9. PointF* pPoints = points;
  10. // 画多边形,也就是闭合直线
  11. graphics.DrawPolygon(&blackPen, pPoints, 5);

8、画弧线----DrawArc

[cpp] view
plain
copy

  1. Pen redPen(Color::Red, 3);
  2. RectF ellipseRect(0, 0, 200, 100);
  3. REAL startAngle = 0.0f;//起始度数
  4. REAL sweepAngle = 90.0f;//结尾时的度数
  5. // 画弧线
  6. graphics.DrawArc(&redPen, ellipseRect, startAngle, sweepAngle);
  7. //画出边框,做为参考
  8. Pen greenPen(Color::Green, 1);
  9. graphics.DrawRectangle(&greenPen,ellipseRect);

9、画扇形----DrawPie

[cpp] view
plain
copy

  1. Pen blackPen(Color(255, 0, 255, 0), 3);
  2. // 定义椭圆,然后在里面截一部分作为最终的扇形
  3. RectF ellipseRect(0, 0, 200, 100);
  4. REAL startAngle = 40.0f;
  5. REAL sweepAngle = 100.0f;
  6. //画扇形
  7. graphics.DrawPie(&blackPen, ellipseRect, startAngle, sweepAngle);

先出效果图:

这里要对它两上名词讲解一下,什么叫startAngle(开始度数),什么叫sweepAngle(范围度数也能叫扫过度数,我译的,嘿嘿)

看下MSDN里对DrawPie函数的讲解就会懂了,里面有这个图,给大家看一下

填充区域(SolidBrush)

1、填充闭合区域----FillClosedCurve,边框对应:DrawClosedCurve

[cpp] view
plain
copy

  1. SolidBrush blackBrush(Color(255, 0, 0, 0));
  2. PointF point1(100.0f, 100.0f);
  3. PointF point2(200.0f, 50.0f);
  4. PointF point3(250.0f, 200.0f);
  5. PointF point4(50.0f, 150.0f);
  6. PointF points[4] = {point1, point2, point3, point4};
  7. //填充闭合区域
  8. graphics.FillClosedCurve(&blackBrush, points, 4);
  9. //为闭合区域画边框
  10. Pen curPen(Color::Green,3);
  11. graphics.DrawClosedCurve(&curPen,points,4);

注意:从结果图中也可以看出填充区域(背景)和边框是分离的,用FillClosedCurve来填充背景色,用DrawClosedCurve来画边框

2、填充椭圆---FillEllipse,边框对应:DrawEllipse

[cpp] view
plain
copy

  1. SolidBrush blackBrush(Color(255, 0, 0, 0));
  2. RectF ellipseRect(0.0f, 0.6f, 200.8f, 100.9f);
  3. //填充椭圆
  4. graphics.FillEllipse(&blackBrush, ellipseRect);
  5. //画边框,当然也可以不画
  6. Pen borderPen(Color::Green,3);
  7. graphics.DrawEllipse(&borderPen,ellipseRect);

还有类似的几个函数,这里就不一 一讲解了

它们是:

[cpp] view
plain
copy

  1. FillPie(Brush* brush, RectF& rect, REAL startAngle, REAL sweepAngle)    //填充扇形,对应DrawPie
  2. FillPolygon(Brush* brush, PointF* points, INT count)                       //填充多边形,对应DrawPolygon
  3. FillRectangle(Brush* brush, RectF& rect)                                          //填充矩形,对应DrawRectangle
  4. FillRectangles(Brush* brush, RectF* rects, INT count)                   //同时填充多个矩形,对应DrawRectangles

还有是关于路径和区域的,先记下,后面再说

[cpp] view
plain
copy

  1. Status FillPath( const Brush* brush, const GraphicsPath*path);
  2. Status FillRegion( const Brush* brush, const Region*region);

写字(SolidBrush)

形式一:Status DrawString( const WCHAR*string, INTlength, const Font* font,
const PointF&
origin, const Brush*brush);

[cpp] view
plain
copy

  1. Graphics graphics(this->GetDC()->m_hDC);
  2. SolidBrush brush(Color(255,0,0,255));
  3. FontFamily fontfamily(L"宋体");
  4. Font font(&fontfamily,24,FontStyleRegular,UnitPixel);
  5. PointF  pointf(0,0);//PointF类对点进行了封装,这里是指定写字的开始点
  6. graphics.DrawString(L"GDI写字",-1,&font,pointf,&brush);
  7. //DrawString还有另外两个重载形式,能实现更强大的功能

形式二:Status DrawString( const WCHAR*string, INT length, const Font*font,
const RectF&
layoutRect, const StringFormat*stringFormat, const Brush*brush);

[cpp] view
plain
copy

  1. WCHAR string[256];
  2. wcscpy(string, L"Sample Text");
  3. // Initialize arguments.
  4. Font myFont(L"Arial", 16);//字体
  5. RectF layoutRect(0.0f, 0.0f, 200.0f, 50.0f);//矩形
  6. //设定字体格式
  7. StringFormat format;
  8. format.SetAlignment(StringAlignmentCenter); //水平方向的对齐方式,这里设置为水平居中
  9. format.SetLineAlignment(StringAlignmentFar);//垂直方向的对齐方式,这里设置为垂直居下
  10. SolidBrush blackBrush(Color(255, 0, 0, 0));
  11. //画矩形边框
  12. graphics.DrawRectangle(&Pen(Color::Green, 3), layoutRect);
  13. //填充矩形背景
  14. graphics.FillRectangle(&SolidBrush(Color(255,255,0,0)),layoutRect);
  15. //DrawString,一定要先画背景再写字,要不然,字会被背景覆盖
  16. graphics.DrawString(
  17. string,
  18. wcslen(string),
  19. &myFont,
  20. layoutRect,
  21. &format,
  22. &blackBrush);

形式三:Status DrawString( const WCHAR*string, INTlength, const Font* font,
const PointF&
origin, const StringFormat*stringFormat, const Brush* brush);

这种形式是形式一与形式二的结合,指定写字开始点和字体格式,这里就不举例了。

时间: 2024-07-30 12:10:52

GDI+学习之 ------ 画线、区域填充、写字的相关文章

openCV 和GDI画线效率对照

一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大小,函数通过參数输入.用GetTickCount来实现计时功能. 三. GDI的主要循代码例如以下: void show_line(int line_num,int point_num) { ULONAG start_time = get_tick_count(); VMGdiPolygon* te

openCV 和GDI画线效率对比

一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大小,函数通过参数输入,用GetTickCount来实现计时功能. 三. GDI的主要循代码如下: void show_line(int line_num,int point_num) { ULONAG start_time = get_tick_count(); VMGdiPolygon* test

MFC画线功能总结

MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始端点,其二,利用不同的方法实现画线.下面着重学习总结画线功能实现方法. 在OnLButtonDown函数中记录起始端点,CPoint m_ptOrigin = point; 在OnLButtonUp函数中实现画线.方法如下: 画线方法一:利用SDK全局函数实现视图窗口画线功能 //获取设备描述表 HDC hdc; //调用全局函数获得当前窗口的设备描述表,CWnd::m_hWnd根据继承原理,CDrawView继承了CWnd类的数据成

GDI+学习笔记(六)渐变画刷

画刷,顾名思义,就是像画刷一样,向设备上绘制,还记得小时候常唱的首歌,"我是一个粉刷匠.." 好吧,跑题了. 本系列博客希望尽可能简单的描述每项功能,而不希望把每个参数都介绍的详详细细,如果需要,请查阅msdn,本节讲述的渐变画刷,主要有两种,一种是叫线性画刷(LinearGradientBrush),还有一种叫路径画刷(PathGradientBrush),我希望以一种尽可能简单的方式去描述它,但能力有限,所以有什么意见,希望各位能帮忙提出,谢谢. (一)使用画刷 上一节中,我们实际

[游戏学习25] MFC 橡皮筋画线效果

>_<:这是给出窗口内外不同情况的处理展示的例子. >_<:MouseCap.h 1 #include<afxwin.h> 2 class CMyApp :public CWinApp 3 { 4 public: 5 virtual BOOL InitInstance(); 6 }; 7 class CMainWindow:public CFrameWnd 8 { 9 protected: 10 BOOL m_bTracking; //标志:鼠标按下为真,否则为假 11

unity3d NavMeshAgent 寻路画线/画路径

今天在群里看见有个小伙在问Game视图寻路时怎么画线 正好前几天写了个寻路,而且自己也不知道具体怎么在寻路时画线,所以决定帮帮他,自己也好学习一下 在百度查了一下资料,直接搜寻路画路径.寻路画线...... 我可不是伸手党,我只是想看看别人是怎么实现的 结果什么都没有搜到!!那就直接搜unity3d 画线吧.....  果然很多资料!! Debug.DrawLine:使用这个函数只能在 screen 中看见画的线,在 game 中看不见 那我们要怎么在game中画线呢 百度给我答案:LineRe

GDI+学习笔记(五)绘制一个正方体

本文将介绍如何利用GDI+绘制一个正方体. (一)准备阶段 想象一下,高中的时候,我们在学立体几何的时候是怎样画一个正方体的,我们在一张纸上利用投影的思路将其绘制在一张纸上,对吧,这计算投影的部分,我们暂且忽略.下图是我用windows的画图绘制的一个正方体: 我们计算出这些点在平面上的坐标如下: Point A(100,200); Point B(200,200); Point C(100,300); Point D(200,300); Point E(100+50*1.414, 200-50

GDI+学习笔记

7.1.1 GDI+概述 GDI+是微软在Windows 2000以后操作系统中提供的新的图形设备接口,其通过一套部署为托管代码的类来展现, 这套类被称为GDI+的“托管类接口”,GDI+主要提供了以下三类服务: (1) 二维矢量图形:GDI+提供了存储图形基元自身信息的类(或结构体).存储图形基元绘制方式信息的类以及实际进行绘制的类. (2) 图像处理:大多数图片都难以划定为直线和曲线的集合,无法使用二维矢量图形方式进行处理. 因此,GDI+为我们提供了Bitmap.Image等类,它们可用于

GDI+学习笔记(九)带插件的排序算法演示器(MFC中的GDI+实例)

带插件的排序算法演示器 本节将通过一个实例来说明GDI+在MFC中的应用.这个算法演示器其实是本人算法系列的一个开端,由于csdn没有树状的目录结构,咱也只好使用链表了不是?好了,废话不多说,开始今天的文章. (一)功能说明 我们初步制定功能如下: (1). 能够通过柱状图,自动展示排序算法的交换比较过程 (2). 能够使用插件的形式进行开发.即,当新完成一个算法后,只需要完成一个插件文件(我们这里使用动态库dll),由主程序加载插件,即可进行执行,而不再需要重新编译主程序. (3). 保证主程