ShadowControl.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.Windows.Media;
  2. namespace System.Windows.Forms
  3. {
  4. public class ShadowControl : Control
  5. {
  6. private Shadow shadow { get; }
  7. private System.Drawing.Color color;
  8. public Drawing.Color Color
  9. {
  10. get => color;
  11. set
  12. {
  13. color = value;
  14. UpdateControl();
  15. }
  16. }
  17. private int depth;
  18. public int Depth
  19. {
  20. get => depth;
  21. set
  22. {
  23. depth = value;
  24. UpdateControl();
  25. }
  26. }
  27. private DockStyle dock;
  28. public override DockStyle Dock
  29. {
  30. get => dock;
  31. set
  32. {
  33. dock = value;
  34. UpdateControl();
  35. }
  36. }
  37. private void UpdateControl()
  38. {
  39. int angle = dock switch
  40. {
  41. DockStyle.Left => 180,
  42. DockStyle.Bottom => 90,
  43. DockStyle.Top => 270,
  44. _ => 0,
  45. };
  46. bool vertical = dock == DockStyle.Left || dock == DockStyle.Right;
  47. if (vertical)
  48. shadow.Width = depth;
  49. else
  50. shadow.Height = depth;
  51. shadow.Background = new LinearGradientBrush(Helper.GetColor(Color), Helper.GetColor(System.Drawing.Color.FromArgb(0, Color)), angle);
  52. shadow.InvalidateVisual();
  53. }
  54. public ShadowControl()
  55. {
  56. shadow = new Shadow() { IsHitTestVisible = false };
  57. SetControl(shadow);
  58. depth = 3;
  59. Dock = DockStyle.Bottom;
  60. }
  61. private class Shadow : System.Windows.Controls.Control
  62. {
  63. protected override void OnRender(DrawingContext drawingContext)
  64. {
  65. base.OnRender(drawingContext);
  66. drawingContext.PushOpacity(0.7);
  67. drawingContext.DrawRectangle(Background, null, new Rect(0, 0, ActualWidth, ActualHeight));
  68. }
  69. }
  70. }
  71. public static class ShadowControlExtensions
  72. {
  73. public static ShadowControl SetShadow(this Control control, DockStyle dock)
  74. {
  75. var shadow = new ShadowControl() { Dock = dock };
  76. var shCtl = shadow.control;
  77. var ctl = control.control;
  78. (ctl.Parent as System.Windows.Controls.Panel)?.Children.Add(shadow.control);
  79. ctl.LayoutUpdated += (s, e) =>
  80. {
  81. shCtl.Visibility = ctl.Visibility;
  82. switch (shadow.Dock)
  83. {
  84. case DockStyle.Left:
  85. shCtl.Margin = new Thickness(ctl.Margin.Left - shadow.Depth, ctl.Margin.Top, 0, 0);
  86. shCtl.Height = ctl.ActualHeight;
  87. break;
  88. case DockStyle.Right:
  89. shCtl.Margin = new Thickness(ctl.Margin.Left + ctl.ActualWidth, ctl.Margin.Top, 0, 0);
  90. shCtl.Height = ctl.ActualHeight;
  91. break;
  92. case DockStyle.Top:
  93. shCtl.Margin = new Thickness(ctl.Margin.Left, ctl.Margin.Top - shadow.Depth, 0, 0);
  94. shCtl.Width = ctl.ActualWidth;
  95. break;
  96. case DockStyle.Bottom:
  97. shCtl.Margin = new Thickness(ctl.Margin.Left, ctl.Margin.Top + ctl.ActualHeight, 0, 0);
  98. shCtl.Width = ctl.ActualWidth;
  99. break;
  100. }
  101. };
  102. return shadow;
  103. }
  104. }
  105. }