Csharp/C Sharp/2D Graphics/Pens — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:42, 26 мая 2010

11-Pixel Centered Pen

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
      public Form1() {
            InitializeComponent();
            
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      Rectangle rect = new Rectangle(10, 10, 110, 110);
      Pen pen = new Pen(Color.White, 11);
      Pen penOutline = new Pen(Color.Black, 1);
      penOutline.Alignment = PenAlignment.Inset;
      pen.Alignment = PenAlignment.Center;
      e.Graphics.DrawString("11-Pixel Centered Pen", SystemFonts.DefaultFont, Brushes.Black, rect.Location);
      rect.Offset(0, 25);
      e.Graphics.FillRectangle(Brushes.LightBlue, rect);
      e.Graphics.DrawRectangle(pen, rect);
      e.Graphics.DrawRectangle(penOutline, rect);
      
      pen.Dispose();
    }

    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // SimpleStyleRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(384, 353);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);
    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}


11-Pixel Inset Pen

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing.Drawing2D;
public class Form1 : Form
{
      public Form1() {
            InitializeComponent();
            
      }
    private void SimpleStyleRenderer_Paint(object sender, PaintEventArgs e)
    {
      Rectangle rect = new Rectangle(10, 10, 110, 110);
      Pen pen = new Pen(Color.White, 11);
      Pen penOutline = new Pen(Color.Black, 1);
      penOutline.Alignment = PenAlignment.Inset;
      pen.Alignment = PenAlignment.Center;
      rect.Offset(10, -25);
      e.Graphics.DrawString("11-Pixel Inset Pen", SystemFonts.DefaultFont, Brushes.Black, rect.Location);
      rect.Offset(0, 25);
      pen.Alignment = PenAlignment.Inset;
      e.Graphics.FillRectangle(Brushes.LightBlue, rect);
      e.Graphics.DrawRectangle(pen, rect);
      e.Graphics.DrawRectangle(penOutline, rect);
      
      
      
      pen.Dispose();
    }

    private void InitializeComponent()
    {
      this.SuspendLayout();
      // 
      // SimpleStyleRenderer
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(384, 353);
      this.Name = "SimpleStyleRenderer";
      this.Text = "SimpleStyleRenderer";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleStyleRenderer_Paint);
      this.ResumeLayout(false);
    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }
}


creates the custom dash pattern

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    Pen p = new Pen(Color.Black, 2);
    float[] f = { 15, 5, 10, 5 };
    p.DashPattern = f;
    g.DrawRectangle(p, 10, 10, 80, 100);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


creates the custom dash pattern 5

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Pen p = new Pen(Color.Black, 1);
    float[] f = { 15, 5, 10, 5 };
    p.DashPattern = f;
    g.DrawRectangle(p, 10, 10, 80, 100);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Custom Pen

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public class MainForm : Form {
    public MainForm() {
    }
    protected void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Pen bluePen = new Pen(Color.Blue, 20);
        Pen pen2 = Pens.Firebrick;
        g.DrawEllipse(bluePen, 10, 10, 100, 100);
        g.DrawLine(pen2, 10, 130, 110, 130);
        g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80);
        Pen pen3 = new Pen(Color.Purple, 5);
        pen3.DashStyle = DashStyle.DashDotDot;
        g.DrawPolygon(pen3, new Point[]{     new Point(30, 140),
          new Point(265, 200), new Point(100, 225),
          new Point(190, 190), new Point(50, 330),
          new Point(20, 180)});
        Rectangle r = new Rectangle(150, 10, 130, 60);
        g.DrawRectangle(Pens.Blue, r);
        g.DrawString("Hello out there...How are ya?",new Font("Arial", 12), Brushes.Black, r);
        Pen customDashPen = new Pen(Color.BlueViolet, 10);
        float[] myDashes = { 5.0f, 2.0f, 1.0f, 3.0f };
        customDashPen.DashPattern = myDashes;
        g.DrawRectangle(customDashPen, ClientRectangle);
    }
    public static void Main(){
        Application.Run(new MainForm());    
    }
}


DashDot style Pen

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Pen p = new Pen(Color.HotPink, 10);
        p.DashStyle = DashStyle.DashDot;
        Graphics g = this.CreateGraphics();
        g.DrawEllipse(p, 10, 15, 105, 250);
    }
}


