1 创建用户控件
右击项目名 => 添加 => 用户控件(Windows窗体)
2 计算五角星各点坐标
设五角星顶点的坐标为(Xa,Ya),边长为a
在c#坐标系中,y轴向下为正,x轴向右为正
通过计算得出五角星各个坐标及示意图如下
3 重写OnPaint函数
protected override void OnPaint(PaintEventArgs e)
{
Graphics gra = e.Graphics;
Brush brush = new SolidBrush(Color.FromArgb(51, 153, 255));//填充的颜色
double sin18 = Math.Sin(Math.PI / 10);
double cos18 = Math.Cos(Math.PI / 10);
double sin36 = Math.Sin(Math.PI / 5);
// 2*边长 + 2*边长*sin36 <= 宽度
double length = this.Width / ((1+sin36)*2) ; //五角星边长
// A
PointF top = new PointF(60, 0);
//B
PointF topDownLeft = new PointF((float)(top.X - (length * sin18)), (float)(length * cos18)); //与顶点相连、左下的点
//C
PointF topDownRight = new PointF((float)(top.X + (length * sin18)), (float)(length * cos18)); //与顶点相连、右下的点
//D
PointF topDownLeftLeft = new PointF((float)(top.X - (length * sin18) - length), (float)(length * cos18)); //与顶点相连、左下的点 的 左边的点
//E
PointF topDownRightRight = new PointF((float)(top.X + (length * sin18) + length), (float)(length * cos18)); //与顶点相连、右下的点 的 右边的点
//F
PointF middleLeft = new PointF((float)(top.X - (length * sin18 * 2)), (float)(length * cos18 + length * sin36)); //中间左边凹点
//G
PointF middleRight = new PointF((float)(top.X + (length * sin18 * 2)), (float)(length * cos18 + length * sin36)); //中间左边凹点
//J
PointF bottomTop = new PointF(top.X, (float)(top.Y + cos18 * (2 * length + 2 * length * sin18) - (length * sin36))); //地步中间上面的点
//H
PointF bottomLeft = new PointF((float)(top.X - (sin18 * (2 * length + (2 * length * sin18)))), (float)(cos18 * ((2 * length) + 2 * length * sin18))); //底部左边的点
//I
PointF bottomRight = new PointF((float)(top.X + (sin18 * (2 * length + (2 * length * sin18)))), (float)(cos18 * ((2 * length) + 2 * length * sin18))); //底部右边的点
PointF[] point = new PointF[10];
point[0] = top;
point[1] = topDownLeft;
point[2] = topDownLeftLeft;
point[3] = middleLeft;
point[4] = bottomLeft;
point[5] = bottomTop;
point[6] = bottomRight;
point[7] = middleRight;
point[8] = topDownRightRight;
point[9] = topDownRight;
gra.FillPolygon(brush, point);
}
4效果
写好控件的代码后,编译一下,然后从工具箱拉出来就可以用了。效果如下