Brushes are often used to draw outlines of shapes. In addition to the common color and Width properties, the brushes for GDI + have attributes such as alignment, line caps, and transform methods. In GDI +, you define a brush object by using the Pen class.
(i), construction and use of brushes
Pen(brush, width); //用颜色与线宽构造一个画笔
Pen(color, width); //用画刷与宽度构造一个画笔
Example:
Pen pen(Color(255, 0, 0, 0),1);//用第一个构造函数.构造宽度为1的黑色画刷
graphics.DrawLine(&pen, 20, 10, 300, 100);
Image image(L"Texture1.jpg");
TextureBrush tBrush(&image);
graphics.DrawImage(&image, 0, 0, image.GetWidth(), image.GetHeight());
Pen texturedPen(&tBrush, 30);//用第二个构造函数,用一个纹理画刷
graphics.DrawEllipse(&texturedPen, 100, 20, 200, 100);
(ii), set value width and alignment
When creating a brush, you can pass the width as a parameter to the constructor, or you can use the SetWidth () method to change the line width of the brush. An ideal line width of 0, when we draw a line, the pixel is in the middle of the line, in the following example we draw a straight line with a black brush with a width of 1, and then draw again with a brush with a green width of 10.
Pen blackPen(Color(255, 0, 0, 0), 1);
Pen greenPen(Color(255, 0, 255, 0), 10);
greenPen.SetAlignment(PenAlignmentCenter);
graphics.DrawLine(&greenPen, 10, 100, 100, 50);
graphics.DrawLine(&blackPen, 10, 100, 100, 50);
我们把绿色画笔设为中对齐时:
graphics.DrawRectangle(&greenPen, 10, 100, 50, 50);
graphics.DrawRectangle(&blackPen, 10, 100, 50, 50);
我们把绿色画笔设为内对齐时:
greenPen.SetAlignment(PenAlignmentInset);
这样我们可以按需要来设置对齐方式。