(C #) with the PEN line drawing

(C #) with the PEN line drawing

Copy the code

// Function: draw a line created by PEN. 

// (in this case using create Pen. The advantage is that, when an object (pen) goes out of scope, using the structure will automatically call Dispose (), will be deleted PEN. 

// If the object creation by other means (usually very fee resources), be sure to explicitly call Dispose (), such as: 

// "Graphics G = this.CreateGraphics (); 

// ........ 

// ........ 

// g.Dispose (); " 

// IMPORTANT: 

// create Pen, Pen color and thickness to obtain height of the client area and the Width 

 

a using System; 

a using System.Collections.Generic; 

a using System.ComponentModel; 

a using the System.Data; 

a using System .Drawing; 

the using the System.Text; 

the using the System.Windows.Forms; 

 

namespace myDrawPanA 

{ 

    public partial class the Form1: Form1 

    { 

        public the Form1 () 

        {

            InitializeComponent();

        }

 

        protected override void OnPaint(PaintEventArgs e)

        {

            //base.OnPaint(e);

            Graphics g = e.Graphics;

            using (Pen myPen = new Pen(Color.Red,1))

            {

                if (ClientRectangle.Height/10>0)

                {

                    for (int y=0; y< ClientRectangle.Height; y += ClientRectangle.Height/10)

                    {

                        g.DrawLine(myPen,new Point(0,0), new Point(ClientRectangle.Width,y));

                    }

                }

            }

        }

 

 

    }

}

Copy the code

Published 16 original articles · won praise 219 · Views 250,000 +

Guess you like

Origin blog.csdn.net/cxu123321/article/details/104617455