Watermark.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Markup;
  4. using System.Windows.Media;
  5. using System.Windows.Shapes;
  6. using HandyControl.Data;
  7. namespace HandyControl.Controls;
  8. [TemplatePart(Name = ElementRoot, Type = typeof(Border))]
  9. [ContentProperty(nameof(Content))]
  10. public class Watermark : Control
  11. {
  12. private const string ElementRoot = "PART_Root";
  13. private Border _borderRoot;
  14. private DrawingBrush _brush;
  15. public static readonly DependencyProperty AngleProperty = DependencyProperty.Register(
  16. nameof(Angle), typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
  17. public double Angle
  18. {
  19. get => (double) GetValue(AngleProperty);
  20. set => SetValue(AngleProperty, value);
  21. }
  22. public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
  23. nameof(Content), typeof(object), typeof(Watermark), new PropertyMetadata(default));
  24. public object Content
  25. {
  26. get => GetValue(ContentProperty);
  27. set => SetValue(ContentProperty, value);
  28. }
  29. public static readonly DependencyProperty MarkProperty = DependencyProperty.Register(
  30. nameof(Mark), typeof(object), typeof(Watermark), new FrameworkPropertyMetadata(default, FrameworkPropertyMetadataOptions.AffectsRender));
  31. public object Mark
  32. {
  33. get => GetValue(MarkProperty);
  34. set => SetValue(MarkProperty, value);
  35. }
  36. public static readonly DependencyProperty MarkWidthProperty = DependencyProperty.Register(
  37. nameof(MarkWidth), typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
  38. public double MarkWidth
  39. {
  40. get => (double) GetValue(MarkWidthProperty);
  41. set => SetValue(MarkWidthProperty, value);
  42. }
  43. public static readonly DependencyProperty MarkHeightProperty = DependencyProperty.Register(
  44. nameof(MarkHeight), typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
  45. public double MarkHeight
  46. {
  47. get => (double) GetValue(MarkHeightProperty);
  48. set => SetValue(MarkHeightProperty, value);
  49. }
  50. public static readonly DependencyProperty MarkBrushProperty = DependencyProperty.Register(
  51. nameof(MarkBrush), typeof(Brush), typeof(Watermark), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.AffectsRender));
  52. public Brush MarkBrush
  53. {
  54. get => (Brush) GetValue(MarkBrushProperty);
  55. set => SetValue(MarkBrushProperty, value);
  56. }
  57. public static readonly DependencyProperty AutoSizeEnabledProperty = DependencyProperty.Register(
  58. nameof(AutoSizeEnabled), typeof(bool), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.AffectsRender));
  59. public bool AutoSizeEnabled
  60. {
  61. get => (bool) GetValue(AutoSizeEnabledProperty);
  62. set => SetValue(AutoSizeEnabledProperty, ValueBoxes.BooleanBox(value));
  63. }
  64. public static readonly DependencyProperty MarkMarginProperty = DependencyProperty.Register(
  65. nameof(MarkMargin), typeof(Thickness), typeof(Watermark), new FrameworkPropertyMetadata(default(Thickness), FrameworkPropertyMetadataOptions.AffectsRender));
  66. public Thickness MarkMargin
  67. {
  68. get => (Thickness) GetValue(MarkMarginProperty);
  69. set => SetValue(MarkMarginProperty, value);
  70. }
  71. public override void OnApplyTemplate() => _borderRoot = GetTemplateChild(ElementRoot) as Border;
  72. private void EnsureBrush()
  73. {
  74. var presenter = new ContentPresenter();
  75. if (Mark is Geometry geometry)
  76. {
  77. presenter.Content = new Path
  78. {
  79. Width = MarkWidth,
  80. Height = MarkHeight,
  81. Fill = MarkBrush,
  82. Stretch = Stretch.Uniform,
  83. Data = geometry
  84. };
  85. }
  86. else if (Mark is string str)
  87. {
  88. presenter.Content = new TextBlock
  89. {
  90. Text = str,
  91. FontSize = FontSize,
  92. Foreground = MarkBrush
  93. };
  94. }
  95. else
  96. {
  97. presenter.Content = Mark;
  98. }
  99. Size markSize;
  100. if (AutoSizeEnabled)
  101. {
  102. presenter.Measure(new Size(double.MaxValue, double.MaxValue));
  103. markSize = presenter.DesiredSize;
  104. }
  105. else
  106. {
  107. markSize = new Size(MarkWidth, MarkHeight);
  108. }
  109. _brush = new DrawingBrush
  110. {
  111. ViewportUnits = BrushMappingMode.Absolute,
  112. Stretch = Stretch.Uniform,
  113. TileMode = TileMode.Tile,
  114. Transform = new RotateTransform(Angle),
  115. Drawing = new GeometryDrawing
  116. {
  117. Brush = new VisualBrush(new Border
  118. {
  119. Background = Brushes.Transparent,
  120. Padding = MarkMargin,
  121. Child = presenter
  122. }),
  123. Geometry = new RectangleGeometry(new Rect(markSize))
  124. },
  125. Viewport = new Rect(markSize)
  126. };
  127. RenderOptions.SetCacheInvalidationThresholdMinimum(_brush, 0.5);
  128. RenderOptions.SetCacheInvalidationThresholdMaximum(_brush, 2);
  129. RenderOptions.SetCachingHint(_brush, CachingHint.Cache);
  130. if (_borderRoot != null)
  131. {
  132. _borderRoot.Background = _brush;
  133. }
  134. }
  135. protected override void OnRender(DrawingContext drawingContext) => EnsureBrush();
  136. }