二、使用GDI+画线(补充)
DrawLines(Pen pen,PointF[] points)
绘制多个直线
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); }
利用画线函数绘制坐标(复习画线函数,用到了DrawString函数,后面学习)
效果图:
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, this.ClientRectangle.Height / 2); Point pointFirstY = new Point(this.ClientRectangle.Width, this.ClientRectangle.Height / 2); Point pointSecondX = new Point(this.ClientRectangle.Width / 2, 0); Point pointSecondY = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height); graphics.DrawLine(pen, pointFirstX, pointFirstY); graphics.DrawLine(pen, pointSecondX, pointSecondY); //画箭头 Point PointUpLeftX = new Point(this.ClientRectangle.Width / 2, 0); Point PointUpLeftY = new Point(this.ClientRectangle.Width / 2 - 10, 10); Point PointUpRightY = new Point(this.ClientRectangle.Width / 2 + 10, 10); Point PointRightUpX = new Point(this.ClientRectangle.Width, this.ClientRectangle.Height / 2); Point PointRightUpY = new Point(this.ClientRectangle.Width - 10, this.ClientRectangle.Height / 2 - 10); Point PointRightDownY = new Point(this.ClientRectangle.Width - 10, this.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(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2); Font font = new Font("黑体", 14.0f); Brush brush = Brushes.White; graphics.DrawString("0",font,brush,centre); //画坐标点 int iWidht = this.ClientRectangle.Width/2; int iHeight = this.ClientRectangle.Height/2; int j=0; for (int i=iWidht;i<this.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; 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; 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; for (int i = iHeight; i < this.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); }