C# 禁用按钮时如何避免颜色变化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18717090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:00:28  来源:igfitidea点击:

How to avoid color changes when button is disabled?

c#winformsbuttoncolorsbackcolor

提问by user2500179

We have a Windows Forms project with quite a few FlatStyle buttons.

我们有一个带有很多 FlatStyle 按钮的 Windows 窗体项目。

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?

是否可以以某种方式覆盖它,以便我们可以自己控制颜色?

采纳答案by Harsh

You need to use the EnabledChanged event to set the desired color. Here is an example.

您需要使用 EnabledChanged 事件来设置所需的颜色。这是一个例子。

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.

您还需要使用paint事件。

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();

}

回答by radiata1891

To get less-fuzzy text, use the TextRenderer class instead:

要获得不那么模糊的文本,请改用 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);
        }

And the Button1_EnabledChanged method as in Harsh's answer.

以及 Button1_EnabledChanged 方法,如 Harsh 的回答。

回答by girishkatta9

I followed the following approach :- The Click() event of the button can be controlled using custom variable.

我遵循以下方法:- 可以使用自定义变量控制按钮的 Click() 事件。

private bool btnDisabled;
private void btnClick(object sender, EventArgs e){
   if(!btnDisabled) return;}

This way the button doesn't even need to be disabled. The button still has the click feel but no action will be taken. Have to use the right colors to communicate that the button is disabled.

这样按钮甚至不需要被禁用。该按钮仍然有点击的感觉,但不会采取任何行动。必须使用正确的颜色来传达按钮已禁用。

回答by Mathieu Cans

I use ClientRectangle instead of e.ClipRectangle to avoid clip effect when the button is partialy repaint :

当按钮部分重绘时,我使用 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);
}

回答by Vasiliy Sorokin

Never too late for a suggestion:

提出建议永远不会太晚:

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
}