add Watermark

This commit is contained in:
NaBian committed 2020-04-30 01:22:27 +08:00
1 parent 507a80a3ee
commit 2ced600571
42 files changed
+357 -269

No files matched your search

@@ -0,0 +1,164 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using HandyControl.Data;
namespace HandyControl.Controls
{
[TemplatePart(Name = ElementRoot, Type = typeof(Border))]
[ContentProperty(nameof(Content))]
public class Watermark : Control
{
private const string ElementRoot = "PART_Root";
private Border _borderRoot;
private DrawingBrush _brush;
public static readonly DependencyProperty AngleProperty = DependencyProperty.Register(
"Angle", typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
public double Angle
{
get => (double) GetValue(AngleProperty);
set => SetValue(AngleProperty, value);
}
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(
"Content", typeof(object), typeof(Watermark), new PropertyMetadata(default));
public object Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public static readonly DependencyProperty MarkProperty = DependencyProperty.Register(
"Mark", typeof(object), typeof(Watermark), new FrameworkPropertyMetadata(default, FrameworkPropertyMetadataOptions.AffectsRender));
public object Mark
{
get => GetValue(MarkProperty);
set => SetValue(MarkProperty, value);
}
public static readonly DependencyProperty MarkWidthProperty = DependencyProperty.Register(
"MarkWidth", typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
public double MarkWidth
{
get => (double) GetValue(MarkWidthProperty);
set => SetValue(MarkWidthProperty, value);
}
public static readonly DependencyProperty MarkHeightProperty = DependencyProperty.Register(
"MarkHeight", typeof(double), typeof(Watermark), new FrameworkPropertyMetadata(ValueBoxes.Double0Box, FrameworkPropertyMetadataOptions.AffectsRender));
public double MarkHeight
{
get => (double) GetValue(MarkHeightProperty);
set => SetValue(MarkHeightProperty, value);
}
public static readonly DependencyProperty MarkBrushProperty = DependencyProperty.Register(
"MarkBrush", typeof(Brush), typeof(Watermark), new FrameworkPropertyMetadata(default(Brush), FrameworkPropertyMetadataOptions.AffectsRender));
public Brush MarkBrush
{
get => (Brush) GetValue(MarkBrushProperty);
set => SetValue(MarkBrushProperty, value);
}
public static readonly DependencyProperty AutoSizeEnabledProperty = DependencyProperty.Register(
"AutoSizeEnabled", typeof(bool), typeof(Watermark), new PropertyMetadata(ValueBoxes.TrueBox));
public bool AutoSizeEnabled
{
get => (bool) GetValue(AutoSizeEnabledProperty);
set => SetValue(AutoSizeEnabledProperty, value);
}
public static readonly DependencyProperty MarkMarginProperty = DependencyProperty.Register(
"MarkMargin", typeof(Thickness), typeof(Watermark), new PropertyMetadata(default(Thickness)));
public Thickness MarkMargin
{
get => (Thickness) GetValue(MarkMarginProperty);
set => SetValue(MarkMarginProperty, value);
}
public override void OnApplyTemplate() => _borderRoot = GetTemplateChild(ElementRoot) as Border;
private void EnsureBrush()
{
var presenter = new ContentPresenter();
if (Mark is Geometry geometry)
{
presenter.Content = new Path
{
Width = MarkWidth,
Height = MarkHeight,
Fill = MarkBrush,
Stretch = Stretch.Uniform,
Data = geometry
};
}
else if (Mark is string str)
{
presenter.Content = new TextBlock
{
Text = str,
FontSize = FontSize
};
}
else
{
presenter.Content = Mark;
}
Size markSize;
if (AutoSizeEnabled)
{
presenter.Measure(new Size(double.MaxValue, double.MaxValue));
markSize = presenter.DesiredSize;
}
else
{
markSize = new Size(MarkWidth, MarkHeight);
}
_brush = new DrawingBrush
{
ViewportUnits = BrushMappingMode.Absolute,
Stretch = Stretch.Uniform,
TileMode = TileMode.Tile,
Transform = new RotateTransform(Angle),
Drawing = new GeometryDrawing
{
Brush = new VisualBrush(new Border
{
Background = Brushes.Transparent,
Padding = MarkMargin,
Child = presenter
}),
Geometry = new RectangleGeometry(new Rect(markSize))
},
Viewport = new Rect(markSize)
};
RenderOptions.SetCacheInvalidationThresholdMinimum(_brush, 0.5);
RenderOptions.SetCacheInvalidationThresholdMaximum(_brush, 2);
RenderOptions.SetCachingHint(_brush, CachingHint.Cache);
if (_borderRoot != null)
{
_borderRoot.Background = _brush;
}
}
protected override void OnRender(DrawingContext drawingContext) => EnsureBrush();
}
}
@@ -36,6 +36,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Card.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Notification.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Poptip.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Watermark.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Screenshot\Screenshot.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Sprite.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\ToggleBlock.cs" />
@@ -414,6 +415,14 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\Watermark.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\Base\WatermarkBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Themes\Styles\Base\ComboBoxBaseStyle.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -85,7 +85,7 @@
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource DarkDefaultBrush}"/>
<Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource SecondaryRegionBrush}"/>
<Setter Property="TextElement.Foreground" TargetName="menuHeaderContainer" Value="{DynamicResource PrimaryTextBrush}"/>
</Trigger>
<Trigger Property="IsSubmenuOpen" Value="True">
@@ -125,7 +125,7 @@
<Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsHighlighted" Value="True">
<Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource DarkDefaultBrush}"/>
<Setter Property="Background" TargetName="templateRoot" Value="{DynamicResource SecondaryRegionBrush}"/>
<Setter Property="TextElement.Foreground" TargetName="menuHeaderContainer" Value="{DynamicResource PrimaryTextBrush}"/>
</Trigger>
<Trigger Property="IsSubmenuOpen" Value="True">
@@ -0,0 +1,20 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:HandyControl.Controls">
<Style x:Key="WatermarkBaseStyle" TargetType="controls:Watermark">
<Setter Property="MarkBrush" Value="{DynamicResource DarkMaskBrush}"/>
<Setter Property="Foreground" Value="{DynamicResource DarkMaskBrush}"/>
<Setter Property="Angle" Value="20"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:Watermark">
<Border Name="PART_Root">
<ContentPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
@@ -86,6 +86,7 @@
<ResourceDictionary Source="Frame.xaml"/>
<ResourceDictionary Source="BlendEffectBox.xaml"/>
<ResourceDictionary Source="Screenshot.xaml"/>
<ResourceDictionary Source="Watermark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
@@ -0,0 +1,10 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:controls="clr-namespace:HandyControl.Controls">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Base/WatermarkBaseStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style BasedOn="{StaticResource WatermarkBaseStyle}" TargetType="controls:Watermark"/>
</ResourceDictionary>
@@ -49,6 +49,18 @@ namespace HandyControl.Tools
key == Key.RightCtrl || key == Key.RightAlt || key == Key.RightShift || key == Key.RWin;
}
public static void Init(List<KeyBinding> list)
{
CommandDic.Clear();
if (list == null) return;
KeyBindingCollection = new ObservableCollection<KeyBinding>(list);
if (KeyBindingCollection.Count == 0) return;
AddKeyBindings(KeyBindingCollection);
KeyboardHook.Start();
}
private static void AddKeyBindings(IEnumerable<KeyBinding> keyBindings)
{
foreach (var item in keyBindings)