GDI+学习实例1——窗体单机鼠标绘制曲线

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mingtian715/article/details/80346676

程序功能:在Form里建立一个坐标系,以坐标系0点为原点,采集鼠标左键点击,绘制前后两点间的连线。

运行后,界面如下,并绘制了一个“CSDN”图案。

    

(1)初始点定义

private Point startPoint = new Point(50,217);
private Point endPoint = new Point(50, 217);
(2)绘制坐标系
  private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
            Graphics g = e.Graphics;
            Font vertFont = new Font("Verdana", 10, FontStyle.Bold);
            Font horzFont = new Font("Verdana", 10, FontStyle.Bold);
            SolidBrush vertBrush = new SolidBrush(Color.Black);
            SolidBrush horzBrush = new SolidBrush(Color.Blue);
            Pen blackPen = new Pen(Color.Black,2);
            Pen bluePen = new Pen(Color.Blue,2);

            g.DrawLine(blackPen, 50, 220, 50, 25);
            g.DrawLine(bluePen, 50, 220, 250, 220);

            g.DrawString("0", horzFont, horzBrush, 30, 220);
            g.DrawString("1", horzFont, horzBrush, 50, 220);
            g.DrawString("2", horzFont, horzBrush, 70, 220);
            g.DrawString("3", horzFont, horzBrush, 90, 220);
            g.DrawString("4", horzFont, horzBrush, 110, 220);
            g.DrawString("5", horzFont, horzBrush, 130, 220);
            g.DrawString("6", horzFont, horzBrush, 150, 220);
            g.DrawString("7", horzFont, horzBrush, 170, 220);
            g.DrawString("8", horzFont, horzBrush, 190, 220);
            g.DrawString("9", horzFont, horzBrush, 210, 220);
            g.DrawString("10", horzFont, horzBrush, 230, 220);

            StringFormat vertStrFormat = new StringFormat();
            vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical;

            g.DrawString("-", horzFont, horzBrush, 50,212,vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 70, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 90, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 110, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 130, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 150, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 170, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 190, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 210, 212, vertStrFormat);
            g.DrawString("-", horzFont, horzBrush, 230, 212, vertStrFormat);

            g.DrawString("100-", vertFont, vertBrush, 20, 20);
            g.DrawString("90-", vertFont, vertBrush, 25, 40);
            g.DrawString("80-", vertFont, vertBrush, 25, 60);
            g.DrawString("70-", vertFont, vertBrush, 25, 80);
            g.DrawString("60-", vertFont, vertBrush, 25, 100);
            g.DrawString("50-", vertFont, vertBrush, 25, 120);
            g.DrawString("40-", vertFont, vertBrush, 25, 140);
            g.DrawString("30-", vertFont, vertBrush, 25, 160);
            g.DrawString("20-", vertFont, vertBrush, 25, 180);
            g.DrawString("10-", vertFont, vertBrush, 25, 200);

            vertFont.Dispose();
            vertBrush.Dispose();
            horzFont.Dispose();
            horzBrush.Dispose();
            blackPen.Dispose();
            bluePen.Dispose();
            g.Dispose();

        }

(3)鼠标点击响应函数

 private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Graphics g1 = this.CreateGraphics();
                Pen linePen = new Pen(Color.Green, 1);
                Pen ellipsePen = new Pen(Color.Red, 1);
                startPoint = endPoint;
                endPoint = new Point(e.X, e.Y);

                g1.DrawLine(linePen, startPoint, endPoint);

                if (checkBox1.Checked)
                {
                    g1.DrawRectangle(ellipsePen, e.X - 2, e.Y - 2, 4, 4);
                }
                else
                {
                    g1.DrawEllipse(ellipsePen, e.X - 2, e.Y - 2, 4, 4);
                }

                linePen.Dispose();
                ellipsePen.Dispose();
                g1.Dispose();
            
            }
        }

(4)清空绘制函数

 private void button1_Click(object sender, EventArgs e)
        {
            // Create a Graphics object
			Graphics g = this.CreateGraphics();
            startPoint.X = 50;
            startPoint.Y = 217;
            endPoint.X = 50;
            endPoint.Y = 217;
            this.Invalidate(this.ClientRectangle);
        }
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页