Dispose a pen

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White, this.ClientRectangle);
        Pen p = new Pen(Color.Black);
        g.DrawLine(p, 0, 0, 100, 100);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


illustrates the use of multiple Pens

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example21_2.cs illustrates the use of multiple Pens
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Example21_2 : System.Windows.Forms.Form
{
  private System.ruponentModel.Container components = null;
  public Example21_2()
  {
    InitializeComponent();
  }
  protected override void Dispose( bool disposing )
  {
    if( disposing )
    {
      if (components != null) 
      {
        components.Dispose();
      }
    }
    base.Dispose( disposing );
  }
  private void InitializeComponent()
  {
    this.BackColor = System.Drawing.Color.White;
    this.ClientSize = new System.Drawing.Size(400, 400);
    this.Name = "Example21_2";
    this.Text = "Example21_2";
    this.Paint += new System.Windows.Forms.
      PaintEventHandler(this.Example21_2_Paint);
  }

  static void Main() 
  {
    Application.Run(new Example21_2());
  }
  private void Example21_2_Paint(
    object sender, System.Windows.Forms.PaintEventArgs e)
  {
    Graphics g = e.Graphics;
    // draw two lines with one pen
    Pen p = new Pen(Color.Black, 10);
    g.DrawLine(p, 25, 25, 375, 375);
    g.DrawLine(p, 25, 375, 375, 25);
    // draw four lines with another pen
    Pen p2 = new Pen(Color.Gray, 7);
    p2.EndCap = LineCap.Round;
    p2.StartCap = LineCap.ArrowAnchor;
    g.DrawLine(p2, 25, 35, 25, 365);
    g.DrawLine(p2, 35, 375, 365, 375);
    g.DrawLine(p2, 375, 365, 375, 35);
    g.DrawLine(p2, 365, 25, 35, 25);
  }
}


make red and blue pens

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class TestGDI1 : System.Windows.Forms.Form{
    
    //in order to paint something OnPaint method needs to be overridden
    
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe) {
        //OnPaint method is a member of Form class 
        //The following call sends pe to an event listener Graphics
        base.OnPaint(pe);
        
        //initialize Graphics 
        System.Drawing.Graphics g=pe.Graphics;
        
        //designate the area of the form where the drawing must take place
        //ClientRectangle is a member of Windows.Forms.Control class
        System.Drawing.Rectangle client_area=this.ClientRectangle;
        
        //point11 is at the top left corner of the client_area
        System.Drawing.Point point11=new System.Drawing.Point(client_area.X,client_area.Y); 
        //point12 is at the bottom right corner of the client area
        
        System.Drawing.Point point12=new System.Drawing.Point(client_area.Width,client_area.Height);
        
        //create a Brush object of white color
        //SolidBrush means that the color does not change from point to point
        System.Drawing.Brush background=new System.Drawing.SolidBrush(System.Drawing.Color.White);
        
        //color client_area with solid white brush
        g.FillRectangle(background,client_area);
        
        //make red and blue pens
        System.Drawing.Pen p=new System.Drawing.Pen(System.Drawing.Color.Red);
        System.Drawing.Pen p1=new System.Drawing.Pen(System.Drawing.Color.Blue);
        
        //create points and rectangles
        System.Drawing.SizeF size=new System.Drawing.SizeF();
        size.Height=160;
        size. Width=180;
        System.Drawing.PointF point=new System.Drawing.PointF();
        point.X=8;
        point.Y=40;
        System.Drawing.Point point1=new System.Drawing.Point();
        point1.X=300;
        point1.Y=300;
        System.Drawing.Point point2=new System.Drawing.Point();
        point2.X=0;
        point2.Y=0;
        System.Drawing.RectangleF rec =new System.Drawing.RectangleF(point,size);
        
        //draw an ellipse inscribed in the invisible rectangle rec
        //to change the size or shape of the ellipse change an invisible rectangle in which it is inscribed
        //to change the color of the ellipse, change the color of the pen p which is used to draw it
        
        g.DrawEllipse(p,rec);
    
        //draw a line between a pair of points point1 and point2 with pen p1
        g.DrawLine(p1,point1,point2);
    }
    public static void Main() {
        System.Windows.Forms.Application.Run(new TestGDI1());//display form
    }
}


