重写textbox控件的enabled属性,修改enabled=false时字体颜色
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); }
绘制都是从左往右进行的,没办法从右到左绘。换个思路,在OnPaint事件中把字进行一下处理,左右调换
protected override void OnPaint(PaintEventArgs args) { base.OnPaint(args); if (!Enabled) { StringFormat TitleFormat = new StringFormat(); if (this.TextAlign == HorizontalAlignment.Center) { TitleFormat.Alignment = TitleFormat.LineAlignment = StringAlignment.Center; } else if (this.TextAlign == HorizontalAlignment.Right) { TitleFormat.Alignment = TitleFormat.LineAlignment = StringAlignment.Far; } else { TitleFormat.Alignment = TitleFormat.LineAlignment = StringAlignment.Near; } args.Graphics.DrawString(Text, Font, new SolidBrush(Color.Red), args.ClipRectangle, TitleFormat); } }