星火微课系统客户端
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ZJClippingBorder.cs 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. namespace Common.system
  10. {
  11. public class ZJClippingBorder : Border
  12. {
  13. private object _oldClip;
  14. protected override void OnRender(DrawingContext dc)
  15. {
  16. OnApplyChildClip();
  17. base.OnRender(dc);
  18. }
  19. public override UIElement Child
  20. {
  21. get => base.Child;
  22. set
  23. {
  24. if (Child != value)
  25. {
  26. if (Child != null)
  27. {
  28. Child.SetValue(ClipProperty, _oldClip);
  29. }
  30. if (value != null)
  31. {
  32. _oldClip = value.ReadLocalValue(ClipProperty);
  33. }
  34. else
  35. {
  36. // If we dont set it to null we could leak a Geometry object
  37. _oldClip = null;
  38. }
  39. base.Child = value;
  40. }
  41. }
  42. }
  43. protected virtual void OnApplyChildClip()
  44. {
  45. UIElement child = Child;
  46. if (child != null)
  47. {
  48. double top = Math.Max(CornerRadius.TopLeft, CornerRadius.TopRight);
  49. double bottom = Math.Max(CornerRadius.BottomLeft, CornerRadius.BottomRight);
  50. double max = Math.Max(top, bottom);
  51. Size size = RenderSize;
  52. double width = size.Width - (BorderThickness.Left + BorderThickness.Right);
  53. double height = size.Height - (BorderThickness.Top + BorderThickness.Bottom);
  54. Geometry result = new RectangleGeometry(new Rect(0, 0, width, height), max, max);
  55. double halfWidth = width / 2;
  56. double halfHeight = height / 2;
  57. if (CornerRadius.TopLeft == 0)
  58. {
  59. result = new CombinedGeometry(
  60. GeometryCombineMode.Union,
  61. result,
  62. new RectangleGeometry(new Rect(0, 0, halfWidth, halfHeight))
  63. );
  64. }
  65. if (CornerRadius.TopRight == 0)
  66. {
  67. result = new CombinedGeometry(GeometryCombineMode.Union, result, new RectangleGeometry
  68. (new Rect(halfWidth, 0, halfWidth, halfHeight)));
  69. }
  70. if (CornerRadius.BottomLeft == 0)
  71. {
  72. result = new CombinedGeometry
  73. (GeometryCombineMode.Union, result, new RectangleGeometry
  74. (new Rect(0, halfHeight, halfWidth, halfHeight)));
  75. }
  76. if (CornerRadius.BottomRight == 0)
  77. {
  78. result = new CombinedGeometry
  79. (
  80. GeometryCombineMode.Union,
  81. result,
  82. new RectangleGeometry(new Rect(halfWidth, halfHeight, halfWidth, halfHeight))
  83. );
  84. }
  85. child.Clip = result;
  86. }
  87. }
  88. }
  89. }