GDI+学习及代码总结之-----画笔

画笔

一、构造函数

画笔有两个构造函数,分别看下:

[cpp]  view plain  copy
  1. Pen( const Color& color, REAL width);  
  2.   
  3. Pen( const Brush* brush, REAL width);  

注意:第一个构造函数:从一个颜色构造一个单色画笔,平时用的比较多

            第二个构造函数要特别注意,它可以从一个画刷构造一个画笔,当画刷是纯色时,构造的也是纯色画笔,这与第一个构造函数效果相同,但画刷并不一定都是纯色的,如果是纹理画刷的话,构造出来的画笔会是什么样呢,我们看看

例子:(同样用力宏哥哥的照片)

[cpp]  view plain  copy
  1. Image image(L"img.bmp");  
  2. TextureBrush tBrush(&image);  
  3. Pen pen(&tBrush,40);  
  4. //在三个不同区域画椭圆,看实现效果  
  5. //试探在同一张图中画椭圆会怎样  
  6. graphics.DrawEllipse(&pen,100,20,200,100);  
  7. graphics.DrawEllipse(&pen,20,150,200,100);  
  8. //试探超出会怎样  
  9. graphics.DrawEllipse(&pen,400,20,200,100);//水平超出  
  10. graphics.DrawEllipse(&pen,20,450,200,100);//垂直超出  

王力宏的照片:程序运行效果:

从运行结果上可以看出,背景图会在绘制区域横向和纵向平铺,然后根据画笔的位置,将其显示!

二、设置画笔样式-----Pen::SetDashStyle(dashStyle) 

DashStyle定义了下面几种线型:

[cpp]  view plain  copy
  1. enum DashStyle{  
  2.   DashStyleSolid = 0,   //实线  
  3.   DashStyleDash = 1,  //虚线  
  4.   DashStyleDot = 2, //点线  
  5.   DashStyleDashDot = 3, //点划线  
  6.   DashStyleDashDotDot = 4, //双点划线  
  7.   DashStyleCustom = 5 //自定义线型  
  8. };  

示例:

[cpp]  view plain  copy
  1. Pen pen(Color(255, 0, 0, 255), 15);  
  2.   
  3. pen.SetDashStyle(DashStyleDash);//虚线  
  4. graphics.DrawLine(&pen, 0, 50, 400, 150);  
  5.   
  6. pen.SetDashStyle(DashStyleDot);//点线  
  7. graphics.DrawLine(&pen, 0, 80, 400, 180);   
  8.   
  9. pen.SetDashStyle(DashStyleDashDot);//点划线  
  10. graphics.DrawLine(&pen, 0, 110, 400, 210);   

三、自定义画笔样式-----Pen::SetDashPattern(dashArray, count)

Pen类的成员函数SetDashPattern(设置线型风格)可以使用一个预定义的数组来描述画笔的虚实,这个数组的格式为:

[画线部分长度,间隔部分长度,画线部分长度,间隔部分长度……]

数组的大小由开发者自定义,画笔会按照自定义的格式循环画线

例子:

[cpp]  view plain  copy
  1. //定义线型数组  
  2. REAL dashVals[4] = {  
  3.     5.0f,   //线长5个像素  
  4.     2.0f,   //间隔2个像素  
  5.     15.0f,  //线长15个像素  
  6.     4.0f};  //间隔4个像素  
  7.   
  8.     Pen pen(Color(255, 0, 0, 0), 5);  
  9.     //设置线型  
  10.     pen.SetDashPattern(dashVals, 4);  
  11.     graphics.DrawLine(&pen, 5, 20, 405, 200);   

四、设置画笔对齐方式-----Pen::SetAlignment(penAlignment)

penAlignment枚举了两种对齐方式:

[cpp]  view plain  copy
  1. enum PenAlignment{  
  2.   PenAlignmentCenter = 0,//居中对齐,默认  
  3.   PenAlignmentInset  = 1,//嵌入方式  
  4. };  

例子:

[cpp]  view plain  copy
  1. Pen greenPen(Color(255, 0, 255, 0), 15);  
  2. Pen BorderPen(Color(100,0,0,0),1);//透明度设为100  
  3.   
  4. // 设置成嵌入式  
  5. greenPen.SetAlignment(PenAlignmentInset);  
  6. graphics.DrawEllipse(&greenPen, 10,20, 100, 200);  
  7. graphics.DrawRectangle(&BorderPen,10,20,100,200);  
  8. //设置成居中  
  9. greenPen.SetAlignment(PenAlignmentCenter );  
  10. graphics.DrawEllipse(&greenPen, 130, 20, 100, 200);  
  11. graphics.DrawRectangle(&BorderPen,130,20,100,200);  

