GDI+学习之------ 画线、区域填充、写字
《精通GDI编程》里的代码。在学习过程中对它加以总结,以防以后用到,全部代码都是在MFC 单文档中实现的,写在View::OnDraw(CDC */*pDC*/)中
画线/边框(Pen)
1、画单线-------DrawLine
- Pen pen(Color(255,0,0,0),3);
- PointF L_PTStart(0,0);
- PointF L_PTEnd(100,10);
- graphics.DrawLine(&pen,L_PTStart,L_PTEnd);
2、连接线--------DrawLines
- Pen blackPen(Color(255, 0, 0, 0), 3);
- PointF point1(10.0f, 10.0f);
- PointF point2(10.0f, 100.0f);
- PointF point3(200.0f, 50.0f);
- PointF point4(250.0f, 80.0f);
- PointF points[4] = {point1, point2, point3, point4};
- PointF* pPoints = points;
- graphics.DrawLines(&blackPen, pPoints, 4);
解说:points数组中的每一个点都是连接线上的转折点,DrawLines会把它们依照顺序一个个连接起来
3、画矩形-----DrawRectangle,仅仅画边框。不画背景色
- Pen blackPen(Color(255,255, 0, 0), 3);
- Rect rect(0, 0, 100, 100);
- graphics.DrawRectangle(&blackPen, rect);
4、一次画多个矩形----DrawRectangles
- Pen blackPen(Color(255, 0, 255, 0), 3);
- // 定义三个矩形
- RectF rect1(0.0f, 0.0f, 50.0f, 60.0f);
- RectF rect2(60.0f, 70.0f, 70.0f, 100.0f);
- RectF rect3(100.0f, 0.0f, 50.0f, 50.0f);
- RectF rects[] = {rect1, rect2, rect3};
- //RectF是对Rect的封装
- graphics.DrawRectangles(&blackPen, rects, 3);
5、画曲线-----DrawCurve
- Pen greenPen(Color::Green, 3);
- PointF point1(100.0f, 100.0f);
- PointF point2(200.0f, 50.0f);
- PointF point3(400.0f, 10.0f);
- PointF point4(500.0f, 100.0f);
- PointF curvePoints[4] = {
- point1,
- point2,
- point3,
- point4};
- PointF* pcurvePoints = curvePoints;
- // 画曲线
- graphics.DrawCurve(&greenPen, curvePoints, 4);
- //画连接点和直线连接线
- SolidBrush redBrush(Color::Red);
- graphics.FillEllipse(&redBrush, Rect(95, 95, 10, 10));//画连接点
- graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));
- Pen redPen(Color::Red, 2);
- graphics.DrawLines(&redPen,curvePoints,4);//画连接线
注意:这里为了比較画曲线与画直线连接线的差别。我用绿色画的曲线,用红色画的直线连接线。同一时候画出了连接点,大家能够比較一下。
6、画闭合曲线
- Pen greenPen(Color::Green, 3);
- PointF point1(100.0f, 100.0f);//開始点
- PointF point2(200.0f, 50.0f);
- PointF point3(400.0f, 10.0f);
- PointF point4(500.0f, 100.0f);
- PointF point5(600.0f, 200.0f);
- PointF point6(700.0f, 400.0f);
- PointF point7(500.0f, 500.0f);//结束点
- PointF curvePoints[7] = {
- point1,
- point2,
- point3,
- point4,
- point5,
- point6,
- point7};
- PointF* pcurvePoints = curvePoints;
- //画闭合曲线
- graphics.DrawClosedCurve(&greenPen, curvePoints, 7);
- //画连接点
- SolidBrush redBrush(Color::Red);
- SolidBrush startBrush(Color::Blue);
- SolidBrush endBrush(Color::Black);
- graphics.FillEllipse(&startBrush, Rect(95, 95, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(495, 95, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(195, 45, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(395, 5, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(595, 195, 10, 10));
- graphics.FillEllipse(&redBrush, Rect(695, 395, 10, 10));
- graphics.FillEllipse(&endBrush, Rect(495, 495, 10, 10));
注意:蓝色点是開始点,黑色点是结束点
7、画多边形-----DrawPolygon,既然能画闭合的曲线。肯定也有闭合的直线。当然闭合的直线也就是所谓的多边形
- Pen blackPen(Color(255, 0, 0, 0), 3);
- //创建点数组,DrawPolygon会按这些点的顺序逐个连接起来
- PointF point1(100.0f, 100.0f);
- PointF point2(200.0f, 130.0f);
- PointF point3(150.0f, 200.0f);
- PointF point4(50.0f, 200.0f);
- PointF point5(0.0f, 130.0f);
- PointF points[5] = {point1, point2, point3, point4, point5};
- PointF* pPoints = points;
- // 画多边形,也就是闭合直线
- graphics.DrawPolygon(&blackPen, pPoints, 5);
8、画弧线----DrawArc
- Pen redPen(Color::Red, 3);
- RectF ellipseRect(0, 0, 200, 100);
- REAL startAngle = 0.0f;//起始度数
- REAL sweepAngle = 90.0f;//结尾时的度数
- // 画弧线
- graphics.DrawArc(&redPen, ellipseRect, startAngle, sweepAngle);
- //画出边框,做为參考
- Pen greenPen(Color::Green, 1);
- graphics.DrawRectangle(&greenPen,ellipseRect);
9、画扇形----DrawPie
- Pen blackPen(Color(255, 0, 255, 0), 3);
- // 定义椭圆。然后在里面截一部分作为终于的扇形
- RectF ellipseRect(0, 0, 200, 100);
- REAL startAngle = 40.0f;
- REAL sweepAngle = 100.0f;
- //画扇形
- graphics.DrawPie(&blackPen, ellipseRect, startAngle, sweepAngle);
先出效果图:
这里要对它两上名词解说一下,什么叫startAngle(開始度数),什么叫sweepAngle(范围度数也能叫扫过度数,我译的。嘿嘿)
看下MSDN里对DrawPie函数的解说就会懂了,里面有这个图,给大家看一下
填充区域(SolidBrush)
1、填充闭合区域----FillClosedCurve,边框相应:DrawClosedCurve
- SolidBrush blackBrush(Color(255, 0, 0, 0));
- PointF point1(100.0f, 100.0f);
- PointF point2(200.0f, 50.0f);
- PointF point3(250.0f, 200.0f);
- PointF point4(50.0f, 150.0f);
- PointF points[4] = {point1, point2, point3, point4};
- //填充闭合区域
- graphics.FillClosedCurve(&blackBrush, points, 4);
- //为闭合区域画边框
- Pen curPen(Color::Green,3);
- graphics.DrawClosedCurve(&curPen,points,4);
注意:从结果图中也能够看出填充区域(背景)和边框是分离的,用FillClosedCurve来填充背景色,用DrawClosedCurve来画边框
2、填充椭圆---FillEllipse。边框相应:DrawEllipse
- SolidBrush blackBrush(Color(255, 0, 0, 0));
- RectF ellipseRect(0.0f, 0.6f, 200.8f, 100.9f);
- //填充椭圆
- graphics.FillEllipse(&blackBrush, ellipseRect);
- //画边框,当然也能够不画
- Pen borderPen(Color::Green,3);
- graphics.DrawEllipse(&borderPen,ellipseRect);
还有类似的几个函数。这里就不一 一解说了
它们是:
- FillPie(Brush* brush, RectF& rect, REAL startAngle, REAL sweepAngle) //填充扇形,相应DrawPie
- FillPolygon(Brush* brush, PointF* points, INT count) //填充多边形。相应DrawPolygon
- FillRectangle(Brush* brush, RectF& rect) //填充矩形,相应DrawRectangle
- FillRectangles(Brush* brush, RectF* rects, INT count) //同一时候填充多个矩形。相应DrawRectangles
还有是关于路径和区域的,先记下,后面再说
- Status FillPath( const Brush* brush, const GraphicsPath*path);
- Status FillRegion( const Brush* brush, const Region*region);
写字(SolidBrush)
形式一:Status DrawString( const WCHAR*string, INTlength, const Font* font,
const PointF&origin, const Brush*brush);
- Graphics graphics(this->GetDC()->m_hDC);
- SolidBrush brush(Color(255,0,0,255));
- FontFamily fontfamily(L"宋体");
- Font font(&fontfamily,24,FontStyleRegular,UnitPixel);
- PointF pointf(0,0);//PointF类对点进行了封装。这里是指定写字的開始点
- graphics.DrawString(L"GDI写字",-1,&font,pointf,&brush);
- //DrawString还有另外两个重载形式,能实现更强大的功能
形式二:Status DrawString( const WCHAR*string, INT length, const Font*font,
const RectF&layoutRect, const StringFormat*stringFormat, const Brush*brush);
- WCHAR string[256];
- wcscpy(string, L"Sample Text");
- // Initialize arguments.
- Font myFont(L"Arial", 16);//字体
- RectF layoutRect(0.0f, 0.0f, 200.0f, 50.0f);//矩形
- //设定字体格式
- StringFormat format;
- format.SetAlignment(StringAlignmentCenter); //水平方向的对齐方式,这里设置为水平居中
- format.SetLineAlignment(StringAlignmentFar);//垂直方向的对齐方式,这里设置为垂直居下
- SolidBrush blackBrush(Color(255, 0, 0, 0));
- //画矩形边框
- graphics.DrawRectangle(&Pen(Color::Green, 3), layoutRect);
- //填充矩形背景
- graphics.FillRectangle(&SolidBrush(Color(255,255,0,0)),layoutRect);
- //DrawString,一定要先画背景再写字,要不然,字会被背景覆盖
- graphics.DrawString(
- string,
- wcslen(string),
- &myFont,
- layoutRect,
- &format,
- &blackBrush);
形式三:Status DrawString( const WCHAR*string, INTlength, const Font* font,
const PointF&origin, const StringFormat*stringFormat, const Brush* brush);
这样的形式是形式一与形式二的结合,指定写字開始点和字体格式,这里就不举例了。
GDI+学习之------ 画线、区域填充、写字的更多相关文章
- .NET CAD二次开发学习 对称画线功能
[CommandMethod("CBline")] //对称画线 public void CBline() { Document doc = Application.Documen ...
- webgl学习总结画线面及场景和物体动
WebGL是在浏览器中实现三维效果的一套规范.是浏览器中的3D引擎,是利用js代码来实现加载3D模型,渲染.输出等功能,从而实现在浏览器和微信中浏览三维文件的效果. three.js是基于WebGL的 ...
- openCV 和GDI画线效率对照
一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大 ...
- openCV 和GDI画线效率对比
一. 由于项目需要,原来用GDI做的画线的功能,新的项目中考虑到垮平台的问题,打算用openCV来实现,故此做个效率对比. 二. 2点做一条线,来测试效率. 用了同样的画板大小---256*256的大 ...
- MFC画线功能总结
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6216464.html MFC画线功能要点有二:其一,鼠标按下时记录初始位置为线的起始 ...
- MFC消息映射机制以及画线功能实现
---此仅供用于学习交流,切勿用于商业用途,转载请注明http://www.cnblogs.com/mxbs/p/6213404.html. 利用VS2010创建一个单文档标准MFC工程,工程名为Dr ...
- C#使用 DirectX SDK 9做视频播放器 并在视频画线添加文字 VMR9
视频图像处理系列 索引 VS2013下测试通过. 在百度中搜索关键字“DirectX SDk”,或者进入微软官网https://www.microsoft.com/en-us/download/det ...
- win32画线考虑去锯齿
整理日: 2015年2月16日 这几天一直在研究win32 SDk下画线去锯齿,之前一直用的QT的画线接口函数,里面有去锯齿的效果,可是突然项目要求不能用QT的只能用win32 SDK下的GDI画线接 ...
- GDI+学习---2.GDI+编程模式及组成类
在使用GDI+的时候,您不必像在GDI中那样关心设备场景句柄,只需简单地创建一个Graphics对象,然后以您熟悉的面向对象的方式(如myGraphicsObject.DrawLine(paramet ...
随机推荐
- wireshark mqtt协议解析
local tcp_dissector_table = DissectorTable.get("tcp.port") local ws_dissector_table = Diss ...
- JS中二进制与十进制的相互转换
今天在做题目的时候遇到了需要十进制转换为二进制,这个我知道用toString可以,但是二进制转换为十进制我一下子就想不起来,网上搜了下,才知道是parseInt可以实现,特此记录下. 十进制转换为二进 ...
- mitmproxy 数据抓包
1.安装环境: 基于python windows操作系统需要安装Microsoft Visual C++ V14.0以上 linux操作系统则直接基于python安装即可 2.安装mitmproxy ...
- DNS服务器原理简述、搭建主/从DNS服务器并实现智能解析
1. TLD:Top Level Domain 顶级域名 组织域:.com, .net, .org, .gov, .edu, .mil 国家域:.iq, .tw, .hk, .jp, .cn, ... ...
- 【Jsp,Servlet初学总结】 含 cookie和session的使用
一.Jsp 1. 指令: <%@ page language="java" import="java.*" contextType="text/ ...
- python 多线程并发threading & 任务队列Queue
https://docs.python.org/3.7/library/concurrency.htmlpython程序默认是单线程的,也就是说在前一句语句执行完之前后面的语句不能继续执行先感受一下线 ...
- python链家网高并发异步爬虫and异步存入数据
python链家网二手房异步IO爬虫,使用asyncio.aiohttp和aiomysql 很多小伙伴初学python时都会学习到爬虫,刚入门时会使用requests.urllib这些同步的库进行单线 ...
- Matlab学习笔记(五)
三.矩阵运算 (一)矩阵函数和特殊矩阵 常见的矩阵处理函数 表3-1 常见的矩阵函数 函数 说明 /或\ 矩阵除法中的左除或右除,可以用于求解线性方程组 accumarray(ind,val) ...
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 关于SELECT 逻辑的执行顺序问题
不会有大多数人都和我一样的认为,是先进行的Where 剔除结果集,再进行Join的吧 SQL server 2014 逻辑执行标准: https://msdn.microsoft.com/en-us/ ...