public string CalculateAlternateText(Graphics g, float width) { var actualText = Text; ActualWidth = g.MeasureString(Text, Font, 9999, Format).Width; foreach (var curText in AlternateText.OrderByDescending(x => x.Length)) { if (width < ActualWidth) { actualText = curText; ActualWidth = g.MeasureString(actualText, Font, 9999, Format).Width; } else { break; } } return(actualText); }
public void Draw(Graphics g) { var format = new StringFormat { Alignment = HorizontalAlignment, LineAlignment = VerticalAlignment, FormatFlags = StringFormatFlags.NoWrap, Trimming = StringTrimming.EllipsisCharacter }; if (!IsMonospaced) { var actualText = Text; ActualWidth = g.MeasureString(Text, Font, 9999, format).Width; foreach (var curText in AlternateText.OrderByDescending(x => x.Length)) { if (Width < ActualWidth) { actualText = curText; ActualWidth = g.MeasureString(actualText, Font, 9999, format).Width; } else { break; } } if (HasShadow) { using (var shadowBrush = new SolidBrush(ShadowColor)) { g.DrawString(actualText, Font, shadowBrush, new RectangleF(X + 1, Y + 1, Width, Height), format); g.DrawString(actualText, Font, shadowBrush, new RectangleF(X + 2, Y + 2, Width, Height), format); } } g.DrawString(actualText, Font, Brush, new RectangleF(X, Y, Width, Height), format); } else { var monoFormat = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = VerticalAlignment }; var measurement = TextRenderer.MeasureText(g, "0", Font, new Size((int)(Width + 0.5f), (int)(Height + 0.5f)), TextFormatFlags.NoPadding).Width; var offset = Width; var charIndex = 0; SetActualWidth(g); var cutOffText = CutOff(g); offset = Width - MeasureActualWidth(cutOffText, g); if (HorizontalAlignment != StringAlignment.Far) { offset = 0f; } while (charIndex < cutOffText.Length) { var curOffset = 0f; var curChar = cutOffText[charIndex]; if (char.IsDigit(curChar)) { curOffset = measurement; } else { curOffset = TextRenderer.MeasureText(g, curChar.ToString(), Font, new Size((int)(Width + 0.5f), (int)(Height + 0.5f)), TextFormatFlags.NoPadding).Width; } if (HasShadow) { using (var shadowBrush = new SolidBrush(ShadowColor)) { g.DrawString(curChar.ToString(), Font, shadowBrush, new RectangleF(X + 1 + offset - curOffset * 2f, Y + 1, curOffset * 5f, Height), monoFormat); g.DrawString(curChar.ToString(), Font, shadowBrush, new RectangleF(X + 2 + offset - curOffset * 2f, Y + 2, curOffset * 5f, Height), monoFormat); } } g.DrawString(cutOffText[charIndex].ToString(), Font, Brush, new RectangleF(X + offset - curOffset / 2f, Y, curOffset * 2f, Height), monoFormat); charIndex++; offset += curOffset; } } /*if (HasGlassEffects) * { * var glassBrush = new LinearGradientBrush( * new PointF(0.0f, 0.0f), * new PointF(0.0f, Height), * Color.Transparent, * Color.Transparent); * * var cb = new ColorBlend(); * cb.Positions = new float[] { 0f, 0.5f, 0.55f, 1f }; * var c1 = Color.FromArgb(150, Color.White); * var c2 = Color.FromArgb(100, Color.White); * cb.Colors = new Color[] { c1, c2, Color.FromArgb(255, Color.Black), Color.Transparent }; * * glassBrush.InterpolationColors = cb; * * g.DrawString(cutOffText, Font, glassBrush, new RectangleF(X, Y, Width, Height), format); * }*/ }