五、画笔的缩放与旋转(待完)

注意,这是对画笔的缩放与旋转,在画笔缩放或旋转后,用这个画笔画的图像都是缩放/旋转的,这里并不是对图像缩放/旋转,这点一定要清楚。

缩放

几个缩放函数:

[cpp]  view plain  copy
  1. Pen::ScaleTransform(sx, sy, order)//方法一,sx指定水平缩放倍数,sy指定纵向缩放倍数  
  2.   
  3. Pen::SetTransform(matrix)//方法二,  
  4.   
  5. Pen::MultiplyTransform(matrix, order)//方法三,  
  6.   
  7. Pen::ResetTransform() //重置缩放参数,即把缩放参数全部去掉,还原到原来的1:1状态  

后面两个函数还不会用,方法一比较简单,举个例子吧:

[cpp]  view plain  copy
  1. //缩放一-----ScaleTransform  
  2. //放大之前画个图,放大之后再画个图,对比一下  
  3. Pen pen(Color(255, 0, 0, 255), 2);  
  4. graphics.DrawRectangle(&pen, 50, 50, 150, 100);  
  5.   
  6. // 水平放大8倍,垂直放大4倍  
  7. pen.ScaleTransform(8, 4);  
  8. graphics.DrawRectangle(&pen, 250, 50, 150, 100);  

关于重置Pen::ResetTransform(),贴段代码吧

[cpp]  view plain  copy
  1. Pen pen(Color(255, 0, 0, 255), 2);  
  2. // 水平放大8倍,垂直放大4倍  
  3. pen.ScaleTransform(8, 4);  
  4. graphics.DrawRectangle(&pen, 250, 50, 150, 100);  
  5.   
  6. pen.ResetTransform();  
  7. graphics.DrawRectangle(&pen, 450, 50, 150, 100);  

旋转

旋转函数:
[cpp]  view plain  copy
  1. Pen::RotateTransform(angle, order)//第一个参数表示旋转度数,第二个参数表示往左旋转还是往右旋转  

例子:

[cpp]  view plain  copy
  1. //画笔要旋转,粗细要大于1,否则应用旋转函数不会成功  
  2. Pen pen(Color::Green,5);  
  3. pen.ScaleTransform(1,6);  
  4. graphics.DrawEllipse(&pen,20,20,100,100);  
  5.   
  6. //往右旋转60度  
  7. pen.RotateTransform(60,MatrixOrderAppend );  
  8. graphics.DrawEllipse(&pen,150,20,100,100);  
  9.   
  10. //再往右旋转60度,也就是相对于原来的旋转了120度  
  11. pen.RotateTransform(60,MatrixOrderAppend );  
  12. graphics.DrawEllipse(&pen,300,20,100,100);  
  13. //注意:RotateTransform按说设置成MatrixOrderPrepend会往左旋转,但我设置成它之后,总是不成功的,不知为什么  

六、设置系统线帽

“线帽”,顾名思义,就是线条首尾的外观,默认情况下,使用画笔绘制的直线,其起点和结束点的外观都是相同的——方形。在程序设计中,我们可以对这两个端点的外观进行修改。对于起点,可以使用SetStartCap,对于终点,可以使用SetEndCap来完成。

看这两个函数:

[cpp]  view plain  copy
  1. Pen::SetStartCap(startCap)//设置起点线帽  
  2.   
  3. Pen::SetEndCap(endCap)//设置终点线帽  

线帽是由LineCap枚举列出的。LineCap定义如下:

[cpp]  view plain  copy
  1. enum LineCap{  
  2.   LineCapFlat     = 0,  
  3.   LineCapSquare   = 1,  
  4.   LineCapRound    = 2,  
  5.   LineCapTriangle = 3,  
  6.   LineCapNoAnchor = 0x10,  
  7.   LineCapSquareAnchor  = 0x11,  
  8.   LineCapRoundAnchor   = 0x12,  
  9.   LineCapDiamondAnchor = 0x13,  
  10.   LineCapArrowAnchor   = 0x14,  
  11.   LineCapCustom        = 0xff  
  12. };  
对于它们各是什么线帽,看例子:(对于每一种线帽都一一把它画了出来,并在其后标上该线帽的名字,大家可以比对一下)

