1.使用GDI+画线

     转自:http://www.cnblogs.com/kiny/articles/2505602.html

             http://www.cnblogs.com/kiny/articles/2506291.html

DrawLine(Pen pen, Point pt1, Point pt2)
DrawLine(Pen pen, PointF pt1, PointF pt2)
  
DrawLine(Pen pen, float x1, float y1, float x2, float y2)
DrawLine(Pen pen, int x1, int y1, int x2, int y2)
DrawLines(Pen pen, Point[] points)
DrawLines(Pen pen, PointF[] points)

1.连接两个Point结构的线

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            //定义画笔
            Pen pen = new Pen(Color.Red, 3.0f);

            //定义两个点
            Point pointStart = new Point(100, 100);
            Point pointEnd = new Point(100, 500);

            //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线)
            graphics.DrawLine(pen, pointStart, pointEnd);
        }

2.连接两个PointF结构的线

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            //定义画笔
            Pen pen = new Pen(Color.Red, 3.0f);

            //定义两个点
            PointF pointStart = new PointF(100.0f, 100.0f);
            PointF pointEnd = new PointF(100.0f, 500.0f);

            //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线)
            graphics.DrawLine(pen, pointStart, pointEnd);
        }

3.连接由坐标对指定的两个点的线条

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            //定义画笔
            Pen pen = new Pen(Color.Red, 3.0f);

            //定义点坐标
            int x1 = 100;
            int x2 = 100;
            int y1 = 100;
            int y2 = 500;

            //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线)
            graphics.DrawLine(pen, x1, y1, x2, y2);
        }

4.连接由坐标对指定的两个点的线条

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            //定义画笔
            Pen pen = new Pen(Color.Red, 3.0f);

            //定义点坐标
            float x1 = 100.0f;
            float x2 = 100.0f;
            float y1 = 100.0f;
            float y2 = 500.0f;

            //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线)
            graphics.DrawLine(pen, x1, y1, x2, y2);
        }

5.绘制多个直线

private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            //定义画笔
            Pen pen = new Pen(Color.Red, 3.0f);

            //定义坐标数组
            PointF[] points = { 
                        new PointF(10.0f,10.0f),
                        new PointF(10.0f,100.0f),
                        new PointF(200.0f,50.0f),
                        new PointF(300.0f,100.0f),
                        new PointF(300.0f,150.0f)
                    };

            //画线(线条粗线为3.0f,长度400,垂直于屏幕的一条直线)
            graphics.DrawLines(pen, points);
        }

