HeaderLabel.cs 3.7 KB

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