HeaderLabel.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using SmartCoalApplication.SystemLayer;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace SmartCoalApplication.Core
  11. {
  12. public sealed class HeaderLabel
  13. : Control
  14. {
  15. private const TextFormatFlags textFormatFlags =
  16. TextFormatFlags.Default |
  17. TextFormatFlags.EndEllipsis |
  18. TextFormatFlags.HidePrefix |
  19. TextFormatFlags.NoPadding |
  20. TextFormatFlags.NoPrefix |
  21. TextFormatFlags.SingleLine;
  22. private int leftMargin = 2;
  23. private int rightMargin = 8;
  24. private EtchedLine etchedLine;
  25. [DefaultValue(8)]
  26. public int RightMargin
  27. {
  28. get
  29. {
  30. return this.rightMargin;
  31. }
  32. set
  33. {
  34. this.rightMargin = value;
  35. PerformLayout();
  36. }
  37. }
  38. protected override void OnFontChanged(EventArgs e)
  39. {
  40. PerformLayout();
  41. Refresh();
  42. base.OnFontChanged(e);
  43. }
  44. protected override void OnTextChanged(EventArgs e)
  45. {
  46. PerformLayout();
  47. Refresh();
  48. base.OnTextChanged(e);
  49. }
  50. public HeaderLabel()
  51. {
  52. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  53. SetStyle(ControlStyles.Opaque, true);
  54. SetStyle(ControlStyles.ResizeRedraw, true);
  55. SetStyle(ControlStyles.UserPaint, true);
  56. SetStyle(ControlStyles.Selectable, false);
  57. UI.InitScaling(null);
  58. TabStop = false;
  59. ForeColor = SystemColors.Highlight;
  60. DoubleBuffered = true;
  61. ResizeRedraw = true;
  62. SuspendLayout();
  63. this.etchedLine = new EtchedLine();
  64. Controls.Add(this.etchedLine);
  65. Size = new Size(144, 14);
  66. ResumeLayout(false);
  67. }
  68. private int GetPreferredWidth(Size proposedSize)
  69. {
  70. Size textSize = GetTextSize();
  71. return this.leftMargin + textSize.Width;
  72. }
  73. public override Size GetPreferredSize(Size proposedSize)
  74. {
  75. return new Size(Math.Max(proposedSize.Width, GetPreferredWidth(proposedSize)), GetTextSize().Height);
  76. }
  77. private Size GetTextSize()
  78. {
  79. string textToUse = string.IsNullOrEmpty(Text) ? " " : Text;
  80. Size size = TextRenderer.MeasureText(textToUse, this.Font, this.ClientSize, textFormatFlags);
  81. if (string.IsNullOrEmpty(Text))
  82. {
  83. size.Width = 0;
  84. }
  85. return size;
  86. }
  87. protected override void OnLayout(LayoutEventArgs levent)
  88. {
  89. Size textSize = GetTextSize();
  90. int lineLeft = (string.IsNullOrEmpty(this.Text) ? 0 : this.leftMargin) + textSize.Width + (string.IsNullOrEmpty(this.Text) ? 0 : 1);
  91. int lineRight = ClientRectangle.Right - this.rightMargin;
  92. this.etchedLine.Size = this.etchedLine.GetPreferredSize(new Size(lineRight - lineLeft, 1));
  93. this.etchedLine.Location = new Point(lineLeft, (ClientSize.Height - this.etchedLine.Height) / 2);
  94. base.OnLayout(levent);
  95. }
  96. protected override void OnPaint(PaintEventArgs e)
  97. {
  98. using (SolidBrush backBrush = new SolidBrush(BackColor))
  99. {
  100. e.Graphics.FillRectangle(backBrush, e.ClipRectangle);
  101. }
  102. Size textSize = GetTextSize();
  103. TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Point(this.leftMargin, 0), SystemColors.WindowText, textFormatFlags);
  104. base.OnPaint(e);
  105. }
  106. }
  107. }