new Pen(clr, f)

 
using System;
using System.Drawing;
using System.Windows.Forms;
   
class PenWidths: Form
{
     public static void Main()
     {
          Application.Run(new PenWidths());
     }
     public PenWidths()
     {
          Text = "Pen Widths";
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }     
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Brush brush = new SolidBrush(clr);
          float y     = 0;
   
          grfx.PageUnit  = GraphicsUnit.Point;
          grfx.PageScale = 1;
   
          for (float f = 0; f < 3.2; f += 0.2f)
          {
               Pen    pen   = new Pen(clr, f);
               string str   = String.Format("{0:F1} point wide pen: ", f);
               SizeF  sizef = grfx.MeasureString(str, Font);
   
               grfx.DrawString(str, Font, brush, 0, y);
               grfx.DrawLine(pen, sizef.Width,       y + sizef.Height / 2,
                                  sizef.Width + 144, y + sizef.Height / 2);
               y += sizef.Height;
          }
     }
}


new Pen(ForeColor)

 

using System;
using System.Drawing;
using System.Windows.Forms;
   
class XMarksTheSpot: Form
{
     public static void Main()
     {
          Application.Run(new XMarksTheSpot());
     }
     public XMarksTheSpot()
     {
          Text = "X Marks The Spot";
          ResizeRedraw = true;
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          Graphics grfx = pea.Graphics;
          Pen      pen  = new Pen(ForeColor);
   
          grfx.DrawLine(pen, 0, 0, 
                             ClientSize.Width - 1, ClientSize.Height - 1);
          grfx.DrawLine(pen, 0, ClientSize.Height - 1, 
                             ClientSize.Width - 1, 0);
     }
}


new Pen(lgbrush, Math.Min(cx, cy) / 25), Gradient Pen

 
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class GradientPen: Form 
{
     public static void Main()
     {
          Application.Run(new GradientPen());
     }
     public GradientPen()
     {
          ResizeRedraw = true; 
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }        
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Brush lgbrush = new LinearGradientBrush(
                                   new Rectangle(0, 0, cx, cy), 
                                   Color.White, Color.Black,
                                   LinearGradientMode.BackwardDiagonal);
   
          Pen pen = new Pen(lgbrush, Math.Min(cx, cy) / 25);
   
          pen.Alignment = PenAlignment.Inset;
   
          grfx.DrawRectangle(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, 0, cx, cy);
          grfx.DrawLine(pen, 0, cy, cx, 0);
     }
}


