LabelLine.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using FastReport.Utils;
  6. namespace FastReport.Controls
  7. {
  8. /// <summary>
  9. /// Represents the label with line.
  10. /// </summary>
  11. [ToolboxItem(false)]
  12. public class LabelLine : Control
  13. {
  14. /// <inheritdoc/>
  15. protected override void OnPaint(PaintEventArgs e)
  16. {
  17. Graphics g = e.Graphics;
  18. #if AVALONIA
  19. TextRenderer.FontScale = g.FontScale;
  20. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  21. #endif
  22. using (Pen pen = new Pen(Color.Silver, this.LogicalToDevice(1)))
  23. {
  24. int x = 0;
  25. if (RightToLeft == RightToLeft.Yes)
  26. {
  27. if (!String.IsNullOrEmpty(Text))
  28. {
  29. x = Width - TextRenderer.MeasureText(Text, Font).Width;
  30. TextRenderer.DrawText(g, Text, Font, new Point(x, 0), ForeColor, TextFormatFlags.RightToLeft);
  31. x -= this.LogicalToDevice(4);
  32. }
  33. g.DrawLine(pen, 0, Height / 2, x, Height / 2);
  34. }
  35. else
  36. {
  37. if (!String.IsNullOrEmpty(Text))
  38. {
  39. TextRenderer.DrawText(g, Text, Font, new Point(0, 0), ForeColor);
  40. x += TextRenderer.MeasureText(Text, Font).Width + this.LogicalToDevice(4);
  41. }
  42. g.DrawLine(pen, x, Height / 2, Width, Height / 2);
  43. }
  44. }
  45. }
  46. /// <summary>
  47. /// Initializes a new instance of the <see cref="LabelLine"/> class.
  48. /// </summary>
  49. public LabelLine()
  50. {
  51. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  52. SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  53. SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  54. }
  55. }
  56. }