GDI + programming (ii) using brushes

Source: Internet
Author: User

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);

这样我们可以按需要来设置对齐方式。

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.