6.应用

 private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //创建画板从Paint事件中的直接引用Graphics对象
            Graphics graphics = e.Graphics;
            graphics.Clear(Color.Black);

            //定义画笔
            Pen pen = new Pen(Color.White, 3.0f);
            Pen thickPen = new Pen(Color.White, 2.0f);
            Pen thick = new Pen(Color.Red, 2.0f);

            //画坐标,坐标原点在中央位置
            Point pointFirstX = new Point(0, ClientRectangle.Height / 2);//X轴起点
            Point pointFirstY = new Point(ClientRectangle.Width, ClientRectangle.Height / 2);//X轴终点
            Point pointSecondX = new Point(ClientRectangle.Width / 2, 0);//Y轴起点
            Point pointSecondY = new Point(ClientRectangle.Width / 2, ClientRectangle.Height);//Y轴终点
            graphics.DrawLine(pen, pointFirstX, pointFirstY);
            graphics.DrawLine(pen, pointSecondX, pointSecondY);

            //画箭头,4条短线
            Point PointUpLeftX = new Point(ClientRectangle.Width / 2, 0);
            Point PointUpLeftY = new Point(ClientRectangle.Width / 2 - 10, 10);
            Point PointUpRightY = new Point(ClientRectangle.Width / 2 + 10, 10);

            Point PointRightUpX = new Point(ClientRectangle.Width, ClientRectangle.Height / 2);
            Point PointRightUpY = new Point(ClientRectangle.Width - 10, ClientRectangle.Height / 2 - 10);
            Point PointRightDownY = new Point(ClientRectangle.Width - 10, ClientRectangle.Height / 2 + 10);

            graphics.DrawLine(pen, PointUpLeftX, PointUpLeftY);
            graphics.DrawLine(pen, PointUpLeftX, PointUpRightY);
            graphics.DrawLine(pen, PointRightUpX, PointRightUpY);
            graphics.DrawLine(pen, PointRightUpX, PointRightDownY);

            //画原点
            PointF centre = new PointF(ClientRectangle.Width / 2, ClientRectangle.Height / 2);
            Font font = new Font("黑体", 14.0f);
            Brush brush = Brushes.White;
            graphics.DrawString("0", font, brush, centre);

            //画坐标点,每隔50像素画一个坐标点    
            int iWidht = ClientRectangle.Width / 2;
            int iHeight = ClientRectangle.Height / 2;

            int j = 0;//X轴正半轴
            for (int i = iWidht; i < ClientRectangle.Width; i = i + 50)
            {
                Point tmpStart = new Point(i, iHeight);
                Point tmpEnd = new Point(i, iHeight - 8);
                graphics.DrawLine(thickPen, tmpStart, tmpEnd);//画短线标志
                if (j != 0)
                {
                    //写数据标志
                    graphics.DrawString(j.ToString(), font, brush, new Point(tmpEnd.X - 7, tmpEnd.Y + 10));
                }
                j++;
            }

            int k = 0;//X轴负半轴
            for (int i = iWidht; i > 0; i = i - 50)
            {
                Point tmpStart = new Point(i, iHeight);
                Point tmpEnd = new Point(i, iHeight - 8);
                graphics.DrawLine(thickPen, tmpStart, tmpEnd);//画短线标志
                if (k != 0)
                {
                    //写数据标志
                    graphics.DrawString("-" + k.ToString(), font, brush, new Point(tmpEnd.X - 7, tmpEnd.Y + 10));
                }
                k++;
            }

            int l = 0;//Y轴正半轴
            for (int i = iHeight; i > 0; i = i - 50)
            {
                Point tmpStart = new Point(iWidht, i);
                Point tmpEnd = new Point(iWidht + 8, i);
                graphics.DrawLine(thickPen, tmpStart, tmpEnd);//画短线标志
                if (l != 0)
                {
                    //写数据标志
                    graphics.DrawString(l.ToString(), font, brush, new Point(tmpEnd.X, tmpEnd.Y - 9));
                }
                l++;
            }

            int m = 0;//Y轴负半轴
            for (int i = iHeight; i < ClientRectangle.Height; i = i + 50)
            {
                Point tmpStart = new Point(iWidht, i);
                Point tmpEnd = new Point(iWidht + 8, i);
                graphics.DrawLine(thickPen, tmpStart, tmpEnd);//画短线标志
                if (m != 0)
                {
                    //写数据标志
                    graphics.DrawString("-" + m.ToString(), font, brush, new Point(tmpEnd.X, tmpEnd.Y - 9));
                }
                m++;
            }


            //定义坐标数组
            PointF[] points = { 
                        new PointF(105.0f,102.0f),
                        new PointF(106.0f,105.0f),
                        new PointF(200.0f,121.0f),
                        new PointF(220.0f,126.0f),
                        new PointF(230.0f,110.0f),
                        new PointF(300.0f,200.0f),
                        new PointF(320.0f,95.0f),
                        new PointF(343.0f,158.0f),
                        new PointF(360.0f,100.0f),
                        new PointF(450.0f,184.3f)
                    };

            //画折线
            graphics.DrawLines(thick, points);
        }

    

     这是不同窗体大小下面的显示效果,因为线的位置是绝对的不会,而坐标系会随着窗体的变化而改变中心位置。如果将两个图重叠着看,那么这两个红线会重叠,另外会存在两个不同的坐标系。

posted on 2012-10-25 16:17  凡一二三  阅读(6832)  评论(0编辑  收藏  举报