【问题标题】:Button background image changes on mouseover and mousedown eventsmouseover 和 mousedown 事件时按钮背景图像发生变化
【发布时间】:2011-02-24 15:13:19
【问题描述】:

我正在创建一个 Windows 窗体应用程序,我希望 button5 的背景图像在 mouseover 和 mousedown 事件上发生变化。资源中的图像是“1.jpg”和“2.jpg”。 当我将光标悬停在按钮上时,我希望将“2.jpg”作为背景图像,否则将“1.jpg”作为背景图像。
我没有收到任何错误。我使用此代码:

public Form1()
    {
        InitializeComponent();
        button5.MouseEnter += new EventHandler(button5_MouseEnter);
        button5.MouseLeave += new EventHandler(button5_MouseLeave);
    }


    void button5_MouseLeave(object sender, EventArgs e)
    {
        this.button5.BackgroundImage = ((System.Drawing.Image)(Properties.Resources._1));
    }


    void button5_MouseEnter(object sender, EventArgs e)
    {
        this.button5.BackgroundImage = ((System.Drawing.Image)(Properties.Resources._2));
    }

似乎是什么问题?我应该使用其他东西来做到这一点吗?
我也想知道如何完全删除按钮的边框。

非常感谢!
弗拉德

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这样你的代码应该没问题(只要确保你的事件被添加到你的设计器部分类中,而不是在构造函数中)。如果控件没有刷新,可能是表单或者控件很忙,可以尝试使用Refresh()方法刷新按钮。

    要移除按钮的边框,请将控件的FlatStyle 设置为Flat 并将FlatAppearance.BorderSize 设置为0。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多