[cpp]  view plain  copy
  1. Pen pen(Color::Green,15);  
  2. SolidBrush brush(Color(255,255,0,0));  
  3. FontFamily fontfamily(L"黑体");  
  4. Font font(&fontfamily,16,FontStyleRegular,UnitPixel);  
  5.   
  6. pen.SetStartCap(LineCapFlat);  
  7. pen.SetEndCap(LineCapFlat);  
  8. graphics.DrawLine(&pen,PointF(30,20),PointF(400,20));  
  9. graphics.DrawString(L"LineCapFlat",-1,&font,PointF(430,10),&brush);  
  10.   
  11. pen.SetStartCap(LineCapSquare);  
  12. pen.SetEndCap(LineCapSquare);  
  13. graphics.DrawLine(&pen,PointF(30,50),PointF(400,50));  
  14. graphics.DrawString(L"LineCapSquare",-1,&font,PointF(430,40),&brush);  
  15.   
  16. pen.SetStartCap(LineCapRound);  
  17. pen.SetEndCap(LineCapRound);  
  18. graphics.DrawLine(&pen,PointF(30,80),PointF(400,80));  
  19. graphics.DrawString(L"LineCapRound",-1,&font,PointF(430,70),&brush);  
  20.   
  21. pen.SetStartCap(LineCapTriangle);  
  22. pen.SetEndCap(LineCapTriangle);  
  23. graphics.DrawLine(&pen,PointF(30,110),PointF(400,110));  
  24. graphics.DrawString(L"LineCapTriangle",-1,&font,PointF(430,100),&brush);  
  25.   
  26. pen.SetStartCap(LineCapNoAnchor);  
  27. pen.SetEndCap(LineCapNoAnchor);  
  28. graphics.DrawLine(&pen,PointF(30,140),PointF(400,140));  
  29. graphics.DrawString(L"LineCapNoAnchor",-1,&font,PointF(430,130),&brush);  
  30.   
  31. pen.SetStartCap(LineCapSquareAnchor);  
  32. pen.SetEndCap(LineCapSquareAnchor);  
  33. graphics.DrawLine(&pen,PointF(30,170),PointF(400,170));  
  34. graphics.DrawString(L"LineCapSquareAnchor",-1,&font,PointF(430,160),&brush);  
  35.   
  36. pen.SetStartCap(LineCapRoundAnchor);  
  37. pen.SetEndCap(LineCapRoundAnchor);  
  38. graphics.DrawLine(&pen,PointF(30,200),PointF(400,200));  
  39. graphics.DrawString(L"LineCapRoundAnchor",-1,&font,PointF(430,190),&brush);  
  40.   
  41. pen.SetStartCap(LineCapDiamondAnchor);  
  42. pen.SetEndCap(LineCapDiamondAnchor);  
  43. graphics.DrawLine(&pen,PointF(30,230),PointF(400,230));  
  44. graphics.DrawString(L"LineCapDiamondAnchor",-1,&font,PointF(430,220),&brush);  
  45.   
  46. pen.SetStartCap(LineCapArrowAnchor);  
  47. pen.SetEndCap(LineCapArrowAnchor);  
  48. graphics.DrawLine(&pen,PointF(30,260),PointF(400,260));  
  49. graphics.DrawString(L"LineCapArrowAnchor",-1,&font,PointF(430,250),&brush);  
  50.   
  51. pen.SetStartCap(LineCapCustom);  
  52. pen.SetEndCap(LineCapCustom);  
  53. graphics.DrawLine(&pen,PointF(30,290),PointF(400,290));  
  54. graphics.DrawString(L"LineCapCustom",-1,&font,PointF(430,280),&brush);  


七、自定义线帽

GDI+使用CustomLineCap类来自定义线帽,该类的构造函数为:

[cpp]  view plain  copy
  1. CustomLineCap( const GraphicsPath* fillPath, const GraphicsPath* strokePath, LineCap baseCap, REAL baseInset);  
在上述的构造函数中,我们可以从一个路径创建自定义的线帽。使用SetCustomStartCap和SetCustomEndCap将由CustomLineCap函数创建的自定义的线帽与画笔相关联。
示例:

[cpp]  view plain  copy
  1. //自定义线帽  
  2. GraphicsPath StartPath,EndPath;  
  3. //构造开始点线帽路径:矩形  
  4. StartPath.AddRectangle(Rect(-10,-5,20,10));  
  5. //构造结束点线帽路径:箭头  
  6. EndPath.AddLine(0,-20,10,0);  
  7. EndPath.AddLine(0,-20,-10,0);  
  8. EndPath.AddLine(0,-10,10,0);  
  9. EndPath.AddLine(0,-10,-10,0);  
  10.   
  11. Pen pen(Color(255,0,0,255),2);  
  12.   
  13. CustomLineCap startCap(NULL,&StartPath);  
  14. CustomLineCap endCap(NULL,&EndPath);  
  15. //将线帽与画笔相关联  
  16. pen.SetCustomStartCap(&startCap);  
  17. pen.SetCustomEndCap(&endCap);  
  18.   
  19. //先画条试验线  
  20. graphics.DrawLine(&pen,20,30,300,30);//水平的  
  21. graphics.DrawLine(&pen,100,50,80,200);//斜的  

