EtchedLine.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using PaintDotNet.SystemLayer;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Windows.Forms.VisualStyles;
  6. namespace PaintDotNet
  7. {
  8. public sealed class EtchedLine
  9. : Control
  10. {
  11. private bool selfDrawn = false;
  12. private Label label;
  13. public void InitForCurrentVisualStyle()
  14. {
  15. // If we are Vista Aero, draw using a GroupBox
  16. // Else, use the "etched line via a label w/ a border style" trick
  17. // We would use the "GroupBox" style w/ Luna, except that it wasn't
  18. // working correctly for some reason.
  19. switch (UI.VisualStyleClass)
  20. {
  21. case VisualStyleClass.Aero:
  22. this.selfDrawn = true;
  23. break;
  24. case VisualStyleClass.Luna:
  25. case VisualStyleClass.Classic:
  26. case VisualStyleClass.Other:
  27. this.selfDrawn = false;
  28. break;
  29. default:
  30. throw new InvalidEnumArgumentException();
  31. }
  32. if (this.selfDrawn && (this.label != null && Controls.Contains(this.label)))
  33. {
  34. SuspendLayout();
  35. Controls.Remove(this.label);
  36. ResumeLayout(false);
  37. PerformLayout();
  38. Invalidate(true);
  39. }
  40. else if (!this.selfDrawn && (this.label != null || !Controls.Contains(this.label)))
  41. {
  42. if (this.label == null)
  43. {
  44. this.label = new Label();
  45. this.label.BorderStyle = BorderStyle.Fixed3D;
  46. }
  47. SuspendLayout();
  48. Controls.Add(this.label);
  49. ResumeLayout(false);
  50. PerformLayout();
  51. Invalidate(true);
  52. }
  53. }
  54. public EtchedLine()
  55. {
  56. InitForCurrentVisualStyle();
  57. DoubleBuffered = true;
  58. ResizeRedraw = true;
  59. TabStop = false;
  60. SetStyle(ControlStyles.Selectable, false);
  61. }
  62. public override Size GetPreferredSize(Size proposedSize)
  63. {
  64. return new Size(proposedSize.Width, 2);
  65. }
  66. protected override void OnPaint(PaintEventArgs e)
  67. {
  68. if (this.selfDrawn)
  69. {
  70. GroupBoxRenderer.DrawGroupBox(e.Graphics, new Rectangle(0, 0, Width, 1), GroupBoxState.Normal);
  71. }
  72. base.OnPaint(e);
  73. }
  74. protected override void OnLayout(LayoutEventArgs levent)
  75. {
  76. if (!this.selfDrawn)
  77. {
  78. this.label.Bounds = new Rectangle(0, 0, Width, Height);
  79. }
  80. base.OnLayout(levent);
  81. }
  82. }
  83. }