背景
图形类 Graphics 是 GDI+的核心,它提供绘制图形、图像和文本的各种方法。Graphics 中使用 DrawString 方法在指定位置绘制文本或者在一个指定矩形内绘制文本。
一个闭合图形比如矩形和椭圆包含一个边框和内部区域。边框是由 Pen 对象绘制,而内部区域由 Brush 对象进行填充。
纯色刷
①.概述
纯色刷用于使用单一颜色来填充形状,对应于 SolidBrush 类,构造函数的输入参数为颜色对象的引用。
②.纯色刷的使用
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
SolidBrush m_brush(Color::Red);//纯色画刷
graphics.DrawRectangle(&m_pen, 20, 20, 60, 80);
graphics.FillRectangle(&m_brush, 20, 20, 60, 80);
}
阴影刷
①.概述
阴影是一种重复填充的小方形图案,一般为横线、竖线、斜线和小方块等构成,阴影图案由两种颜色组成:一种是背景色,一种是在背景上形成图案的线条的颜色。对应于 HatchBrush 类。
②.阴影刷的使用
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
HatchBrush m_brush(HatchStyleForwardDiagonal, Color::Red,Color::Green);//阴影画刷
graphics.DrawRectangle(&m_pen, 20, 20, 60, 80);
graphics.FillRectangle(&m_brush, 20, 20, 60, 80);
}
纹理刷
①.概述
纹理刷就是图像刷,它将刷中所装入的图像在目标区域中进行填充绘制,对应于TextureBrush。纹理刷填充图像的排列方式是枚举常量:
typedef enum {
WrapModeTile = 0, // 平铺(瓦)(缺省值)
WrapModeTileFlipX = 1, // 平铺且 X 向翻转(相邻列左右翻转)
WrapModeTileFlipY = 2, // 平铺且 Y 向翻转(相邻行上下翻转)
WrapModeTileFlipXY = 3, // 平铺且 XY 向翻转(相邻行列左右上下翻转)
WrapModeClamp = 4 // 不平铺(不重复,夹住)
} WrapMode;
②.设置排列方式
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
Image image(L"textureBrush.png");
TextureBrush tBrush(&image, WrapModeTileFlipX);
graphics.DrawRectangle(&m_pen, 20, 20, 600,600);
graphics.FillRectangle(&tBrush, 20, 20, 600, 600);
}
③.设置图像平移
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
Image image(L"textureBrush.png");
TextureBrush tBrush(&image);
tBrush.TranslateTransform(30, 40);//平移
graphics.DrawRectangle(&m_pen, 20, 20, 600,600);
graphics.FillRectangle(&tBrush, 20, 20, 600, 600);
}
④.设置图像旋转
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
Image image(L"textureBrush.png");
TextureBrush tBrush(&image);
tBrush.RotateTransform(45);//旋转,单位是度
graphics.DrawRectangle(&m_pen, 20, 20, 600,600);
graphics.FillRectangle(&tBrush, 20, 20, 600, 600);
}
⑤.设置图像缩放
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
Image image(L"textureBrush.png");
TextureBrush tBrush(&image);
tBrush.ScaleTransform(0.5,0.5);//缩放
graphics.DrawRectangle(&m_pen, 20, 20, 600,600);
graphics.FillRectangle(&tBrush, 20, 20, 600, 600);
}
线性渐变刷
①.概述
线性渐变刷使用逐渐变化的颜色填充目标区域,对应于 LinearGradientBrush。通过指定起始和结束颜色,线性的填充指定区域。
②.点到点渐变
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
//水平渐变
{
Point p1(10, 10), p2(110, 10);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(p1, p2, col1, col2);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
//垂直渐变
{
graphics.TranslateTransform(110, 0);
Point p1(10, 10), p2(10, 70);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(p1, p2, col1, col2);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
//对角渐变
{
graphics.TranslateTransform(110, 0);
Point p1(10, 10), p2(110, 70);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(p1, p2, col1, col2);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
}
③.矩形渐变
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
//水平渐变
{
Rect m_rect(10, 10, 100, 60);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(m_rect, col1, col2, LinearGradientModeHorizontal);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
//垂直渐变
{
graphics.TranslateTransform(110, 0);
Rect m_rect(10, 10, 100, 60);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(m_rect, col1, col2, LinearGradientModeVertical);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
//对角渐变
{
graphics.TranslateTransform(110, 0);
Rect m_rect(10, 10, 100, 60);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(m_rect, col1, col2, LinearGradientModeForwardDiagonal);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
}
④.旋转角渐变
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Pen m_pen(Color::Black,4);
//旋转角渐变
{
Rect m_rect(10, 10, 100, 60);
Color col1(0, 255, 0), col2(255, 0, 0);
LinearGradientBrush lineBrush(m_rect, col1, col2, 30.0f,true);
graphics.DrawRectangle(&m_pen, 10, 10, 100, 60);
graphics.FillRectangle(&lineBrush, 10, 10, 100, 60);
}
}
⑤.多色渐变
void DemoGDI::DrawUser(HDC hdc)
{
Graphics graphics(hdc);//构造 Graphics 对象
Color cols[] = { Color::Red, Color::Orange, Color::Yellow, Color::Green,
Color::Cyan, Color::Blue, Color::Purple, Color::Magenta };
REAL bps[] = { 0.0f, 0.15f, 0.3f, 0.45f, 0.6f, 0.75f, 0.875f, 1.0f };
LinearGradientBrush brush(Point(10, 10), Point(510, 10), Color::Black,Color::White);
brush.SetInterpolationColors(cols, bps,8);
graphics.FillRectangle(&brush, Rect(10, 10, 500, 100));
}