如何从禁用按钮中删除黑色边框?
How to remove the black border from the disabled button?
我创建了一个带有背景图片的 changing_button。该按钮的FlatAppearance.BorderSize
属性为零,所以正常显示时无边框。但是如果我禁用 changing_button
,它将有一个黑色边框:
如何删除此边框?
我猜边框出现是因为 changing_button
处于焦点,然后我更改了它的启用 属性。出于这个原因,我试图从按钮上移除焦点并设置 changing_button.TabStop = false
,但它没有帮助。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
private readonly Image enable_img = Image.FromFile("D://images//enable_img.png");
private readonly Image disable_img = Image.FromFile("D://images//disable_img.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
CreateButton();
Controls.Add(changing_button);
}
private void CreateButton()
{
changing_button = new Button
{
BackgroundImage = enable_img,
BackgroundImageLayout = ImageLayout.Center,
TabIndex = 1,
TabStop = false,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(10, 10, 0, 0),
Location = new Point(40, 40),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.Size = new Size(80, 50);
changing_button.Click += new System.EventHandler(this.Button_Click);
}
void Button_Click(object sender, EventArgs e)
{
changing_button.BackgroundImage = disable_img;
changing_button.Enabled = false;
changing_button.TabStop = false;
}
}
}
enable_img.png:
disable_img.png:
为了解决这个问题,我将 border
大小设置为 1。
然后将border
颜色设置为与BackColor
相同的颜色。只需更改点击事件以匹配当前状态。
我创建了一个带有背景图片的 changing_button。该按钮的FlatAppearance.BorderSize
属性为零,所以正常显示时无边框。但是如果我禁用 changing_button
,它将有一个黑色边框:
如何删除此边框?
我猜边框出现是因为 changing_button
处于焦点,然后我更改了它的启用 属性。出于这个原因,我试图从按钮上移除焦点并设置 changing_button.TabStop = false
,但它没有帮助。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace winforms_test_1
{
public partial class Form1 : Form
{
Button changing_button;
private readonly Image enable_img = Image.FromFile("D://images//enable_img.png");
private readonly Image disable_img = Image.FromFile("D://images//disable_img.png");
public Form1()
{
InitializeComponent();
TableLayoutPanel main_panel = new TableLayoutPanel
{
BackColor = Color.White,
Dock = DockStyle.Fill
};
CreateButton();
Controls.Add(changing_button);
}
private void CreateButton()
{
changing_button = new Button
{
BackgroundImage = enable_img,
BackgroundImageLayout = ImageLayout.Center,
TabIndex = 1,
TabStop = false,
FlatStyle = FlatStyle.Flat,
Margin = new Padding(10, 10, 0, 0),
Location = new Point(40, 40),
};
changing_button.FlatAppearance.BorderSize = 0;
changing_button.Size = new Size(80, 50);
changing_button.Click += new System.EventHandler(this.Button_Click);
}
void Button_Click(object sender, EventArgs e)
{
changing_button.BackgroundImage = disable_img;
changing_button.Enabled = false;
changing_button.TabStop = false;
}
}
}
enable_img.png:
为了解决这个问题,我将 border
大小设置为 1。
然后将border
颜色设置为与BackColor
相同的颜色。只需更改点击事件以匹配当前状态。