對不起我的英語不好。這是我第一次用這種語言寫問題。如果你糾正我的語法錯誤,我會很高興。
我創建一個changing_button并設定它的 BackgroundImage 屬性。當游標懸停在此按鈕上時,我希望此影像發生變化。為此我拍了兩張照片。
blue_image.png: 在此處輸入影像描述
red_image.png: 在此處輸入影像描述
通常changing_button應該顯示 red_image。但是如果游標在這個按鈕上,它應該顯示 blue_image。問題是當我嘗試覆寫 BackgroundImage 屬性時 blue_image 發生變化:在此處輸入影像描述
我怎樣才能解決這個問題?
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
Image blue_image = Image.FromFile("D://images//blue_image.png");
Image red_image = Image.FromFile("D://images//red_image.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
changing_button = new Button
{
BackgroundImage = red_image,
BackgroundImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.MouseEnter = new System.EventHandler(this.test_button_MouseEnter);
changing_button.MouseLeave = new System.EventHandler(this.test_button_MouseLeave);
main_panel.Controls.Add(changing_button, 0, 0);
Controls.Add(main_panel);
}
void test_button_MouseEnter(object sender, EventArgs e)
{
changing_button.Image = blue_image;
}
void test_button_MouseLeave(object sender, EventArgs e)
{
changing_button.Image = red_image;
}
}
}
uj5u.com熱心網友回復:
將初始影像設定為紅色影像,而不是更改背景影像。BackgroundImage 和 Image 看起來它們的位置略有不同。
changing_button = new Button
{
Image = red_image,
ImageLayout = ImageLayout.Center,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(0, 0, 0, 0),
Size = new Size(50, 50),
};
使用 BackgroundImage 或 Image,但不能同時使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/450314.html
