Graphics graphics(dc.GetSafeHdc());
graphics.Clear(Color::White);
//定义三种参与渐变的色彩
Color colors[] =
{
Color::Red, //红色
Color::Green, //过渡色为绿色
Color::Blue //蓝色
};
//定义三种颜色的位置
float positions[] =
{
0.0f, // 由红色起
0.3f, // 绿色始于画刷长度的三分之一
1.0f // 到蓝色止
};
//构造一条从黑色到白色的渐变画刷
LinearGradientBrush linGrBrush(
Point(0, 0),
Point(180, 0),
Color::Black,Color::White);
//设置渐变画刷的多色渐变信息
linGrBrush.SetInterpolationColors(colors, positions, 3);
//使用多色渐变画刷填充目标区域
graphics.FillRectangle(&linGrBrush, 0, 0, 180, 100);
//使用普通的方法实现多色渐变
//由红到绿,长度60
LinearGradientBrush linGrBrush1(
Point(0, 0),
Point(60, 0),
Color::Red,
Color::Green);
//由绿到蓝,长度120
LinearGradientBrush linGrBrush2(
Point(60, 0),
Point(181, 0),
Color::Green,
Color::Blue);
//分别使用两个画刷填充两个相邻区域,形成多色渐变
graphics.FillRectangle(&linGrBrush1, 0, 120, 60, 100);
graphics.FillRectangle(&linGrBrush2, 60, 120, 120, 100);
例2:
//定义一个线性渐变画刷,沿直线方向渐变
LinearGradientBrush linGrBrush(Point(100, 0), Point(100, rc2.Height() / 2), Color(255, 255, 0, 0), Color(255, 0, 0, 255));
Color colors[] = {
Color(255, 255, 0, 0), //red
Color(255, 255, 255, 0), //yellow
Color(255, 0, 0, 255), //blue
Color(255, 0, 255, 0) //green
};
//按红黄蓝绿的顺序四种颜色渐变,四种颜色,各占四分之一
REAL positions[] = {
0.0f,
0.33f,
0.66f,
1.0f
};
//设置插补颜色(插值法)
linGrBrush.SetInterpolationColors(colors, positions, 4);
//填充指定区域矩形
Gdiplus::Graphics graphics(pDC->m_hDC);;
graphics.FillRectangle(&linGrBrush, rc2.Width() / 2, 0, 80, rc2.Height() / 2);
//设置字体绘制文本
Gdiplus::Pen pen(Gdiplus::Color(255, 0, 0, 255));
Gdiplus::SolidBrush brush(Gdiplus::Color(255, 0, 0, 255)); //设置颜色和透明(0完全透明)/R/G/B
Gdiplus::FontFamily family(L"微软雅黑"); //设置字体 FontFamily fontFamily(L"Arial");
Gdiplus::Font font(&family, 24, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel); //FontStyleBold:粗体;FontStyleItalic:斜体;FontStyleUnderline:下划线
CRect rt;
GetClientRect(&rt);
Gdiplus::PointF pointF(rt.Width() / 2, rt.Height() / 2);
graphics.DrawString(L"GDI+程序示意", -1, &font, pointF, &brush);