using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing.Text; namespace JwKdsV.Component { public partial class RichLabel : Label { public RichLabel() { InitializeComponent(); } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Color nearestColor = e.Graphics.GetNearestColor(base.Enabled ? this.ForeColor : SystemColors.Control); string[] strArr = this.Text.Split(new char[2] { ':', ':' }, StringSplitOptions.RemoveEmptyEntries); StringFormat format = this.CreateStringFormat2(); if (this.Enabled) { using (Brush brush = new SolidBrush(System.Drawing.Color.Red)) { e.Graphics.DrawString(strArr[0], this.Font, brush, base.ClientRectangle, format); } SizeF size = e.Graphics.MeasureString(strArr[0], this.Font); //计算已经画了字符串的大小 计算已经画了字符串的大小; //计算剩余空间 System.Drawing.Rectangle rectangle = e.ClipRectangle; rectangle.Location = new Point(e.ClipRectangle.Location.X + (int)size.Width, e.ClipRectangle.Location.Y); using (Brush brush = new SolidBrush(nearestColor)) { e.Graphics.DrawString(Text.Substring(strArr.Length), this.Font, brush, rectangle, format); } } else { ControlPaint.DrawStringDisabled(e.Graphics, this.Text, this.Font, nearestColor, base.ClientRectangle, format); } format.Dispose(); } /// /// 获取文字对齐方式 /// /// private StringFormat CreateStringFormat2() { StringFormat format = StringFormat.GenericDefault; if (this.RightToLeft == RightToLeft.Yes) { format.FormatFlags |= StringFormatFlags.DirectionRightToLeft; } if (!this.UseMnemonic) { format.HotkeyPrefix = HotkeyPrefix.None; } else if (base.ShowKeyboardCues) { format.HotkeyPrefix = HotkeyPrefix.Show; } else { format.HotkeyPrefix = HotkeyPrefix.Hide; } if (this.AutoSize) { format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces; } return format; } } }