轉自: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); }
這是不同窗體大小下面的顯示效果,因為線的位置是絕對的不會,而坐標系會隨着窗體的變化而改變中心位置。如果將兩個圖重疊着看,那么這兩個紅線會重疊,另外會存在兩個不同的坐標系。