簡體   English   中英

禁用時更改winform datetimepicker的字體顏色

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

我正在創建在Windows窗體應用程序中禁用時使用標准字體顏色的自定義控件,但為使DateTimePicker正常工作而感到困惑。

我找到了很多答案,但是它們已經有10多年的歷史了,似乎不再起作用。 是2007年的一個簡單答案,但是當我復制它時,它可以將Font設置為enabled/disabled但是外圍的組合框圖形不再繪制。

進行更多的挖掘,我發現了一些更完整的代碼來繪制組合框圖形,並結合了第一個簡單的答案,我創建了以下類:

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

但是在我的情況下,不支持ComboBoxRenderer因此它不會繪畫,而且我看不到另一種簡單的繪制方法,而無需從頭開始構建控件。

所以我要以正確的方式這樣做嗎? 是否有一個簡單的答案(如提供的第一個鏈接),或者提供了代碼,還有使用ComboBoxRenderer的替代方法嗎?

我做了類似的事情,但是我認為在禁用控件時不必繪制下拉按鈕,因為無論如何不能單擊它。 禁用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