Can I draw shapes inside buttons?

I read stackoverflow for a while, just to find out, and I came across a situation where I can finally ask a question. I am making a memory game like Simon Says, where I click on the shapes of the user, and the user must click the button in the same order as the shapes. I want to draw a figure that I draw on the screen inside the button that they press, because it is much easier to remember and compare the figures with the figures, rather than the figures, on the button that shows the name of the figures.

I hope my question is clear and thanks for watching!

+4
source share
5 answers

, Image Button. , , . :

using System;
using System.Drawing;
using System.Windows.Forms;

class ShapeButton : Button {
  public Action<PaintEventArgs> DoPaint { get; set; }
  protected override void OnPaint(PaintEventArgs e) {
    if (DoPaint != null) { DoPaint(e); }
  }
}

static class Program {
  static void Main() {
    // Ellipse button
    ShapeButton ellipseButton = new ShapeButton();
    ellipseButton.Location = new Point(10, 10);
    ellipseButton.Size = new Size(80, 80);
    ellipseButton.DoPaint = delegate(PaintEventArgs e) {
      Graphics graphics = e.Graphics;
      SolidBrush brush1 = new SolidBrush(SystemColors.ButtonFace);
      graphics.FillRectangle(brush1, 0, 0, ellipseButton.Width, ellipseButton.Height);
      SolidBrush brush2 = new SolidBrush(Color.Red);
      graphics.FillEllipse(brush2, 0, 0, ellipseButton.Width, ellipseButton.Height);
    };
    ellipseButton.Click += delegate(object sender, EventArgs e) {
      MessageBox.Show("Ellipse!");
    };

    // Triangle button
    ShapeButton triangleButton = new ShapeButton();
    triangleButton.Location = new Point(100, 10);
    triangleButton.Size = new Size(80, 80);
    triangleButton.DoPaint = delegate(PaintEventArgs e) {
      Graphics graphics = e.Graphics;
      SolidBrush brush1 = new SolidBrush(SystemColors.ButtonFace);
      graphics.FillRectangle(brush1, 0, 0, triangleButton.Width, triangleButton.Height);
      SolidBrush brush2 = new SolidBrush(Color.Green);
      Point[] points = { 
        new Point(triangleButton.Width / 2, 0), 
        new Point(0, triangleButton.Height), 
        new Point(triangleButton.Width, triangleButton.Height) 
      };
      graphics.FillPolygon(brush2, points);
    };
    triangleButton.Click += delegate(object sender, EventArgs e) {
      MessageBox.Show("Triangle!");
    };

    // Star button (using image)
    Button starButton = new Button();
    starButton.Location = new Point(190, 10);
    starButton.Size = new Size(80, 80);
    starButton.Image = new Bitmap("Star.png");
    starButton.Click += delegate(object sender, EventArgs e) {
      MessageBox.Show("Star!");
    };

    // The form
    Form form = new Form();
    form.Text = "Shape Button Test";
    form.ClientSize = new Size(280, 100);
    form.Controls.Add(ellipseButton);
    form.Controls.Add(triangleButton);
    form.Controls.Add(starButton);
    form.ShowDialog();
  }
}

( ):

Shape Button Test form

+2

winforms - :

button1.Image = new Bitmap(Image.FromFile(@"Pictures\Koala.jpg"));

. , , Click :

private void button1_Click(object sender, EventArgs e)
{
    button1.Image = new Bitmap(Image.FromFile(@"Pictures\Koala.jpg"));
}
+1

pictureBox, , , .

//odd display, even draw
int count = 0;
Image storePicture;

//whenever the background image changed,store it
private void pictureBoxShow_BackgroundImageChanged(object sender, EventArgs e)
{
    //you can stored to a Image array if you have series pictures to show
    storePicture = pictureBoxShow.BackgroundImage;
}

private void buttonControl_Click(object sender, EventArgs e)
{
    count++;
    //odd show picture, even draw picture on button
    if (count % 2 == 1)
        pictureBoxShow.BackgroundImage = new Bitmap("shapes.JPG");
    else
    { 
        //in case you want to clear text on the button
        buttonDrawn.Text = null;
        //recreate the picture so that it fit the button size
        buttonDrawn.Image = new Bitmap(storePicture, new Size(buttonDrawn.Width, buttonDrawn.Height));
    }
}

. ^^

+1

Why not use a PictureBox instead of buttons. you just need to add your task to your event / OnClick Of course, you can upload any image to any PictureBox at runtime

+1
source

The code above is great, however you can click outside the circle in the square containing the circle and get a click event. If you want to capture a click only if the user clicks inside the shape, you must set the region property with something like this

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        GraphicsPath gp = new GraphicsPath();
        gp.AddEllipse(0, 0, 100, 100);
        Button1.Region = new Region(gp);
    }
}
-1
source

Source: https://habr.com/ru/post/1533436/


All Articles