GDI+ 探索
GDI+接口是Microsoft Whistler操作系统中的一部分,它是GDI的一个新版本,不仅在GDI基础上添加许多新特性而且对原有的GDI功能进行优化。
在为开发人员提供的二维矢量图形、文本、图像处理、区域、路径以及图形数据矩阵等方面构造了一系列相关的类,如Bitmap(位图类)、Brush(画
刷类)、Color(颜色类)、Font(字体类)、Graphics(图形类)、Image(图像类)、Pen(画笔类)和Region(区域类)等。其中,图形类Graphics是GDI
+接口中的一个核心类,许多绘图操作都可用它来完成。
1. VS中使用GDI+的配置步骤:
#include <gdiplus.h>
#pragma comment( lib, "gdiplus.lib" ) //添加lib文件
using namespace Gdiplus;
添加一个成员变量:ULONG_PTR m_gdiplusToken;
初始化GDI+:
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
关闭GDI+:
Gdiplus::GdiplusShutdown(m_gdiplusToken);
2. 画笔类
Pen类,想想画笔能有什么功能,即方法:SetDashStyle和SetDashPattern方法来设置画笔的预定义风格和自定义类型
3. 画刷和渐变
画刷用于指定填充的特性,GDI+为填充色和阴影线画刷提供了SolidBrush和HatchBrush类。
GDI+提供了LinearGradientBrush和PathGradientBrush类分别用来创建一个直线渐变和路径渐变画刷。
View Code
2
3 GraphicsPath path; // 构造一个路径
4 path.AddEllipse(50, 50, 200, 100);
5
6 // 使用路径构造一个画刷
7 PathGradientBrush pthGrBrush(&path);
8
9 // 将路径中心颜色设为蓝色
10 pthGrBrush.SetCenterColor(Color(255, 0, 0, 255));
11
12 // 设置路径周围的颜色为蓝芭,但alpha值为0
13 Color colors[] = {Color(0, 0, 0, 255)};
14 INT count = 1;
15 pthGrBrush.SetSurroundColors(colors, &count);
16
17 graphics.FillRectangle(&pthGrBrush, 50, 50, 200, 100);
18
19 LinearGradientBrush linGrBrush(
20 Point(300, 50),
21 Point(500, 150),
22 Color(255, 255, 0, 0), // 红色
23 Color(255, 0, 0, 255)); // 蓝色
24
25 graphics.FillRectangle(&linGrBrush, 300, 50, 200, 100);
画笔和画刷还可使用一个图片来创建。例如下列代码。
View Code
Image image(L"image.jpg");
TextureBrush tBrush(&image);
Pen texturedPen(&tBrush, 10);
graphics.DrawLine(&texturedPen, 25, 25, 325, 25);
tBrush.SetWrapMode(WrapModeTileFlipXY);
graphics.FillRectangle(&tBrush, 25, 100, 300, 200);
4. 基本绘图函数
| 绘图函数 | 功能描述 |
| DrawArc | 绘制一条圆弧曲线,范围由起止角大小决定,大小由矩形或长宽值指定 |
| DrawBezier | 绘制一条由一系列型值顶点决定的三次Bezier曲线 |
| DrawBeziers | 绘制一系列的三次Bezier曲线 |
| DrawClosedCurve | 绘制一条封闭的样条曲线 |
| DrawCurve | 绘制一条样条曲线 |
| DrawEllipse | 绘制一条椭圆轮廓线,大小由矩形或长宽值指定 |
| DrawLine | 绘制一条直线 |
| DrawPath | 绘制由GraphicsPath定义的路径轮廓线 |
| DrawPie | 绘制一条扇形(饼形)轮廓线 |
| DrawPolygon | 绘制一个多边形的轮廓线 |
| DrawRectangle | 绘制一个矩形 |
| FillEllipse | 填充一个椭圆区域 |
| FillPath | 填充一个由路径指定的区域 |
| FillPie | 填充一个扇形(饼形)区域 |
| FillPolygon | 填充一个多边形区域 |
| FillRectangle | 填充一个矩形区域 |
| FillRectangles | 用同一个画刷填充一系列矩形区域 |
| FillRegion | 填充一个区域(Region)的内部 |


浙公网安备 33010602011771号