100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > c#自定义控件绘制五角星形状

c#自定义控件绘制五角星形状

时间:2022-05-27 14:40:05

相关推荐

c#自定义控件绘制五角星形状

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) ; //五角星边长// APointF top = new PointF(60, 0);//BPointF topDownLeft = new PointF((float)(top.X - (length * sin18)), (float)(length * cos18)); //与顶点相连、左下的点//CPointF topDownRight = new PointF((float)(top.X + (length * sin18)), (float)(length * cos18)); //与顶点相连、右下的点//DPointF topDownLeftLeft = new PointF((float)(top.X - (length * sin18) - length), (float)(length * cos18)); //与顶点相连、左下的点 的 左边的点//EPointF topDownRightRight = new PointF((float)(top.X + (length * sin18) + length), (float)(length * cos18)); //与顶点相连、右下的点 的 右边的点//FPointF middleLeft = new PointF((float)(top.X - (length * sin18 * 2)), (float)(length * cos18 + length * sin36)); //中间左边凹点//GPointF middleRight = new PointF((float)(top.X + (length * sin18 * 2)), (float)(length * cos18 + length * sin36)); //中间左边凹点//JPointF bottomTop = new PointF(top.X, (float)(top.Y + cos18 * (2 * length + 2 * length * sin18) - (length * sin36))); //地步中间上面的点//HPointF bottomLeft = new PointF((float)(top.X - (sin18 * (2 * length + (2 * length * sin18)))), (float)(cos18 * ((2 * length) + 2 * length * sin18))); //底部左边的点//IPointF 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效果

写好控件的代码后,编译一下,然后从工具箱拉出来就可以用了。效果如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。