本文介绍了如何避免颜色变化,当按钮被禁用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们有相当多的FlatStyle按钮的Windows窗体的项目。
We have a Windows Forms project with quite a few FlatStyle buttons.
当我们禁用按钮,按钮的颜色将自动更改皱眉| (
When we disable the buttons, the colors of the buttons are changed automatically Frown | :(
是否有可能以某种方式重写此,让我们可以控制自己的颜色?
Is it possible to override this somehow, so we can control the colors ourselves?
推荐答案
您需要使用EnabledChanged事件来设置所需的颜色。下面是一个例子。
You need to use the EnabledChanged event to set the desired color. Here is an example.
private void Button1_EnabledChanged(object sender, System.EventArgs e)
{
Button1.ForeColor = sender.enabled == false ? Color.Blue : Color.Red;
Button1.BackColor = Color.AliceBlue;
}
根据您的要求使用所需的颜色。
Use the desired colors according to your requirement.
此外,你需要使用油漆的事件。
Also you need to use the paint event.
private void Button1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
dynamic btn = (Button)sender;
dynamic drawBrush = new SolidBrush(btn.ForeColor);
dynamic sf = new StringFormat {
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center };
Button1.Text = string.Empty;
e.Graphics.DrawString("Button1", btn.Font, drawBrush, e.ClipRectangle, sf);
drawBrush.Dispose();
sf.Dispose();
}
这篇关于如何避免颜色变化,当按钮被禁用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!