1.使用System.Drawing繪制一個正方形
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 創建一個Pen對象,用于繪制正方形 using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改顏色和線條寬度 {// 設置正方形的位置和大小 // 在這個例子中,我們從(50, 50)開始,大小為100x100 Rectangle rect = new Rectangle(50, 50, 100, 100);// 使用Graphics對象的DrawRectangle方法來繪制正方形 e.Graphics.DrawRectangle(pen, rect);}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
?
2.?使用System.Drawing繪制一個長方形
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 創建一個Pen對象,用于繪制長方形 using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改顏色和線條寬度 {// 設置長方形的位置和大小 // 在這個例子中,我們從(50, 50)開始,寬度為200,高度為100 Rectangle rect = new Rectangle(50, 50, 200, 100);// 使用Graphics對象的DrawRectangle方法來繪制長方形 e.Graphics.DrawRectangle(pen, rect);}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
3.使用System.Drawing繪制一個圓形
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 創建一個Pen對象,用于繪制圓形的邊框 using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改顏色和線條寬度 {// 創建一個Brush對象,用于填充圓形(如果需要的話) using (Brush brush = new SolidBrush(Color.LightBlue)) // 你可以更改填充顏色 {// 設置圓形的位置和大小 // 在這個例子中,圓心在(100, 100),半徑為50 int centerX = 100;int centerY = 100;int radius = 50;// 繪制圓形的邊框(使用DrawEllipse方法) e.Graphics.DrawEllipse(pen, centerX - radius, centerY - radius, 2 * radius, 2 * radius);// 如果你想要填充圓形,可以使用FillEllipse方法 // e.Graphics.FillEllipse(brush, centerX - radius, centerY - radius, 2 * radius, 2 * radius); }}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
?
4.使用System.Drawing繪制一個三角形?
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 創建一個Pen對象,用于繪制三角形的邊 using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改顏色和線條寬度 {// 設置三角形的三個頂點 Point point1 = new Point(50, 50);Point point2 = new Point(150, 50);Point point3 = new Point(100, 150);// 繪制三條線以構成三角形 e.Graphics.DrawLine(pen, point1, point2);e.Graphics.DrawLine(pen, point2, point3);e.Graphics.DrawLine(pen, point3, point1);}}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
?
5.使用System.Drawing繪制一個五角星?
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 創建一個Pen對象,用于繪制五角星 using (Pen pen = new Pen(Color.Black, 2)) // 你可以更改顏色和線條寬度 {// 設定五角星的中心點 int centerX = 100;int centerY = 100;// 設定五角星的外接圓半徑 int radius = 50;// 設定五角星的旋轉角度(如果需要) double rotateAngle = Math.PI / 2; // 從上頂點開始 // 計算五角星的五個頂點 PointF[] starPoints = CalculateStarPoints(centerX, centerY, radius, 5, rotateAngle);// 繪制五角星 for (int i = 0; i < starPoints.Length; i++){int nextIndex = (i + 1) % starPoints.Length;e.Graphics.DrawLine(pen, starPoints[i], starPoints[nextIndex]);}}}// 計算五角星的頂點 private PointF[] CalculateStarPoints(float centerX, float centerY, float radius, int spikes, double rotation){PointF[] result = new PointF[spikes];double outerRadius = radius; // 外接圓半徑 double innerRadius = radius * 0.5f * Math.Sqrt(3); // 內接圓半徑(五角星的特殊值) double angle = Math.PI / 2 * 3 - spikes * Math.PI / spikes; // 第一個頂點的角度 for (int i = 0; i < spikes; i++){result[i].X = (float)(centerX + Math.Cos(angle + rotation) * outerRadius);if (i % 2 == 0){result[i].Y = (float)(centerY + Math.Sin(angle + rotation) * outerRadius);}else{result[i].Y = (float)(centerY + Math.Sin(angle + rotation) * innerRadius);}angle += Math.PI / spikes;}return result;}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
?
6.使用System.Drawing繪制一個圓外切三角形?
using System;
using System.Drawing;
using System.Windows.Forms;public partial class MyForm : Form
{public MyForm(){// 你可以在這里設置Form的雙緩沖,以避免繪制時出現的閃爍 this.DoubleBuffered = true;}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);// 定義三角形的邊長 int sideLength = 100;// 計算外接圓的半徑 double radius = sideLength / (2 * Math.Sqrt(3));// 設定外接圓的中心點和三角形的中心點 int centerX = this.ClientSize.Width / 2;int centerY = this.ClientSize.Height / 2;// 計算三角形的頂點 PointF[] trianglePoints = CalculateEquilateralTrianglePoints(centerX, centerY, sideLength);// 繪制外接圓 using (Pen circlePen = new Pen(Color.Blue, 2)){e.Graphics.DrawEllipse(circlePen, centerX - (int)radius, centerY - (int)radius, (int)(radius * 2), (int)(radius * 2));}// 繪制三角形 using (Pen trianglePen = new Pen(Color.Black, 2)){e.Graphics.DrawLines(trianglePen, trianglePoints);}}// 計算等邊三角形的三個頂點 private PointF[] CalculateEquilateralTrianglePoints(int centerX, int centerY, int sideLength){PointF[] points = new PointF[3];double angle = Math.PI / 3; // 等邊三角形內角的一半 // 第一個頂點(上方) points[0] = new PointF(centerX, centerY - sideLength / 2);// 第二個頂點(右側) points[1] = new PointF((float)(centerX + sideLength / 2 * Math.Cos(angle)),(float)(centerY + sideLength / 2 * Math.Sin(angle)));// 第三個頂點(左側) points[2] = new PointF((float)(centerX + sideLength / 2 * Math.Cos(-angle)),(float)(centerY + sideLength / 2 * Math.Sin(-angle)));return points;}[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new MyForm());}
}
7.使用System.Drawing繪制一個圓內接三角形
?