You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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();
}
/// <summary>
/// 获取文字对齐方式
/// </summary>
/// <returns></returns>
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;
}
}
}