PartiallyRoundedRectangle.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Windows;
  2. using System.Windows.Media;
  3. namespace Muchinfo.WPF.Controls.Shape
  4. {
  5. /// <summary>
  6. /// 部分圆角控件
  7. /// </summary>
  8. public class PartiallyRoundedRectangle : System.Windows.Shapes.Shape
  9. {
  10. public static readonly DependencyProperty RadiusXProperty;
  11. public static readonly DependencyProperty RadiusYProperty;
  12. public static readonly DependencyProperty RoundTopLeftProperty;
  13. public static readonly DependencyProperty RoundTopRightProperty;
  14. public static readonly DependencyProperty RoundBottomLeftProperty;
  15. public static readonly DependencyProperty RoundBottomRightProperty;
  16. public int RadiusX
  17. {
  18. get { return (int)GetValue(RadiusXProperty); }
  19. set { SetValue(RadiusXProperty, value); }
  20. }
  21. public int RadiusY
  22. {
  23. get { return (int)GetValue(RadiusYProperty); }
  24. set { SetValue(RadiusYProperty, value); }
  25. }
  26. public bool RoundTopLeft
  27. {
  28. get { return (bool)GetValue(RoundTopLeftProperty); }
  29. set { SetValue(RoundTopLeftProperty, value); }
  30. }
  31. public bool RoundTopRight
  32. {
  33. get { return (bool)GetValue(RoundTopRightProperty); }
  34. set { SetValue(RoundTopRightProperty, value); }
  35. }
  36. public bool RoundBottomLeft
  37. {
  38. get { return (bool)GetValue(RoundBottomLeftProperty); }
  39. set { SetValue(RoundBottomLeftProperty, value); }
  40. }
  41. public bool RoundBottomRight
  42. {
  43. get { return (bool)GetValue(RoundBottomRightProperty); }
  44. set { SetValue(RoundBottomRightProperty, value); }
  45. }
  46. static PartiallyRoundedRectangle()
  47. {
  48. RadiusXProperty = DependencyProperty.Register
  49. ("RadiusX", typeof(int), typeof(PartiallyRoundedRectangle));
  50. RadiusYProperty = DependencyProperty.Register
  51. ("RadiusY", typeof(int), typeof(PartiallyRoundedRectangle));
  52. RoundTopLeftProperty = DependencyProperty.Register
  53. ("RoundTopLeft", typeof(bool), typeof(PartiallyRoundedRectangle));
  54. RoundTopRightProperty = DependencyProperty.Register
  55. ("RoundTopRight", typeof(bool), typeof(PartiallyRoundedRectangle));
  56. RoundBottomLeftProperty = DependencyProperty.Register
  57. ("RoundBottomLeft", typeof(bool), typeof(PartiallyRoundedRectangle));
  58. RoundBottomRightProperty = DependencyProperty.Register
  59. ("RoundBottomRight", typeof(bool), typeof(PartiallyRoundedRectangle));
  60. }
  61. public PartiallyRoundedRectangle()
  62. {
  63. }
  64. protected override Geometry DefiningGeometry
  65. {
  66. get
  67. {
  68. Geometry result = new RectangleGeometry
  69. (new Rect(0, 0, base.Width, base.Height), RadiusX, RadiusY);
  70. double halfWidth = base.Width / 2;
  71. double halfHeight = base.Height / 2;
  72. if (!RoundTopLeft)
  73. result = new CombinedGeometry
  74. (GeometryCombineMode.Union, result, new RectangleGeometry
  75. (new Rect(0, 0, halfWidth, halfHeight)));
  76. if (!RoundTopRight)
  77. result = new CombinedGeometry
  78. (GeometryCombineMode.Union, result, new RectangleGeometry
  79. (new Rect(halfWidth, 0, halfWidth, halfHeight)));
  80. if (!RoundBottomLeft)
  81. result = new CombinedGeometry
  82. (GeometryCombineMode.Union, result, new RectangleGeometry
  83. (new Rect(0, halfHeight, halfWidth, halfHeight)));
  84. if (!RoundBottomRight)
  85. result = new CombinedGeometry
  86. (GeometryCombineMode.Union, result, new RectangleGeometry
  87. (new Rect(halfWidth, halfHeight, halfWidth, halfHeight)));
  88. return result;
  89. }
  90. }
  91. }
  92. }