代码片段:
Rect rct(25, 25, 20, 20);
Color clr1 = Color.Gray;
Color clr2 = Color.Yellow;
Color clr3 = Color.Black;
Pen p(clr3, 3);
GraphicsPath gPath1;
gPath1.AddLine(150, 50, 300, 50);
gPath1.AddLine(370, 270, 280, 340);
gPath1.AddLine(260, 280, 170, 400);
gPath1.AddArc(102, 230, 120, 120, 70, 180);
gPath1.CloseFigure();
LinearGradientBrush lBrh(rct, clr1, clr2, 30);
gp.FillPath(&lBrh, &gPath1);
gp.DrawPath(&p, &gPath1);
效果图:
2.晕线绘制问题
在地图制作中经常会遇到晕线绘制的问题,本以为GDI+的Pen的Pen::SetAlignment()(数可以传递一下五种枚举值:PenAlignmentCenter= 0, PenAlignmentInset= 1, PenAlignmentOutset= 2, PenAlignmentLeft= 3, PenAlignmentRigh= 4)调用可以解决晕线绘制问题,但是让我失望了。
首先,Pen::SetAlignment()函数调用后,只在Graphics.DrawPath()或Graphics.DrawRectangle()等绘制函数看到效果,也就是说只有在绘制闭合曲线时才能绘制内线或外线;
另外,调用Pen::SetAlignment(PenAlignmentOutset)不起作用,绘制的线没有偏移到外部,依然绘制在中部。
一下为一段测试代码:
Graphics gp(pDC->m_hDC);
gp.SetSmoothingMode(SmoothingModeHighQuality);
Pen p1(Color(Color.Silver), 30);
Pen p2(Color(Color.Red), 5);
Pen p3(Color(Color.Green), 15);
p1.SetLineJoin(LineJoinRound);
p2.SetLineJoin(LineJoinRound);
p3.SetLineJoin(LineJoinRound);
p1.SetAlignment(PenAlignmentOutset);
p2.SetAlignment(PenAlignmentCenter);
p3.SetAlignment(PenAlignmentInset);
Point pt[5] = {Point(100, 20), Point(170, 140), Point(250, 80),
Point(350, 200), Point(450, 30)};
GraphicsPath gPath;
gPath.AddPolygon(pt, 5);
gp.DrawPath(&p1, &gPath);
gp.DrawPath(&p2, &gPath);
gp.DrawPath(&p3, &gPath);
绘制 效果如下: