优秀的编程知识分享平台

网站首页 > 技术文章 正文

C# 绘制用户自定义“圆环形按钮”(WinForm)

nanyue 2024-11-12 11:47:27 技术文章 1 ℃

绘制按钮颜色列表:

        private List<Color> buttonColors; // 按钮颜色列表
        private int activeButtonIndex = -1; // 激活的按钮索引       

        public _55l2()
        {
            InitializeComponent();
            buttonColors = new List<Color>
            {
               
                Color.Green,
                Color.Blue,
                 Color.Red,
                Color.Yellow,
                Color.Orange
            };

        }

绘制按钮扇形或甜甜圈


       protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Rectangle rect = new Rectangle(0, 0, Width - 1, Height - 1);

            // 绘制按钮扇形
            if (buttonColors.Count > 0)
            {
                float startAngle = -90;
                float sweepAngle = 360f / buttonColors.Count;

                for (int i = 0; i < buttonColors.Count; i++)
                {
                    using (SolidBrush buttonBrush = new SolidBrush(buttonColors[i]))
                    {
                        if (i == activeButtonIndex)
                        {
                            g.FillPie(buttonBrush, rect, startAngle, sweepAngle);
                        }
                        else
                        {
                            g.FillPie(buttonBrush, rect, startAngle, sweepAngle - 1);
                        }
                    }

                    startAngle += sweepAngle;
                }
            }
            // 绘制按钮扇形或甜甜圈  
            if (buttonColors.Count > 0)
            {                              
                float innerRectRadius = rect.Height/2/2-Width/2/8; // 调整甜甜圈中心的圆的大小  

                for (int i = 0; i < buttonColors.Count; i++)
                {
                    using (SolidBrush buttonBrush = new SolidBrush(buttonColors[i]))
                    {
                        if (i == activeButtonIndex)
                        {
                            
                            g.FillEllipse(buttonBrush, rect.X + innerRectRadius, rect.Y + innerRectRadius, rect.Width - 2 * innerRectRadius, rect.Height - 2 * innerRectRadius);
                            // 添加文本
                            string buttonText = (i + 1) + "号";
                            SizeF textSize = g.MeasureString(buttonText, new Font(Font.FontFamily, 100)); // 调整字体大小为10
                            PointF textPosition = new PointF(rect.X + (rect.Width - textSize.Width) / 2, rect.Y + (rect.Height - textSize.Height) / 2);
                            g.DrawString(buttonText, new Font(Font.FontFamily, 100), Brushes.Pink, textPosition); // 调整字体大小为10
                        }
                    }                  
                }                                                                                    

引发按钮点击事件

       protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            float startAngle = -90;
            float sweepAngle = 360f / buttonColors.Count;

            for (int i = 0; i < buttonColors.Count; i++)
            {
                GraphicsPath buttonPath = new GraphicsPath();
                buttonPath.AddPie(new Rectangle(0, 0, Width - 1, Height - 1), startAngle, sweepAngle);

                if (buttonPath.IsVisible(e.Location))
                {
                    activeButtonIndex = i;
                    Invalidate();
                    break;
                }

                startAngle += sweepAngle;
            }
        }

#把青春华章写在祖国大地上##头条创作挑战赛#

Tags:

最近发表
标签列表