【问题标题】:C# Button.imageC# 按钮.image
【发布时间】:2013-12-16 22:04:41
【问题描述】:

我在这方面已经有一段时间了,但无法让它发挥作用。 我来自 Unity3D 中的 C# 编程,但现在需要通过 Visual Studio 用“普通”C# 构建一些东西。

所以我在 C# 中有这个按钮,我想给它一个背景。没什么大不了的。 但问题是,我希望背景能够自行扩展。 所以我想要一个像这样的图像:!http://i39.tinypic.com/2wphvo6.png 结合一个按钮,所以它变成这样:!http://i42.tinypic.com/dzxlom.jpg

在 Unity3D 中这很容易,但我不知道如何让它在 Visual Studio C# 中工作。希望可以有人帮帮我。我什至不知道如何正确将此图像扩展为按钮...

编辑:Windows 窗体

【问题讨论】:

  • 如果您告诉我们“正常”是指 WPF 还是 Windows 窗体会有所帮助,因为答案会完全不同。
  • 抱歉信息不足,我的意思是 Windows 窗体 :)

标签: c# image button background


【解决方案1】:

假设你想改变鼠标悬停的图片,你可以使用事件来做这样的事情

    public Form1()
                  {
                       InitializeComponent();
                       button1.MouseEnter += new EventHandler(button1_MouseEnter);
                       button1.MouseLeave += new EventHandler(button1_MouseLeave);
                  }

                  void button1_MouseLeave(object sender, EventArgs e)
                  {
                       this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img1));
                       this.button1.Size = button1.BackgroundImage.Size;
                  }


                  void button1_MouseEnter(object sender, EventArgs e)
                  {
                       this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.img2));
                       this.button1.Size = button1.BackgroundImage.Size;
                  }

【讨论】:

  • 对我来说,这只是用这些图像填​​充按钮。如,它被平铺。我正在这样做: Image im = Image.FromFile(@"C:\ProjectSilver\assets\RadarDetectie\register\btn_blue_normal.png"); buttonArray[i].BackgroundImage = ((System.Drawing.Image)(im));我不想将按钮调整为图像,我希望图像调整为按钮。
【解决方案2】:

您需要设置BackgroundImageLayout 属性。

public Form1()
{
    InitializeComponent();
    button1.BackgroundImageLayout = ImageLayout.Stretch; //Or maybe you want Zoom
}

您也可以在表单的设计视图中设置属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2014-08-23
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多