Как динамически создать n Buttons произвольной формы

Необходимо на форме создать произвольное количество кнопок, такой формы, что бы все они сходились в центре(треугольники). Я так понимаю нужно создать свой класс кнопок а потом уже пользоваться им? Помогите. Пример на картинке


Ответы (1 шт):

Автор решения: Pavlo Khyzhniak

Можно попробовать так:

// Load your bitmap for the button private Bitmap bmpButton_06 = Properties.Resources.button_06;

    private void Form1_Load(object sender, EventArgs e)
    {           
        Button button;

        int countButton = 7;
        float startAngle = (float)(-30F  );
        float sweepAngle = (float)(360F / countButton);
        PointF rectPoint = new PointF(50, 50);
        float R = 200;
        SizeF rectSize = new SizeF(2 * R, 2 * R);
        RectangleF rect = new RectangleF(rectPoint, rectSize);
        PointF center = new PointF(rectPoint.X + (rectSize.Width / 2), rectPoint.Y + (rectSize.Height / 2));

        Graphics gr = this.CreateGraphics();

        for (int i = 0; i < countButton; i++)
        {
            float wx1 = (float)(center.X + R * Math.Cos((double)startAngle * 2 * Math.PI / 360));
            float wy1 = (float)(center.Y + R * Math.Sin((double)startAngle * 2 * Math.PI / 360));
            float wx2 = (float)(center.X + R * Math.Cos((double)(startAngle + sweepAngle) * 2 * Math.PI / 360));
            float wy2 = (float)(center.Y + R * Math.Sin((double)(startAngle + sweepAngle) * 2 * Math.PI / 360));
            
            float xM = (wx1 + wx2 + center.X) / 3;
            float yM = (wy1 + wy2 + center.Y) / 3;

            GraphicsPath path;
            path = new GraphicsPath();
            path.StartFigure();
            path.AddLine(center.X, center.Y, wx1, wy1);
            path.AddArc(rect, startAngle, sweepAngle);
            path.AddLine(wx2, wy2, center.X, center.Y);
            RectangleF rectF = path.GetBounds();
            path.AddString(i.ToString(), FontFamily.GenericSansSerif, 20, 20, new PointF(xM,yM), StringFormat.GenericDefault);
            path.CloseFigure();

            gr.FillPath(Brushes.Red, path);
            gr.DrawPath(Pens.Black, path);
            button = new Button()
            {
                Name = "button_" + (i + 1).ToString(),
                Region = new Region(path),
            //    Text = "Function_" + (i + 1).ToString(),
            //    TextAlign = ContentAlignment.MiddleCenter,
                Visible = true,
                AutoSize = true,
            //    BackgroundImage = bmpButton_06,
                Size = new Size((int)(rectSize.Width+rectPoint.X + 10), (int)(rectSize.Height+rectPoint.Y + 10))
            };
            button.Click += Button_Click;
            this.Controls.Add(button);

            startAngle += sweepAngle;
        }

        this.Invalidate();
    }

    private void Button_Click(object sender, EventArgs e)
    {
        if(sender is Button button)
        MessageBox.Show(button.Name);
    }
→ Ссылка