add:自定义控件

屎山给我吃饱了 把浮动工具栏用了自定义控件 不然重复代码太多了
This commit is contained in:
PrefacedCorg
2026-04-11 12:06:53 +08:00
parent 0d6ffa6de6
commit dba59b92ac
2 changed files with 120 additions and 0 deletions
@@ -0,0 +1,32 @@
<UserControl x:Class="Ink_Canvas.Controls.ToolbarImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ikw="http://schemas.inkore.net/lib/ui/wpf"
mc:Ignorable="d"
d:DesignHeight="45" d:DesignWidth="28">
<ikw:SimpleStackPanel Name="ButtonPanel"
MouseDown="ButtonPanel_MouseDown"
MouseLeave="ButtonPanel_MouseLeave"
MouseUp="ButtonPanel_MouseUp"
Background="Transparent" Orientation="Vertical"
HorizontalAlignment="Center" Width="28" Margin="0,-2">
<Image x:Name="ButtonImage" RenderOptions.BitmapScalingMode="HighQuality" Height="17" Margin="0,3,0,0">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup ClipGeometry="M0,0 V24 H24 V0 H0 Z">
<GeometryDrawing x:Name="IconGeometryInternal" Brush="{DynamicResource IconForeground}" />
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
<TextBlock x:Name="LabelTextBlock"
Foreground="{DynamicResource FloatBarForeground}"
FontSize="8"
Margin="0,1,0,0" TextAlignment="Center"
Style="{DynamicResource AutoFitMainToolbarLabel8}" />
</ikw:SimpleStackPanel>
</UserControl>
@@ -0,0 +1,88 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace Ink_Canvas.Controls
{
public partial class ToolbarImageButton : UserControl
{
private static ToolbarImageButton _lastPressedButton;
public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
nameof(Label), typeof(string), typeof(ToolbarImageButton),
new PropertyMetadata(string.Empty, (d, e) => ((ToolbarImageButton)d).LabelTextBlock.Text = (string)e.NewValue));
public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}
public static readonly DependencyProperty IconGeometryDrawingProperty = DependencyProperty.Register(
nameof(IconGeometryDrawing), typeof(GeometryDrawing), typeof(ToolbarImageButton),
new PropertyMetadata(null, OnIconGeometryDrawingChanged));
private static void OnIconGeometryDrawingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var button = (ToolbarImageButton)d;
if (e.NewValue is GeometryDrawing newDrawing)
{
button.IconGeometryInternal.Geometry = newDrawing.Geometry;
button.IconGeometryInternal.Brush = newDrawing.Brush;
}
}
public GeometryDrawing IconGeometryDrawing
{
get => (GeometryDrawing)GetValue(IconGeometryDrawingProperty);
set => SetValue(IconGeometryDrawingProperty, value);
}
public GeometryDrawing Icon => IconGeometryInternal;
public GeometryDrawing GeometryDrawing => IconGeometryInternal;
public new Brush Background
{
get => ButtonPanel.Background;
set => ButtonPanel.Background = value;
}
public event MouseButtonEventHandler ButtonMouseDown;
public event MouseEventHandler ButtonMouseLeave;
public event RoutedEventHandler ButtonMouseUp;
public ToolbarImageButton()
{
InitializeComponent();
ButtonPanel.Background = Brushes.Transparent;
}
private void ButtonPanel_MouseDown(object sender, MouseButtonEventArgs e)
{
if (_lastPressedButton != null && _lastPressedButton != this)
{
_lastPressedButton.Background = Brushes.Transparent;
}
_lastPressedButton = this;
ButtonPanel.Background = new SolidColorBrush(Color.FromArgb(28, 24, 24, 27));
ButtonMouseDown?.Invoke(this, e);
}
private void ButtonPanel_MouseLeave(object sender, MouseEventArgs e)
{
ButtonMouseLeave?.Invoke(this, e);
}
private void ButtonPanel_MouseUp(object sender, MouseButtonEventArgs e)
{
if (_lastPressedButton == this)
{
ButtonPanel.Background = Brushes.Transparent;
_lastPressedButton = null;
}
ButtonMouseUp?.Invoke(this, new RoutedEventArgs(e.RoutedEvent, this));
}
}
}