Pen alignment: center

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
        Graphics g = e.Graphics;
      Pen p = new Pen(Color.Black, 3);
      p.Alignment = PenAlignment.Center;
      g.DrawRectangle(p, 3, 3, 80, 70);
      p.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Pen alignment: inset

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class Form1 : System.Windows.Forms.Form
  {
    public Form1()
    {
      InitializeComponent();
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {      
      Graphics g = e.Graphics;
      Pen p = new Pen(Color.Black, 3);
      p.Alignment = PenAlignment.Inset;
      g.DrawRectangle(p, 3, 3, 80, 70);
      p.Dispose();
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
      Invalidate();
    }
  }


Pen Dash Caps: Flat, Round, Triangle

 

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
   
class PenDashCaps: Form
{
     MenuItem miChecked;
   
     public static void Main()
     {
          Application.Run(new PenDashCaps());
     }
     public PenDashCaps()
     {
          ResizeRedraw = true; 
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("&Width");
   
          int[] aiWidth = { 1, 2, 5, 10, 15, 20, 25 };
   
          foreach (int iWidth in aiWidth)
               Menu.MenuItems[0].MenuItems.Add(iWidth.ToString(), 
                                        new EventHandler(MenuWidthOnClick));
   
          miChecked = Menu.MenuItems[0].MenuItems[0];
          miChecked.Checked = true;
     }
     void MenuWidthOnClick(object obj, EventArgs ea)
     {
          miChecked.Checked = false;
          miChecked = (MenuItem) obj;
          miChecked.Checked = true;
          Invalidate();
     }
     protected override void OnPaint(PaintEventArgs pea)
     {
          DoPage(pea.Graphics, ForeColor,ClientSize.Width, ClientSize.Height);
     }      
     protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
     {
          Pen pen = new Pen(clr, Convert.ToInt32(miChecked.Text));
          pen.DashStyle = DashStyle.DashDotDot;
   
          foreach (DashCap dc in Enum.GetValues(typeof(DashCap)))
          {
               pen.DashCap = dc;
   
               grfx.DrawLine(pen, cx / 8, cy / 4, 7 * cx / 8, cy / 4); 
               grfx.TranslateTransform(0, cy / 4);
          }
     }
}


Pen Dash Styles

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace GDI_Basics
{
    /// <summary>
    /// Summary description for PenDashStyles.
    /// </summary>
    public class PenDashStyles : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ruponentModel.Container components = null;
        public PenDashStyles()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }
        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            // 
            // PenDashStyles
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(500, 398);
            this.Name = "PenDashStyles";
            this.Text = "PenDashStyles";
            this.Resize += new System.EventHandler(this.PenDashStyles_Resize);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.PenDashStyles_Paint);
        }
        #endregion
        [STAThread]
        static void Main() 
        {
            Application.Run(new PenDashStyles());
        }
        private void PenDashStyles_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Pen myPen = new Pen(Color.Blue, 10);
            int y = 20;
            foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle)))
            {
                myPen.DashStyle = dash;
                e.Graphics.DrawLine(myPen, 20, y, 100, y);
                e.Graphics.DrawString(dash.ToString(), new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
                y += 30;
            }
            
            y += 10;
            myPen.StartCap = LineCap.Round;
            myPen.EndCap = LineCap.Round;
            
            foreach (DashStyle dash in System.Enum.GetValues(typeof(DashStyle)))
            {
                myPen.DashStyle = dash;
                e.Graphics.DrawLine(myPen, 20, y, 100, y);
                e.Graphics.DrawString(dash.ToString() + " (with round caps)", new Font("Tahoma", 8), Brushes.Black, 120, y - 10);
                y += 30;
            }
        }
        private void PenDashStyles_Resize(object sender, System.EventArgs e)
        {
            this.Invalidate();
        }
    }
}


Pen width and color

  using System;
  using System.Drawing;
  using System.Drawing.Drawing2D;
  using System.Collections;
  using System.ruponentModel;
  using System.Windows.Forms;
  using System.Data;
  public class MainForm : System.Windows.Forms.Form
  {
    public MainForm()
    {
      InitializeComponent();
      CenterToScreen();
      SetStyle(ControlStyles.ResizeRedraw, true);  
    }
    private void InitializeComponent()
    {
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(292, 273);
      this.Text = "Pens...";
      this.Resize += new System.EventHandler(this.Form1_Resize);
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
    }
    static void Main() 
    {
      Application.Run(new MainForm());
    }
    private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
      Graphics g = e.Graphics;
      Pen bluePen = new Pen(Color.Blue, 20);
      Pen pen2 = Pens.Firebrick;
      // Render some shapes with the pens.
      g.DrawEllipse(bluePen, 10, 10, 100, 100);
      g.DrawLine(pen2, 10, 130, 110, 130);
      g.DrawPie(Pens.Black, 150, 10, 120, 150, 90, 80);
    }
    private void Form1_Resize(object sender, System.EventArgs e)
    {
       Invalidate();  
    }
  }


Set DashStyle

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White, this.ClientRectangle);
        Pen p = new Pen(Color.Black, 1);
        p.DashStyle = DashStyle.Dash;
        g.DrawLine(p, 3, 3, 100, 3);
        p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


Set LineJoin for Pen

 
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
    protected override void OnPaint(PaintEventArgs e) {
    Graphics g = e.Graphics;
    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.FillRectangle(Brushes.White, this.ClientRectangle);
    Pen p = new Pen(Color.Black, 10);
    p.LineJoin = LineJoin.Bevel;
    e.Graphics.DrawRectangle(p, 20, 20, 60, 60);
    p.Dispose();
    }
    public static void Main() {
        Application.Run(new Form1());
    }
}


StartCap, EndCap

 
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
public class Form1 : System.Windows.Forms.Form {
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected override void OnPaint(PaintEventArgs e) {
        Graphics g = e.Graphics;
        Pen p = new Pen(Color.Brown, 15);
        p.StartCap = LineCap.SquareAnchor;
        p.EndCap = LineCap.SquareAnchor;
        g.DrawLine(p, 30, 190, Width - 50, 190);
    }
}