【问题标题】:Can I draw shapes within buttons?我可以在按钮内绘制形状吗?
【发布时间】:2014-03-25 23:39:30
【问题描述】:

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

希望我的问题很清楚,感谢您的关注!

【问题讨论】:

  • c# 对按钮一无所知。它是 winforms 或 wpf 或 asp.net 还是其他任何应用程序?
  • 为按钮使用背景
  • @zerkms 对不起,winforms。

标签: c# winforms draw shapes


【解决方案1】:

是的,您可以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();
  }
}

结果(点击三角按钮后):

【讨论】:

    【解决方案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"));
    }
    

    【讨论】:

      【解决方案3】:

      在我的例子中,

      我有一个图片框要显示,两个按钮,一个用于控制,一个用于绘制。

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

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            猜你喜欢
            • 2013-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-12
            • 1970-01-01
            • 2022-07-01
            • 1970-01-01
            • 2012-04-24
            相关资源
            最近更新 更多