当前位置: 首页 > news >正文

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.   
  3. PointF point1(10.0f, 10.0f);  
  4. PointF point2(10.0f, 100.0f);  
  5. PointF point3(200.0f, 50.0f);  
  6. PointF point4(250.0f, 80.0f);  
  7.   
  8. PointF points[4] = {point1, point2, point3, point4};  
  9. PointF* pPoints = points;  
  10.   
  11. 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.   
  7. PointF curvePoints[4] = {  
  8.     point1,  
  9.     point2,  
  10.     point3,  
  11.     point4};  
  12.   
  13.     PointF* pcurvePoints = curvePoints;  
  14.   
  15.     // 画曲线  
  16.     graphics.DrawCurve(&greenPen, curvePoints, 4);  
  17.   
  18.     //画连接点和直线连接线  
  19.     SolidBrush redBrush(Color::Red);  
  20.     graphics.FillEllipse(&redBrush, Rect(95, 95, 10, 10));//画连接点  
  21.     graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));  
  22.     graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));  
  23.     graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));  
  24.   
  25.     Pen redPen(Color::Red, 2);  
  26.     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.   
  10. PointF curvePoints[7] = {  
  11.     point1,  
  12.     point2,  
  13.     point3,  
  14.     point4,  
  15.     point5,  
  16.     point6,  
  17.     point7};  
  18.   
  19.     PointF* pcurvePoints = curvePoints;  
  20.   
  21.     //画闭合曲线  
  22.     graphics.DrawClosedCurve(&greenPen, curvePoints, 7);  
  23.   
  24.     //画连接点  
  25.     SolidBrush redBrush(Color::Red);  
  26.     SolidBrush startBrush(Color::Blue);  
  27.     SolidBrush endBrush(Color::Black);  
  28.     graphics.FillEllipse(&startBrush, Rect(95, 95, 10, 10));  
  29.     graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));  
  30.     graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));  
  31.     graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));  
  32.     graphics.FillEllipse(&redBrush, Rect(595, 195, 10, 10));  
  33.     graphics.FillEllipse(&redBrush, Rect(695, 395, 10, 10));  
  34.     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. // 定义椭圆。然后在里面截一部分作为终于的扇形  
  4. RectF ellipseRect(0, 0, 200, 100);  
  5. REAL startAngle = 40.0f;  
  6. REAL sweepAngle = 100.0f;  
  7.   
  8. //画扇形  
  9. 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.   
  3. PointF point1(100.0f, 100.0f);  
  4. PointF point2(200.0f, 50.0f);  
  5. PointF point3(250.0f, 200.0f);  
  6. PointF point4(50.0f, 150.0f);  
  7. PointF points[4] = {point1, point2, point3, point4};  
  8.   
  9. //填充闭合区域  
  10. graphics.FillClosedCurve(&blackBrush, points, 4);  
  11. //为闭合区域画边框  
  12. Pen curPen(Color::Green,3);  
  13. 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.   
  3. FillPolygon(Brush* brush, PointF* points, INT count)                       //填充多边形。相应DrawPolygon  
  4.   
  5. FillRectangle(Brush* brush, RectF& rect)                                          //填充矩形,相应DrawRectangle  
  6.   
  7. FillRectangles(Brush* brush, RectF* rects, INT count)                   //同一时候填充多个矩形。相应DrawRectangles  

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

[cpp]  view plain copy
  1. Status FillPath( const Brush* brush, const GraphicsPath*path);  
  2.   
  3. 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.   
  3. SolidBrush brush(Color(255,0,0,255));  
  4.   
  5. FontFamily fontfamily(L"宋体");  
  6. Font font(&fontfamily,24,FontStyleRegular,UnitPixel);  
  7.   
  8. PointF  pointf(0,0);//PointF类对点进行了封装。这里是指定写字的開始点  
  9.   
  10. graphics.DrawString(L"GDI写字",-1,&font,pointf,&brush);  
  11. //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.   
  4. // Initialize arguments.  
  5. Font myFont(L"Arial", 16);//字体  
  6. RectF layoutRect(0.0f, 0.0f, 200.0f, 50.0f);//矩形  
  7.   
  8. //设定字体格式  
  9. StringFormat format;  
  10. format.SetAlignment(StringAlignmentCenter); //水平方向的对齐方式,这里设置为水平居中  
  11. format.SetLineAlignment(StringAlignmentFar);//垂直方向的对齐方式,这里设置为垂直居下  
  12. SolidBrush blackBrush(Color(255, 0, 0, 0));  
  13.   
  14. //画矩形边框  
  15. graphics.DrawRectangle(&Pen(Color::Green, 3), layoutRect);  
  16. //填充矩形背景  
  17. graphics.FillRectangle(&SolidBrush(Color(255,255,0,0)),layoutRect);  
  18. //DrawString,一定要先画背景再写字,要不然,字会被背景覆盖  
  19. graphics.DrawString(  
  20.     string,  
  21.     wcslen(string),  
  22.     &myFont,  
  23.     layoutRect,  
  24.     &format,  
  25.     &blackBrush);  

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

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


相关文章:

  • 参加51CTO学院软考培训,我通过啦
  • 资金流学习-逐笔交易的分析
  • C语言基础知识【作用域规则】
  • C语言基础知识【指针】
  • Apache无法启动报错
  • noip2014 普及组
  • 利用SEH防范BP(int 3)断点
  • 用vue开发单页应用的一些心得
  • leetcode笔记:Search in Rotated Sorted Array
  • 图片文件重命名
  • Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals)
  • 产品和团队
  • MySQL慎用 ENUM 字段
  • mysql取差集、交集、并集
  • Tex: The top-level auxiliary file: *.aux I couldn't open style file IEEEtran.bst 解决方法
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Electron入门介绍
  • java2019面试题北京
  • JavaScript服务器推送技术之 WebSocket
  • JS数组方法汇总
  • Just for fun——迅速写完快速排序
  • python 学习笔记 - Queue Pipes,进程间通讯
  • python3 使用 asyncio 代替线程
  • React Native移动开发实战-3-实现页面间的数据传递
  • RxJS 实现摩斯密码(Morse) 【内附脑图】
  • Vue 2.3、2.4 知识点小结
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 我看到的前端
  • 用Python写一份独特的元宵节祝福
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 怎么将电脑中的声音录制成WAV格式
  • 终端用户监控:真实用户监控还是模拟监控?
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • ​比特币大跌的 2 个原因
  • "无招胜有招"nbsp;史上最全的互…
  • (007)XHTML文档之标题——h1~h6
  • (笔记)Kotlin——Android封装ViewBinding之二 优化
  • (补)B+树一些思想
  • (分布式缓存)Redis持久化
  • (附源码)springboot学生选课系统 毕业设计 612555
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (十五)devops持续集成开发——jenkins流水线构建策略配置及触发器的使用
  • (四)linux文件内容查看
  • (五)Python 垃圾回收机制
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转)Google的Objective-C编码规范
  • *** 2003
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET MVC之AOP
  • .Net Remoting(分离服务程序实现) - Part.3
  • .Net中的设计模式——Factory Method模式