[카테고리:] <span>Window Forms [C#]</span>

안녕하세요. 언제나휴일입니다.

선을 그릴 때 Pen 개체를 이용합니다.

Pen 개체는 다양한 색, 두께, 시작과 끝 스타일 및 Dash 스타일 등을 지정할 수 있습니다.

같이 작성해 보시고 Look & Feel 하세요.

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace 펜_종류
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Pen pen = new Pen(Color.Black);
            Font font = SystemFonts.DefaultFont;
            for (int width = 1; width < 10; width++)
            {
                pen.Width = width;
                graphics.DrawLine(pen, new Point(0, width*20), new Point(150, width * 20));
                graphics.DrawString(width.ToString(), font, Brushes.Black, 170, width * 20-5);
            }

            DashStyle[] dss = new DashStyle[] {DashStyle.Custom, DashStyle.Solid, DashStyle.Dash, DashStyle.Dot, DashStyle.DashDot, DashStyle.DashDotDot };
            pen.Width = 3;
            for(int di=1; di < dss.Length; di++)
            {
                pen.DashStyle = dss[di];
                graphics.DrawLine(pen, new Point(200, di * 20), new Point(350, di * 20));
                graphics.DrawString(pen.DashStyle.ToString(), font, Brushes.Black, 370, di * 20 - 5);
            }

            LineCap[] lcs = new LineCap[] {LineCap.Custom, LineCap.AnchorMask, LineCap.ArrowAnchor,
               LineCap.DiamondAnchor, LineCap.Flat,LineCap.NoAnchor,LineCap.Round,
               LineCap.RoundAnchor, LineCap.Square, LineCap.SquareAnchor,LineCap.Triangle };
            for (int lci = 1; lci< lcs.Length; lci++)
            {
                pen.StartCap = lcs[lci];
                pen.EndCap = lcs[lci];                
                graphics.DrawLine(pen, new Point(500, lci * 20), new Point(650, lci * 20));
                graphics.DrawString(pen.EndCap.ToString(), font, Brushes.Black, 670, lci * 20 - 5);
            }
            pen.Width = 1;
            pen.DashStyle = DashStyle.Solid;
            pen.StartCap = pen.EndCap = LineCap.Flat;
            for(int x=0; x<255;x++)
            {
                for(int y = 0;y < 255;y++)
                {
                    pen.Color = Color.FromArgb((x+y)/2, x, y);
                    graphics.DrawRectangle(pen, new Rectangle(x, y + 300, 1, 1));
                }
            }
        }
    }
}
실행 화면
실행 화면

Window Forms [C#]