八、设置直线连接点属性(设置转角)

直线连接点是两条端点重合或重叠的直线形成的公共区域。GDI+提供了四种直线连接方式:斜接(LineJoinMiter)、斜切(LineJoinBevel)、圆形(LineJoinRound)、剪裁斜接(LineJoinMiterClipped)。

LineJoin枚举了常见的连接方式,其定义是:

[cpp]  view plain  copy
  1. enum LineJoin{  
  2.   LineJoinMiter = 0, //斜接  
  3.   LineJoinBevel = 1, //斜切  
  4.   LineJoinRound = 2,  //圆形  
  5.   LineJoinMiterClipped =3  //剪裁  
  6. };  
看一下连接点示例吧:(先看距形中的应用,再看再条直线连接时的转角)

[cpp]  view plain  copy
  1. //为了强化效果,特地把直线设的特别粗   
  2. Pen pen(Color::Green,25);  
  3. FontFamily fontfamily(L"宋体");  
  4. Font font(&fontfamily,18,FontStyleRegular,UnitPixel);  
  5. SolidBrush brush(Color(255,255,0,0));  
  6. //先看看矩形的边效果  
  7. pen.SetLineJoin(LineJoinMiter);//设定为斜接  
  8. graphics.DrawRectangle(&pen,20,20,150,100);  
  9.   
  10. pen.SetLineJoin(LineJoinBevel);//设定为斜切  
  11. graphics.DrawRectangle(&pen,220,20,150,100);  
  12.   
  13. pen.SetLineJoin(LineJoinRound );//设定连接方式为圆形  
  14. graphics.DrawRectangle(&pen,420,20,150,100);  
  15.   
  16. pen.SetLineJoin(LineJoinMiterClipped);//设定为剪裁斜接  
  17. graphics.DrawRectangle(&pen,620,20,150,100);  
  18.   
  19. //再看直线的连接效果  
  20. PointF points[]={PointF(20,220),PointF(100,200),PointF(50,280)};  
  21. graphics.SetSmoothingMode(SmoothingModeHighQuality);//设定连接时的平滑度  
  22.   
  23. pen.SetLineJoin(LineJoinMiter);//设定为斜接  
  24. graphics.DrawLines(&pen,points,3);  
  25.   
  26. points[0].X+=100;points[1].X+=100;points[2].X+=100;  
  27. pen.SetLineJoin(LineJoinBevel);//设定为斜切  
  28. graphics.DrawLines(&pen,points,3);  
  29.   
  30. points[0].X+=100;points[1].X+=100;points[2].X+=100;  
  31. pen.SetLineJoin(LineJoinRound );//设定连接方式为圆形  
  32. graphics.DrawLines(&pen,points,3);  
  33.   
  34. points[0].X+=100;points[1].X+=100;points[2].X+=100;  
  35. pen.SetLineJoin(LineJoinMiterClipped);//设定为剪裁斜接  
  36. graphics.DrawLines(&pen,points,3);  
运行结果:


画笔的斜连接限制(交集长度限制)---SetMiterLimit

当两条直线以一定的角度相交时,其形成的交集如图所示:


在实际绘制中,Miter后的灰色部分是不会绘制的,也就是说,会少个角,为什么会这样呢,考虑一种情况,当两条直线之间的夹角较小或者更极端一点,以近乎平行的方式相交,使用LineJoinMiter方式绘制这两条的交集时,其结果则令人难以想像,因为细窄的交集快成了一条直线,接近无限的延伸下去,为了解决这个问题,当两条直线直交时,我们就需要对它们的交集的长度进行裁剪,超出部分将不会被绘制,而这个交集的长度就是由画笔的斜连接限制来确定的。

画笔的斜连接限制是指斜连接长度与画笔宽度之间所允许的最大比值。默认值是10。大家可以通过SetMiterLimit函数来设置画笔的斜连接限制值。

示例1(采取默认值):

[cpp]  view plain  copy
  1. PointF points[]={PointF(20,220),PointF(500,220),PointF(30,280)};  
  2. pen.SetLineJoin(LineJoinMiter);//设定为斜接  
  3. graphics.DrawLines(&pen,points,3);  

示例二(设定斜连接限制为2)

[cpp]  view plain  copy
  1. PointF points[]={PointF(20,220),PointF(500,220),PointF(30,280)};  
  2. pen.SetLineJoin(LineJoinMiter);//设定为斜接  
  3. pen.SetMiterLimit(2.0f);  
  4. graphics.DrawLines(&pen,points,3);  

引用:https://blog.csdn.net/harvic880925/article/details/9028823

猜你喜欢

转载自blog.csdn.net/wyq429703159/article/details/80165426