我们有一个Windows Forms项目,里面有很多FlatStyle按钮。
当我们禁用按钮时,按钮的颜色会自动更改Frown | :(
是否有可能以某种方式覆盖它,所以我们可以自己控制颜色?
答案 0 :(得分:15)
您需要使用EnabledChanged事件来设置所需的颜色。这是一个例子。
private void Button1_EnabledChanged(object sender, System.EventArgs e)
{
Button1.ForeColor = sender.enabled == false ? Color.Blue : Color.Red;
Button1.BackColor = Color.AliceBlue;
}
根据您的要求使用所需的颜色。
此外,您还需要使用绘画事件。
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();
}
答案 1 :(得分:2)
要获得较少模糊的文本,请改用TextRenderer类:
private void Button1_Paint(object sender, PaintEventArgs e)
{
Button btn = (Button)sender;
// make sure Text is not also written on button
btn.Text = string.Empty;
// set flags to center text on button
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak; // center the text
// render the text onto the button
TextRenderer.DrawText(e.Graphics, "Hello", btn.Font, e.ClipRectangle, btn.ForeColor, flags);
}
按照Harsh的回答中的Button1_EnabledChanged方法。
答案 2 :(得分:1)
关于按钮的 EnableChanged 事件
private void button1_EnabledChanged(object sender, EventArgs e)
{
if (button1.Enabled == false)
{
button1.ForeColor = Color.DarkGray; //or pick the color you want when not enabled
}
else
{
button1.ForeColor = Color.White; //same here with the color
}
}
答案 3 :(得分:0)
我遵循以下方法: - 可以使用自定义变量控制按钮的Click()事件。
private bool btnDisabled;
private void btnClick(object sender, EventArgs e){
if(!btnDisabled) return;}
这样,按钮甚至不需要被禁用。按钮仍然具有点击感,但不会采取任何操作。必须使用正确的颜色来表示按钮被禁用。
答案 4 :(得分:0)
我使用ClientRectangle而不是e.ClipRectangle来避免按钮部分重绘时的剪辑效果:
e.Graphics.Clear(BackColor);
using (var drawBrush = new SolidBrush(ForeColor))
using (var sf = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
})
{
e.Graphics.DrawString(Text, Font, drawBrush, ClientRectangle, sf);
}
答案 5 :(得分:-1)
提出建议永远不要太晚
public class BlackButton : Button
{
#region #Private Members
private bool m_BasePaint = false;
#endregion #Private Members
#region #Ctor
public BlackButton() : base()
{
base.ForeColor = Color.White;
base.BackColor = Color.Black;
this.DisabledForeColor = Color.FromArgb(0x6D, 0x6D, 0x6D);
}
#endregion #Ctor
#region #Public Interface
public Color DisabledForeColor
{
get;
set;
}
#endregion #Public Interface
#region #Overrides
public override string Text
{
get
{
if (m_BasePaint)
return "";
return base.Text;
}
set
{
base.Text = value;
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
m_BasePaint = true;
base.OnPaint(pevent);
m_BasePaint = false;
TextFormatFlags flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
TextRenderer.DrawText(pevent.Graphics,
Text,
base.Font,
ClientRectangle,
base.Enabled ? base.ForeColor : this.DisabledForeColor,
flags);
}
#endregion #Overrides
}