TextRenderer.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Drawing;
  2. namespace System.Windows.Forms
  3. {
  4. public static class TextRenderer
  5. {
  6. private static Graphics measureGraphics = Graphics.FromImage(new Bitmap(1, 1));
  7. private static StringFormat GetStringFormat(TextFormatFlags flags)
  8. {
  9. var format = new StringFormat(StringFormat.GenericTypographic);
  10. var rtl = false;
  11. if ((flags & TextFormatFlags.RightToLeft) != 0)
  12. {
  13. format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  14. rtl = true;
  15. }
  16. if ((flags & TextFormatFlags.HorizontalCenter) != 0)
  17. format.Alignment = StringAlignment.Center;
  18. if ((flags & TextFormatFlags.Right) != 0)
  19. format.Alignment = rtl ? StringAlignment.Near : StringAlignment.Far;
  20. if ((flags & TextFormatFlags.VerticalCenter) != 0)
  21. format.LineAlignment = StringAlignment.Center;
  22. if ((flags & TextFormatFlags.Bottom) != 0)
  23. format.LineAlignment = StringAlignment.Far;
  24. if ((flags & TextFormatFlags.EndEllipsis) != 0)
  25. {
  26. format.Trimming = StringTrimming.EllipsisCharacter;
  27. format.FormatFlags |= StringFormatFlags.NoWrap;
  28. }
  29. else
  30. format.Trimming = StringTrimming.Character;
  31. format.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Hide;
  32. format.FormatFlags &= ~StringFormatFlags.LineLimit;
  33. return format;
  34. }
  35. private static void Transform(Graphics g, TextFormatFlags flags)
  36. {
  37. var offset = new PointF(g.Transform.OffsetX, g.Transform.OffsetY);
  38. g.ResetTransform();
  39. if ((flags & TextFormatFlags.PreserveGraphicsTranslateTransform) != 0)
  40. g.TranslateTransform(offset.X, offset.Y);
  41. }
  42. public static void DrawText(Graphics g, string text, Font font, System.Drawing.Point pos, Color foreColor, TextFormatFlags flags)
  43. {
  44. using (StringFormat format = GetStringFormat(flags))
  45. using (SolidBrush brush = new SolidBrush(foreColor))
  46. {
  47. var state = g.Save();
  48. Transform(g, flags);
  49. if ((flags & TextFormatFlags.RightToLeft) != 0)
  50. {
  51. pos.X += MeasureText(text, font).Width - 4;
  52. }
  53. g.DrawString(text, font, brush, pos.X + 2, pos.Y, format);
  54. g.Restore(state);
  55. }
  56. }
  57. public static void DrawText(Graphics g, string text, Font font, System.Drawing.Point pos, Color foreColor) => DrawText(g, text, font, pos, foreColor, TextFormatFlags.Default);
  58. public static void DrawText(Graphics g, string text, Font font, Rectangle rect, Color foreColor, TextFormatFlags flags)
  59. {
  60. using (StringFormat format = GetStringFormat(flags))
  61. using (SolidBrush brush = new SolidBrush(foreColor))
  62. {
  63. var state = g.Save();
  64. Transform(g, flags);
  65. g.DrawString(text, font, brush, new Rectangle(rect.X + 2, rect.Y, rect.Width - 4, rect.Height), format);
  66. g.Restore(state);
  67. }
  68. }
  69. public static void DrawText(Graphics g, string text, Font font, Rectangle rect, Color foreColor) => DrawText(g, text, font, rect, foreColor, TextFormatFlags.Default);
  70. public static void DrawText(Graphics g, string text, Font font, Rectangle rect, Color foreColor, Color backColor, TextFormatFlags flags) => DrawText(g, text, font, rect, foreColor, flags);
  71. public static System.Drawing.Size MeasureText(string text, Font font) => MeasureString(text, font).ToSize();
  72. public static System.Drawing.Size MeasureText(string text, Font font, Drawing.Size size) => MeasureString(text, font, size.Width, StringFormat.GenericTypographic).ToSize();
  73. public static SizeF MeasureString(string text, Font font) => measureGraphics.MeasureString(text, font);
  74. public static SizeF MeasureString(string text, Font font, int maxWidth, StringFormat format) => measureGraphics.MeasureString(text, font, maxWidth, format);
  75. }
  76. [Flags]
  77. public enum TextFormatFlags
  78. {
  79. Bottom = 0x8,
  80. EndEllipsis = 0x8000,
  81. ExpandTabs = 0x40,
  82. ExternalLeading = 0x200,
  83. Default = 0x0,
  84. HidePrefix = 0x100000,
  85. HorizontalCenter = 0x1,
  86. Internal = 0x1000,
  87. Left = 0x0,
  88. ModifyString = 0x10000,
  89. NoClipping = 0x100,
  90. NoPrefix = 0x800,
  91. NoFullWidthCharacterBreak = 0x80000,
  92. PathEllipsis = 0x4000,
  93. PrefixOnly = 0x200000,
  94. Right = 0x2,
  95. RightToLeft = 0x20000,
  96. SingleLine = 0x20,
  97. TextBoxControl = 0x2000,
  98. Top = 0x0,
  99. VerticalCenter = 0x4,
  100. WordBreak = 0x10,
  101. WordEllipsis = 0x40000,
  102. PreserveGraphicsClipping = 0x1000000,
  103. PreserveGraphicsTranslateTransform = 0x2000000,
  104. GlyphOverhangPadding = 0x0,
  105. NoPadding = 0x10000000,
  106. LeftAndRightPadding = 0x20000000
  107. }
  108. }