GDI+中的线型,大多数与GDI中的相同,区别主要有两点:
1>GDI中的非实线线型,对宽度>1的笔无效;而GDI+的笔对任意非零宽度的笔都是有效的;
2>GDI+中新增了一种风格——自定义虚线风格。
具体的自定义虚线风格,由Pen类的设置虚线图案的成员函数
Status SetDashPattern(const REAL *dashArray, INT count);
来设置,其中的实数数组dashArray含若干个正实数(单位为像素),按线、空、线、空、……的交叉方式排列;参数count为数组中实数的个数(须>0)。
例如:
Graphics graph(pDC->m_hDC);
Pen pen(Color::Black, 8); // 创建宽8个像素的黑色笔(画虚线用)
REAL dashVals[4] = {5.0f, 2.0f, 15.0f, 4.0f}; // 线5、空2、线15、空4(像素)
FontFamily fontFamily(L"Times New Roman"); // 创建字体族对象
Font font(&fontFamily, 10.5); // 创建5号字大小的Times New Roman字体
SolidBrush brush(Color(0, 128, 0)); // 创建绿色的实心刷(写字符串用)
// 笔的虚线风格枚举常量的名称字符串数组
CString strs[] = {L"DashStyleSolid", L"DashStyleDash", L"DashStyleDot",
L"DashStyleDashDot", L"DashStyleDashDotDot", L"DashStyleCustom"};
for (int i = 0; i <= 5; i++)
{ // 绘制各种风格的虚线及其名称串
pen.SetDashStyle((DashStyle)i); // 设置笔的虚线风格
if (i == 5)
{
pen.SetDashPattern(dashVals, 4); // 设置自定义虚线图案
}
graph.DrawLine(&pen, 10, 10 + i * 20, 400, 10 + i * 20); // 画虚线
// 绘制虚线风格枚举常量名称字符串
graph.DrawString(strs[i], -1, &font, PointF(410, 2 + i * 20), &brush);
}
转载于:https://blog.51cto.com/qsjming/447067