EtchedLine.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. using System.Windows.Forms.VisualStyles;
  11. namespace SmartCoalApplication.Core
  12. {
  13. public sealed class EtchedLine : Control
  14. {
  15. private bool selfDrawn = false;
  16. private Label label;
  17. public void InitForCurrentVisualStyle()
  18. {
  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 System.Drawing.Rectangle(0, 0, Width, Height);
  79. }
  80. base.OnLayout(levent);
  81. }
  82. }
  83. }