using LYFZ.OtherExpansion.SkinClass; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace LYFZ.OtherExpansion.SkinControl { [ToolboxBitmap(typeof(Label))] public class SkinLabel : Label { private ArtTextStyle _artTextStyle = ArtTextStyle.Border; private int _borderSize = 1; private Color _borderColor = Color.White; [Browsable(true), Category("Skin"), DefaultValue(typeof(ArtTextStyle), "1"), Description("字体样式(None:正常样式,Border:边框样式,Relievo:浮雕样式,Forme:印版样式,Anamorphosis:渐变样式)")] public ArtTextStyle ArtTextStyle { get { return this._artTextStyle; } set { if (this._artTextStyle != value) { this._artTextStyle = value; base.Invalidate(); } } } [Browsable(true), Category("Skin"), DefaultValue(1), Description("样式效果宽度")] public int BorderSize { get { return this._borderSize; } set { if (this._borderSize != value) { this._borderSize = value; base.Invalidate(); } } } [Browsable(true), Category("Skin"), DefaultValue(typeof(Color), "80, 0, 0, 0"), Description("样式效果颜色")] public Color BorderColor { get { return this._borderColor; } set { if (this._borderColor != value) { this._borderColor = value; base.Invalidate(); } } } public SkinLabel() { this.SetStyles(); this.Font = new Font("微软雅黑", 9f, FontStyle.Regular, GraphicsUnit.Point, 134); } protected override void OnPaint(PaintEventArgs e) { if (this.ArtTextStyle == ArtTextStyle.None) { base.OnPaint(e); return; } if (base.Text.Length == 0) { return; } this.RenderText(e.Graphics); } public string SetStrLeng(string txt, Font font, int width) { Size sizef = TextRenderer.MeasureText(txt, font); while (sizef.Width > width && txt.Length != 0) { txt = txt.Substring(0, txt.Length - 1); sizef = TextRenderer.MeasureText(txt, font); } return txt; } private void RenderText(Graphics g) { using (new TextRenderingHintGraphics(g)) { PointF point = this.CalculateRenderTextStartPoint(g); switch (this._artTextStyle) { case ArtTextStyle.Border: this.RenderBordText(g, point); break; case ArtTextStyle.Relievo: this.RenderRelievoText(g, point); break; case ArtTextStyle.Forme: this.RenderFormeText(g, point); break; case ArtTextStyle.Anamorphosis: this.RenderAnamorphosisText(g, point); break; } } } private void RenderAnamorphosisText(Graphics g, PointF point) { using (new SolidBrush(base.ForeColor)) { Rectangle rc = new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)), base.ClientRectangle.Size); Image img = UpdateForm.ImageLightEffect(this.Text, base.Font, this.ForeColor, this.BorderColor, this.BorderSize, rc, !this.AutoSize); g.DrawImage(img, point.X - (float)(this.BorderSize / 2), point.Y - (float)(this.BorderSize / 2)); } } private void RenderFormeText(Graphics g, PointF point) { StringFormat sf = new StringFormat(StringFormatFlags.NoWrap); sf.Trimming = (this.AutoSize ? StringTrimming.None : StringTrimming.EllipsisWord); Rectangle rc = new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)), base.ClientRectangle.Size); using (Brush brush = new SolidBrush(this._borderColor)) { for (int i = 1; i <= this._borderSize; i++) { g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X - (float)i), Convert.ToInt32(point.Y + (float)i)), base.ClientRectangle.Size), sf); } } using (Brush brush2 = new SolidBrush(base.ForeColor)) { g.DrawString(this.Text, this.Font, brush2, rc, sf); } } private void RenderRelievoText(Graphics g, PointF point) { StringFormat sf = new StringFormat(StringFormatFlags.NoWrap); sf.Trimming = (this.AutoSize ? StringTrimming.None : StringTrimming.EllipsisWord); Rectangle rc = new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)), base.ClientRectangle.Size); using (Brush brush = new SolidBrush(this._borderColor)) { for (int i = 1; i <= this._borderSize; i++) { g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X + (float)i), Convert.ToInt32(point.Y)), base.ClientRectangle.Size), sf); g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y + (float)i)), base.ClientRectangle.Size), sf); } } using (Brush brush2 = new SolidBrush(base.ForeColor)) { g.DrawString(this.Text, base.Font, brush2, rc, sf); } } private void RenderBordText(Graphics g, PointF point) { StringFormat sf = new StringFormat(StringFormatFlags.NoWrap); sf.Trimming = (this.AutoSize ? StringTrimming.None : StringTrimming.EllipsisWord); Rectangle rc = new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y)), base.ClientRectangle.Size); using (Brush brush = new SolidBrush(this._borderColor)) { for (int i = 1; i <= this._borderSize; i++) { g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X - (float)i), Convert.ToInt32(point.Y)), base.ClientRectangle.Size), sf); g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y - (float)i)), base.ClientRectangle.Size), sf); g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X + (float)i), Convert.ToInt32(point.Y)), base.ClientRectangle.Size), sf); g.DrawString(this.Text, base.Font, brush, new Rectangle(new Point(Convert.ToInt32(point.X), Convert.ToInt32(point.Y + (float)i)), base.ClientRectangle.Size), sf); } } using (Brush brush2 = new SolidBrush(base.ForeColor)) { g.DrawString(this.Text, base.Font, brush2, rc, sf); } } private PointF CalculateRenderTextStartPoint(Graphics g) { PointF point = PointF.Empty; SizeF textSize = g.MeasureString(base.Text, base.Font, PointF.Empty, StringFormat.GenericTypographic); if (this.AutoSize) { point.X = (float)base.Padding.Left; point.Y = (float)base.Padding.Top; } else { ContentAlignment align = base.TextAlign; if (align == ContentAlignment.TopLeft || align == ContentAlignment.MiddleLeft || align == ContentAlignment.BottomLeft) { point.X = (float)base.Padding.Left; } else { if (align == ContentAlignment.TopCenter || align == ContentAlignment.MiddleCenter || align == ContentAlignment.BottomCenter) { point.X = ((float)base.Width - textSize.Width) / 2f; } else { point.X = (float)base.Width - ((float)base.Padding.Right + textSize.Width); } } if (align == ContentAlignment.TopLeft || align == ContentAlignment.TopCenter || align == ContentAlignment.TopRight) { point.Y = (float)base.Padding.Top; } else { if (align == ContentAlignment.MiddleLeft || align == ContentAlignment.MiddleCenter || align == ContentAlignment.MiddleRight) { point.Y = ((float)base.Height - textSize.Height) / 2f; } else { point.Y = (float)base.Height - ((float)base.Padding.Bottom + textSize.Height); } } } return point; } private void SetStyles() { base.SetStyle(ControlStyles.ResizeRedraw, true); base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); base.SetStyle(ControlStyles.AllPaintingInWmPaint, true); base.SetStyle(ControlStyles.UserPaint, true); base.SetStyle(ControlStyles.SupportsTransparentBackColor, true); base.ResizeRedraw = true; this.BackColor = Color.Transparent; base.UpdateStyles(); } } }