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

画线/边框(Pen)

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

  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

  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,仅仅画边框。不画背景色

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

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

  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

  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、画闭合曲线

  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,既然能画闭合的曲线。肯定也有闭合的直线。当然闭合的直线也就是所谓的多边形

  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

  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

  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

  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

  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);

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

它们是:

  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

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

  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);

  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);

  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);

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

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

  1. .NET CAD二次开发学习 对称画线功能

    [CommandMethod("CBline")] //对称画线 public void CBline() { Document doc = Application.Documen ...

  2. webgl学习总结画线面及场景和物体动

    WebGL是在浏览器中实现三维效果的一套规范.是浏览器中的3D引擎,是利用js代码来实现加载3D模型,渲染.输出等功能,从而实现在浏览器和微信中浏览三维文件的效果. three.js是基于WebGL的 ...

  3. openCV 和GDI画线效率对照

    一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大 ...

  4. openCV 和GDI画线效率对比

    一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...

  5. MFC画线功能总结

    本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...

  6. MFC消息映射机制以及画线功能实现

    ---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...

  7. C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9

    视频图像处理系列 索引 VS2013下测试通过. 在百度中搜索关键字“DirectX SDk”,或者进入微软官网https://www.microsoft.com/en-us/download/det ...

  8. win32画线考虑去锯齿

    整理日: 2015年2月16日 这几天一直在研究win32 SDk下画线去锯齿,之前一直用的QT的画线接口函数,里面有去锯齿的效果,可是突然项目要求不能用QT的只能用win32 SDK下的GDI画线接 ...

  9. GDI+学习---2.GDI+编程模式及组成类

    在使用GDI+的时候,您不必像在GDI中那样关心设备场景句柄,只需简单地创建一个Graphics对象,然后以您熟悉的面向对象的方式(如myGraphicsObject.DrawLine(paramet ...

随机推荐

  1. 扩展 IHttpModule

    上篇提到请求进入到System.Web后,创建完HttpApplication对象后会执行一堆的管道事件,然后可以通过HttpModule来对其进行扩展,那么这篇文章就来介绍下如何定义我们自己的mod ...

  2. JavaSE-06 二维数组

    学习要点 二维数组的定义 二维数组内存数据结构 不规则二维数组 二维数组的定义 语法格式 格式一 数据类型[][] 数组名 = new 数据类型[m][n]; m:表示这个二维数组有多少个一维数组. ...

  3. vue hash模式和404页面的配置

    1.设置我们的路由配置文件(/src/router/index.js): { path:'*', component:Error } 这里的path:’*’就是找不到页面时的配置,component是 ...

  4. 用1天快速上手org-mode(windows系统)

    Table of Contents 1. 选择Emacs的理由--Org-mode 1.1. 现状(基于本人现有软件的使用) 1.2. 理念(够用才好) 1.3. 学习过程(少走弯路) 2. 快速安装 ...

  5. [BZOJ] 1037 [ZJOI2008]生日聚会

    Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3007 Solved: 1795 [Submit][Status][Discuss] Descript ...

  6. 色码表 Color code table

    最近打算更新设计博客页面,需要用到CSS色码表,查了一些资料现转载此处以备以后使用,点击此处查看原文,另外还发现了几个不错的网站: color-hex HTML颜色代码 色碼表 色碼表英文為 Colo ...

  7. PHP 数组使用之道

    本文首发于 PHP 数组使用之道,转载请注明出处. 这个教程我将通过一些实用的实例和最佳实践的方式列举出 PHP 中常用的数组函数.每个 PHP 工程师都应该掌握它们的使用方法,以及如何通过组合使用来 ...

  8. Python基础知识点总结

    Python基础知识与常用数据类型 一.Python概述: 1.1.Python的特点: 1.Python是一门面向对象的语言,在Python中一切皆对象 2.Python是一门解释性语言 3.Pyt ...

  9. Leetcode 204计数质数

    计数质数 统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 比计算少n中素数的个数. 素数又称质 ...

  10. NYOJ-1188并集与交集,STL的灵活运用!

    并集与交集 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 给你两个字符串的集合A和B,让你求这两个字符串集合的并集和交集,按字典序排序后输出. 然后又给出给出两个字符串 ...