我可以在按钮内绘制形状吗?

时间:2014-03-25 23:39:30

标签: c# winforms draw shapes

我一直在阅读stackoverflow一段时间,现在只是为了学习,我遇到的情况是我终于可以问一个问题了。我正在制作一个Simon Says类型记忆游戏,我在那里向用户闪现形状,用户必须按照显示形状的相同顺序单击按钮。我想在他们点击的按钮内绘制我在屏幕上绘制的形状,因为它更容易记住并将形状与形状而不是形状比较为形状名称的按钮。

希望我的问题很明确,谢谢你看看!

5 个答案:

答案 0 :(得分:2)

是的,你可以set the Image property of Button。或者,您可以draw non-rectangular buttons,即任何形状的按钮。以下代码演示了这两种技术:

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

答案 1 :(得分:1)

在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"));
}

答案 2 :(得分: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));
    }
}

请记得将处理程序附加到相应的事件中。 ^^

答案 3 :(得分:1)

为什么不使用PictureBox而不是Buttons。 你只需要将你的任务添加到它的事件/ OnClick 当然,您可以将任何Image加载到运行时的任何PictureBox

答案 4 :(得分:-1)

上面的代码非常棒,但您可以在包含圆圈的正方形内点击圆圈并获取点击事件。 如果您只想在用户点击形状内部时捕获点击,您必须使用类似的内容设置区域属性

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);
    }
}