c#TextBox控件Enabled為false後設定控件字體顏色

首先自己重寫TextBox控件

然後在控件裡寫入下面代碼:

 protected override void OnPaint(PaintEventArgs args)
        {
            base.OnPaint(args);
            if (!Enabled)
            {
                args.Graphics.DrawString(Text, Font, new SolidBrush(Color.DimGray), new PointF(0.0F, 0.0F));
            }
        }

        protected override void OnEnabledChanged(EventArgs e)
        {
            if (Enabled)
            {
                this.SetStyle(ControlStyles.UserPaint, false);
                this.Font = new System.Drawing.Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit);
            }
            else
            {
                this.SetStyle(ControlStyles.UserPaint, true);
            }

            base.OnEnabledChanged(e);
        }

搞定.主要一句this.SetStyle(ControlStyles.UserPaint, true);

你可能感兴趣的:(C#Winform,.NET)