using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; namespace MyFirstProject { public partial class Drows : Form { public Drows() { InitializeComponent(); } private void Drows_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { //动态画饼形图 int[] arr = new int[] { 5, 4, 7, 6, 3, 2, 7, 9, 3 }; int sum = 0;//总数 float num = 0;//度数 foreach (int i in arr) { sum += i; //求和 } Random Ra = new Random(); for (int i = 0; i < arr.Length; i++) { SB.Color = Color.FromArgb(Ra.Next(0, 255), Ra.Next(0, 255), Ra.Next(0, 255));//随机的画笔颜色 Gra.FillPie(SB, 300, 300, 200, 200, num, arr[i] * 360f / sum);//画扇形,指定画笔,坐标,大小,开始结束角度 num += arr[i] * 360f / sum; //画完后记录上次结束度数,以作为下次的开始度数 } } //画图的事件 private void Drows_Paint(object sender, PaintEventArgs e) { //button1_Click(null, null); } } } 效果