Label立体字体效果

一.效果图

二.简单谈谈实现思路

   应该说实现方法还是很简单的,就是错位绘制而已.当然,这种效果没有完全体现出字体的立体效果,只是一种错位模仿,在对字体的边框进行描边后,又移动了一下绘制的坐标来体现阴影.感兴趣的朋友应该很容易实现.这里就不多说了.

三.部分源代码

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace JCSControlLibary.AutoSetDocument
{
public partial class JcsBorderLabel : Label
{
private float borderSize = 1;//边框宽度
private Color borderColor = Color.White;//边框颜色
private SizeF drawningSize;
private bool isShowShadow = false;//是否显示阴影
#region Constructor
public JcsBorderLabel()
{
}
#endregion
#region Control Events
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
this.Invalidate();
}
protected override void OnTextAlignChanged(EventArgs e)
{
base.OnTextAlignChanged(e);
this.Invalidate();
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
}
protected override void OnForeColorChanged(EventArgs e)
{
base.OnForeColorChanged(e);
this.Invalidate();
}
#endregion
#region Properties
/// <summary>
/// 边框宽度
/// </summary>
[Browsable(true)]
[Category("Jcs属性")]
[Description("边框宽度")]
[DefaultValue(1F)]
public float BorderSize
{
get { return this.borderSize; }
set
{
this.borderSize = value;
if (value < 0)
{
throw new Exception("无效的值.");
}
else
{
borderSize = value;
}
this.OnTextChanged(EventArgs.Empty);
}
}
/// <summary>
/// 边框颜色
/// </summary>
[Browsable(true)]
[Category("Jcs属性")]
[DefaultValue(typeof(Color), "Black")]
[Description("边框颜色")]
public Color StringBorderColor
{
get { return this.borderColor; }
set
{
this.borderColor = value;
this.Invalidate();
}
}
/// <summary>
/// 是否显示阴影
/// </summary>
[Browsable(true)]
[Category("Jcs属性")]
[DefaultValue(false)]
[Description("是否显示阴影")]
public bool IsShowShadow
{
get { return this.isShowShadow; }
set
{
this.isShowShadow = value;
this.Invalidate();
}
}
#endregion
#region Private Methods
/// <summary>
/// 取得路径中最大宽度与高度的坐标
/// </summary>
private SizeF MeasurePath(GraphicsPath path)
{
SizeF maxSize = new SizeF(0, 0);
foreach (PointF point in path.PathPoints)
{
if (point.X > maxSize.Width)
maxSize.Width = point.X;
if (point.Y > maxSize.Height)
maxSize.Height = point.Y;
}
return maxSize;
}
#endregion
#region Drawnings
protected override void OnPaint(PaintEventArgs e)
{
if (this.Text.Length == 0)
return;
Point drawningPoint = new Point(0, 0);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
using (System.Drawing.Drawing2D.GraphicsPath drawningPath = new GraphicsPath())
{
drawningPath.Reset();
//fontsize是限制绘制字符所在矩形的高度
float fontSize = e.Graphics.DpiY * this.Font.SizeInPoints / 72;
drawningPath.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, fontSize,
new Point(0, 0), StringFormat.GenericTypographic);
//得到要绘制的字符串中应用到指定字符样式后的最宽位置,最高位置
this.drawningSize = MeasurePath(drawningPath);
//将边框宽度与间隔宽度也计算上(高度亦然)
this.drawningSize.Height += this.borderSize + 5;
this.drawningSize.Width += this.borderSize + 5;
if (this.AutoSize)
{
drawningPoint.X = this.Padding.Left;
drawningPoint.Y = this.Padding.Top;
}
else
{
//If text is Left-Aligned
if (this.TextAlign == ContentAlignment.TopLeft ||
this.TextAlign == ContentAlignment.MiddleLeft ||
this.TextAlign == ContentAlignment.BottomLeft)
drawningPoint.X = this.Padding.Left;
else if (this.TextAlign == ContentAlignment.TopCenter ||
this.TextAlign == ContentAlignment.MiddleCenter ||
this.TextAlign == ContentAlignment.BottomCenter)
drawningPoint.X = (this.Width - drawningSize.ToSize().Width) / 2;
else drawningPoint.X = this.Width - (this.Padding.Right + drawningSize.ToSize().Width);
if (this.TextAlign == ContentAlignment.TopLeft ||
this.TextAlign == ContentAlignment.TopCenter ||
this.TextAlign == ContentAlignment.TopRight)
drawningPoint.Y = this.Padding.Top;
else if (this.TextAlign == ContentAlignment.MiddleLeft ||
this.TextAlign == ContentAlignment.MiddleCenter ||
this.TextAlign == ContentAlignment.MiddleRight)
drawningPoint.Y = (this.Height - drawningSize.ToSize().Height) / 2;
//If text is Bottom-Aligned
else drawningPoint.Y = this.Height - (this.Padding.Bottom + drawningSize.ToSize().Height);
}
if (isShowShadow)
{
drawningPath.Reset();
//加入时,指定了调整后的绘制坐标
drawningPath.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, fontSize,
new Point(drawningPoint.X + 3, drawningPoint.Y - 2), StringFormat.GenericTypographic);
using (Pen drawningPen = new Pen(ControlPaint.Dark(this.ForeColor, 100)))
{
//先绘制边框
e.Graphics.DrawPath(drawningPen, drawningPath);
}
}
drawningPath.Reset();
drawningPath.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, fontSize,
drawningPoint, StringFormat.GenericTypographic);
//绘制前景色
using (SolidBrush drawningForecolorBrush = new SolidBrush(this.ForeColor))
{
e.Graphics.FillPath(drawningForecolorBrush, drawningPath);
}
//绘制边框
if (borderSize > 0)
{
using (Pen drawningPen = new Pen(this.borderColor))
{
drawningPen.Width = borderSize;
e.Graphics.DrawPath(drawningPen, drawningPath);
}
}
}
}
#endregion
}
}

 

本文链接:https://my.lmcjl.com/post/8501.html

展开阅读全文

4 评论

留下您的评论.