以下方法是winform标签继承类的一部分。目的是对文本产生光晕或发光效果。到目前为止,效果实际上只是轮廓。它值得保留,因为它还不错,但还没有到那儿。
protected override void OnPaint(PaintEventArgs e)
{
if (this.Text.Length == 0) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
Rectangle rc = new Rectangle(ClientRectangle.X + Padding.Left,
ClientRectangle.Y + Padding.Top,
ClientRectangle.Width - (Padding.Left + Padding.Right),
ClientRectangle.Height - (Padding.Top + Padding.Bottom));
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic)
{
Alignment = TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft
? StringAlignment.Near
: TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter
? StringAlignment.Center
: StringAlignment.Far
};
if ((haloSize < 1) | (haloColor == Color.Transparent))
{
using (var brush = new SolidBrush(this.ForeColor))
{
e.Graphics.DrawString(Text, Font, brush, rc, fmt);
}
}
else
{
using (var path = new GraphicsPath())
using (var halopen = new Pen(new SolidBrush(this.haloColor), haloSize) { LineJoin = LineJoin.Round })
using (var brush = new SolidBrush(ForeColor))
{
if (fmt.Alignment == StringAlignment.Center) rc.X += rc.Width / 2;
if (fmt.Alignment == StringAlignment.Far) rc.X += rc.Width;
path.AddString(Text, Font.FontFamily, (int)Font.Style, rc.Height, rc.Location, fmt);
//path.AddString(Text, Font.FontFamily, (int)Font.Style, rc.Height, rc, fmt);
e.Graphics.DrawPath(halopen, path);
e.Graphics.FillPath(brush, path);
}
}
}
两个问题。
AddString
和Rectangle
注释掉的行不起作用,但是却与Point
一起起作用?辉光/光晕效果可为恰好与文本基色匹配的背景图像提供良好的对比度。这主要是针对目标,但对于标签太长的文本则不起作用。我可能会在以后添加。
protected override void OnPaint(PaintEventArgs e)
{
if (this.Text.Length == 0) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.CompositingMode = CompositingMode.SourceOver;
Rectangle rc = new Rectangle(ClientRectangle.X + Padding.Left,
ClientRectangle.Y + Padding.Top,
ClientRectangle.Width - (Padding.Left + Padding.Right),
ClientRectangle.Height - (Padding.Top + Padding.Bottom));
StringFormat fmt = new StringFormat(StringFormat.GenericTypographic)
{
Alignment = TextAlign == ContentAlignment.TopLeft || TextAlign == ContentAlignment.MiddleLeft || TextAlign == ContentAlignment.BottomLeft
? StringAlignment.Near
: TextAlign == ContentAlignment.TopCenter || TextAlign == ContentAlignment.MiddleCenter || TextAlign == ContentAlignment.BottomCenter
? StringAlignment.Center
: StringAlignment.Far
};
float EmSize = rc.Height * 0.70f //Font.SizeInPoints disreguard fontsize and fit height of label
* (Font.FontFamily.GetCellAscent(Font.Style) + Font.FontFamily.GetCellDescent(Font.Style))
/ Font.FontFamily.GetEmHeight(Font.Style);
using (var path = new GraphicsPath())
using (var brush = new SolidBrush(ForeColor))
{
path.AddString(Text, Font.FontFamily, (int)Font.Style, EmSize, rc, fmt);
if ((haloSize > 0) & (haloColor != Color.Transparent))
{
using (var halopen = new Pen(new SolidBrush(Color.FromArgb(255 / (int)haloSize, haloColor)), haloSize) { LineJoin = LineJoin.Round })
{
for (int i = (int)haloSize; i > 0; --i)
{
halopen.Width = i;
e.Graphics.DrawPath(halopen, path);
}
}
}
e.Graphics.FillPath(brush, path);
}
}