简体   繁体   English

禁用时更改winform datetimepicker的字体颜色

[英]Change the font color of a winform datetimepicker when disabled

I am creating custom controls that use the standard font color when disabled in a windows form application but am stumped at getting the DateTimePicker to work. 我正在创建在Windows窗体应用程序中禁用时使用标准字体颜色的自定义控件,但为使DateTimePicker正常工作而感到困惑。

I've found a number of answers but they're 10+ years old and no longer seem to work. 我找到了很多答案,但是它们已经有10多年的历史了,似乎不再起作用。 This is a simple answer from 2007, however when I replicate that it works to set the Font on enabled/disabled but the outlying combobox graphic is no longer painted. 是2007年的一个简单答案,但是当我复制它时,它可以将Font设置为enabled/disabled但是外围的组合框图形不再绘制。

Doing some more digging I've found some more complete code to draw the combobox graphics, which combined with the first simple answer I've created the following class: 进行更多的挖掘,我发现了一些更完整的代码来绘制组合框图形,并结合了第一个简单的答案,我创建了以下类:

public partial class CustomDatetimePicker : DateTimePicker
{

    public CustomDatetimePicker()
    {
        InitializeComponent();

        this.SetStyle(ControlStyles.UserPaint, true);
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        //Graphics g = this.CreateGraphics();
        Graphics g = pe.Graphics;

        //The dropDownRectangle defines position and size of dropdownbutton block, 
        //the width is fixed to 17 and height to 16. The dropdownbutton is aligned to right
        Rectangle dropDownRectangle = new Rectangle(ClientRectangle.Width - 17, 0, 17, 16);
        Brush bkgBrush;
        Brush txtBrush;
        ComboBoxState visualState;

        //When the control is enabled the brush is set to Backcolor, 
        //otherwise to color stored in _backDisabledColor
        if (this.Enabled)
        {
            bkgBrush = new SolidBrush(SystemColors.Window);
            txtBrush = new SolidBrush(SystemColors.WindowText);
            visualState = ComboBoxState.Normal;
        }
        else
        {
            bkgBrush = new SolidBrush(SystemColors.InactiveBorder);
            txtBrush = new SolidBrush(SystemColors.WindowText);
            visualState = ComboBoxState.Disabled;
        }

        // Painting...in action

        //Filling the background
        g.FillRectangle(bkgBrush, 0, 0, ClientRectangle.Width, ClientRectangle.Height);

        //Drawing the datetime text
        g.DrawString(this.Text, this.Font, txtBrush, 0, 2);

        //Drawing the dropdownbutton using ComboBoxRenderer
        if (ComboBoxRenderer.IsSupported)
        {
            ComboBoxRenderer.DrawDropDownButton(g, dropDownRectangle, visualState);
        }

        g.Dispose();
        bkgBrush.Dispose();
    }
}

But in my case the ComboBoxRenderer is not supported so it won't paint, and I don't see another easy way to draw it without building a control from scratch. 但是在我的情况下,不支持ComboBoxRenderer因此它不会绘画,而且我看不到另一种简单的绘制方法,而无需从头开始构建控件。

So am I going about this the right way? 所以我要以正确的方式这样做吗? Is there a simple answer like in the first link provided, or with the code provided is there an alternative to using ComboBoxRenderer ? 是否有一个简单的答案(如提供的第一个链接),或者提供了代码,还有使用ComboBoxRenderer的替代方法吗?

I have done something similar, but I didn't think it was necessary to paint the drop-down button when the control was disabled, as it can't be clicked anyway. 我做了类似的事情,但是我认为在禁用控件时不必绘制下拉按钮,因为无论如何不能单击它。 When the control is disabled set the style to UserPaint , otherwise let the control paint itself. 禁用UserPaint ,将样式设置为UserPaint ,否则让控件自己绘制。

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    var graphics        = e.Graphics;
    var clientRectangle = ClientRectangle;
    var font            = Font;
    var text            = Text;
    var textSize        = TextRenderer.MeasureText(text, font);
    var textBounds      = DrawingHelper.AlignInRectangle(clientRectangle, textSize, ContentAlignment.MiddleLeft);

    graphics.FillRectangle(new SolidBrush(SystemColors.Control), ClientRectangle);

    ControlPaint.DrawBorder(graphics, clientRectangle, SystemColors.ControlDark, ButtonBorderStyle.Solid);

    TextRenderer.DrawText(graphics, text, font, textBounds, SystemColors.WindowText);
}

protected override void OnEnabledChanged(EventArgs e)
{
    base.OnEnabledChanged(e);

    SetStyle();
}

public void SetStyle()
{
    if (DesignMode)
        return;

    SetStyle(ControlStyles.UserPaint, !Enabled);

    Invalidate();
}

public static class DrawingHelper
{
    public static Rectangle AlignInRectangle(Rectangle outer, Size inner, ContentAlignment alignment)
    {
        int x = 0;
        int y = 0;

        switch (alignment)
        {
            case ContentAlignment.BottomLeft:
            case ContentAlignment.MiddleLeft:
            case ContentAlignment.TopLeft:
                x = outer.X;
                break;

            case ContentAlignment.BottomCenter:
            case ContentAlignment.MiddleCenter:
            case ContentAlignment.TopCenter:
                x = Math.Max(outer.X + ((outer.Width - inner.Width) / 2), outer.Left);
                break;

            case ContentAlignment.BottomRight:
            case ContentAlignment.MiddleRight:
            case ContentAlignment.TopRight:
                x = outer.Right - inner.Width;
                break;
        }

        switch (alignment)
        {
            case ContentAlignment.TopCenter:
            case ContentAlignment.TopLeft:
            case ContentAlignment.TopRight:
                y = outer.Y;
                break;

            case ContentAlignment.MiddleCenter:
            case ContentAlignment.MiddleLeft:
            case ContentAlignment.MiddleRight:
                y = outer.Y + (outer.Height - inner.Height) / 2;
                break;

            case ContentAlignment.BottomCenter:
            case ContentAlignment.BottomRight:
            case ContentAlignment.BottomLeft:
                y = outer.Bottom - inner.Height;
                break;
        }

        return new Rectangle(x, y, Math.Min(inner.Width, outer.Width), Math.Min(inner.Height, outer.Height));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM