improve:快抽按钮

This commit is contained in:
CJK_mkp
2025-11-08 22:07:38 +08:00
parent 261ecefb17
commit 1baa74bb69
2 changed files with 218 additions and 24 deletions
+77
View File
@@ -3956,6 +3956,83 @@
</Image.RenderTransform>
</Image>
</Canvas>
<!-- 快抽悬浮按钮 -->
<Border x:Name="QuickDrawFloatingButton"
Background="#80000000"
CornerRadius="8"
BorderThickness="1"
BorderBrush="#40000000"
Width="65"
Height="45"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Margin="0,0,0,200"
Visibility="Collapsed"
Panel.ZIndex="1001">
<Border.Effect>
<DropShadowEffect Color="Black" Direction="315" ShadowDepth="3" Opacity="0.3" BlurRadius="5"/>
</Border.Effect>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="22"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!-- 拖动区域 -->
<Border Grid.Column="0"
MouseLeftButtonDown="QuickDrawFloatingButton_DragArea_MouseLeftButtonDown"
MouseMove="QuickDrawFloatingButton_DragArea_MouseMove"
MouseLeftButtonUp="QuickDrawFloatingButton_DragArea_MouseLeftButtonUp"
Cursor="SizeAll"
Background="Transparent">
<Grid VerticalAlignment="Center" Height="14" IsHitTestVisible="False">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="4"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="4"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- 三个白色横线 -->
<Border Grid.Row="0" Background="White" Height="2" Width="10"
HorizontalAlignment="Center"
CornerRadius="1" Opacity="0.8" IsHitTestVisible="False"/>
<Border Grid.Row="2" Background="White" Height="2" Width="10"
HorizontalAlignment="Center"
CornerRadius="1" Opacity="0.8" IsHitTestVisible="False"/>
<Border Grid.Row="4" Background="White" Height="2" Width="10"
HorizontalAlignment="Center"
CornerRadius="1" Opacity="0.8" IsHitTestVisible="False"/>
</Grid>
</Border>
<!-- 半透明分割线 -->
<Rectangle Grid.Column="1" Width="1" Fill="#20FFFFFF" Margin="0,8,0,8"/>
<!-- 按钮区域 -->
<Border Grid.Column="2"
MouseLeftButtonDown="QuickDrawFloatingButton_Click"
Cursor="Hand"
Background="Transparent">
<Grid IsHitTestVisible="False">
<Path Data="M5 7C5 8.06087 5.42143 9.07828 6.17157 9.82843C6.92172 10.5786 7.93913 11 9 11C10.0609 11 11.0783 10.5786 11.8284 9.82843C12.5786 9.07828 13 8.06087 13 7C13 5.93913 12.5786 4.92172 11.8284 4.17157C11.0783 3.42143 10.0609 3 9 3C7.93913 3 6.92172 3.42143 6.17157 4.17157C5.42143 4.92172 5 5.93913 5 7Z M3 21V19C3 17.9391 3.42143 16.9217 4.17157 16.1716C4.92172 15.4214 5.93913 15 7 15H11C12.0609 15 13.0783 15.4214 13.8284 16.1716C14.5786 16.9217 15 17.9391 15 19V21 M16 3.13C16.8604 3.35031 17.623 3.85071 18.1676 4.55232C18.7122 5.25392 19.0078 6.11683 19.0078 7.005C19.0078 7.89318 18.7122 8.75608 18.1676 9.45769C17.623 10.1593 16.8604 10.6597 16 10.88 M21 21V19C20.9949 18.1172 20.6979 17.2608 20.1553 16.5644C19.6126 15.868 18.8548 15.3707 18 15.15"
Stroke="White"
StrokeThickness="2"
StrokeLineJoin="Round"
Fill="Transparent"
Width="20" Height="20"
Stretch="Uniform"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsHitTestVisible="False"/>
</Grid>
</Border>
</Grid>
</Border>
</Grid>
<Canvas IsHitTestVisible="False">
+141 -24
View File
@@ -28,6 +28,9 @@ using Application = System.Windows.Application;
using Brushes = System.Windows.Media.Brushes;
using Button = System.Windows.Controls.Button;
using Cursor = System.Windows.Input.Cursor;
using MouseEventArgs = System.Windows.Input.MouseEventArgs;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using VerticalAlignment = System.Windows.VerticalAlignment;
using Cursors = System.Windows.Input.Cursors;
using DpiChangedEventArgs = System.Windows.DpiChangedEventArgs;
using File = System.IO.File;
@@ -60,8 +63,10 @@ namespace Ink_Canvas
// 悬浮窗拦截管理器
private FloatingWindowInterceptorManager _floatingWindowInterceptorManager;
// 快抽悬浮按钮
private QuickDrawFloatingButton _quickDrawFloatingButton;
// 快抽悬浮按钮拖动相关
private bool _quickDrawButtonIsDragging = false;
private Point _quickDrawButtonDragStartPoint;
private Point _quickDrawButtonWindowStartPoint;
// 设置面板相关状态
private bool userChangedNoFocusModeInSettings;
@@ -676,11 +681,7 @@ namespace Ink_Canvas
LogHelper.WriteLogToFile("Ink Canvas closing", LogHelper.LogType.Event);
try
{
if (_quickDrawFloatingButton != null)
{
_quickDrawFloatingButton.Close();
_quickDrawFloatingButton = null;
}
// 快抽按钮现在集成在主窗口中,不需要单独关闭
}
catch (Exception ex)
{
@@ -3082,28 +3083,18 @@ namespace Ink_Canvas
{
try
{
var quickDrawButton = FindName("QuickDrawFloatingButton") as Border;
if (quickDrawButton == null) return;
// 检查设置是否启用快抽功能
if (Settings?.RandSettings?.EnableQuickDraw != true)
if (Settings?.RandSettings?.EnableQuickDraw == true)
{
// 如果设置未启用,确保悬浮按钮被关闭
if (_quickDrawFloatingButton != null)
{
_quickDrawFloatingButton.Close();
_quickDrawFloatingButton = null;
}
return;
quickDrawButton.Visibility = Visibility.Visible;
}
// 如果已经存在悬浮按钮,先关闭它
if (_quickDrawFloatingButton != null)
else
{
_quickDrawFloatingButton.Close();
_quickDrawFloatingButton = null;
quickDrawButton.Visibility = Visibility.Collapsed;
}
// 创建并显示悬浮按钮
_quickDrawFloatingButton = new QuickDrawFloatingButton();
_quickDrawFloatingButton.Show();
}
catch (Exception ex)
{
@@ -3111,6 +3102,132 @@ namespace Ink_Canvas
}
}
/// <summary>
/// 快抽按钮点击事件
/// </summary>
private void QuickDrawFloatingButton_Click(object sender, MouseButtonEventArgs e)
{
try
{
// 如果正在拖动,不触发点击事件
if (_quickDrawButtonIsDragging) return;
// 打开快抽窗口
var quickDrawWindow = new QuickDrawWindow();
quickDrawWindow.ShowDialog();
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"打开快抽窗口失败: {ex.Message}", LogHelper.LogType.Error);
}
}
/// <summary>
/// 快抽按钮拖动区域鼠标按下事件
/// </summary>
private void QuickDrawFloatingButton_DragArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
_quickDrawButtonIsDragging = false;
var quickDrawButton = FindName("QuickDrawFloatingButton") as Border;
if (quickDrawButton == null) return;
// 记录鼠标在屏幕上的初始位置
_quickDrawButtonDragStartPoint = quickDrawButton.PointToScreen(e.GetPosition(quickDrawButton));
// 记录按钮的初始位置(相对于Grid)
var parentGrid = quickDrawButton.Parent as Grid;
if (parentGrid != null)
{
var transform = quickDrawButton.TransformToVisual(parentGrid);
var currentPos = transform.Transform(new Point(0, 0));
_quickDrawButtonWindowStartPoint = currentPos;
}
else
{
var currentMargin = quickDrawButton.Margin;
_quickDrawButtonWindowStartPoint = new Point(
double.IsNaN(currentMargin.Left) ? 0 : currentMargin.Left,
double.IsNaN(currentMargin.Top) ? 0 : currentMargin.Top);
}
((UIElement)sender).CaptureMouse();
e.Handled = true;
}
/// <summary>
/// 快抽按钮拖动区域鼠标移动事件
/// </summary>
private void QuickDrawFloatingButton_DragArea_MouseMove(object sender, MouseEventArgs e)
{
var quickDrawButton = FindName("QuickDrawFloatingButton") as Border;
if (quickDrawButton == null) return;
if (e.LeftButton == MouseButtonState.Pressed && ((UIElement)sender).IsMouseCaptured)
{
// 获取鼠标在屏幕上的当前位置
Point currentScreenPoint = quickDrawButton.PointToScreen(e.GetPosition(quickDrawButton));
Vector diff = currentScreenPoint - _quickDrawButtonDragStartPoint;
if (!_quickDrawButtonIsDragging && (Math.Abs(diff.X) > 3 || Math.Abs(diff.Y) > 3))
{
_quickDrawButtonIsDragging = true;
// 切换到绝对定位模式
quickDrawButton.HorizontalAlignment = HorizontalAlignment.Left;
quickDrawButton.VerticalAlignment = VerticalAlignment.Top;
}
if (_quickDrawButtonIsDragging)
{
// 计算新位置
var parentGrid = quickDrawButton.Parent as Grid;
if (parentGrid != null)
{
// 计算屏幕坐标相对于Grid的位置
var gridPoint = parentGrid.PointFromScreen(currentScreenPoint);
var startGridPoint = parentGrid.PointFromScreen(_quickDrawButtonDragStartPoint);
// 计算相对于初始位置的偏移
double offsetX = gridPoint.X - startGridPoint.X;
double offsetY = gridPoint.Y - startGridPoint.Y;
// 新位置 = 初始位置 + 偏移
double newLeft = _quickDrawButtonWindowStartPoint.X + offsetX;
double newTop = _quickDrawButtonWindowStartPoint.Y + offsetY;
// 限制在Grid范围内
newLeft = Math.Max(0, Math.Min(newLeft, parentGrid.ActualWidth - quickDrawButton.ActualWidth));
newTop = Math.Max(0, Math.Min(newTop, parentGrid.ActualHeight - quickDrawButton.ActualHeight));
// 更新Margin
quickDrawButton.Margin = new Thickness(newLeft, newTop, 0, 0);
}
}
}
}
/// <summary>
/// 快抽按钮拖动区域鼠标释放事件
/// </summary>
private void QuickDrawFloatingButton_DragArea_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (((UIElement)sender).IsMouseCaptured)
{
((UIElement)sender).ReleaseMouseCapture();
}
if (_quickDrawButtonIsDragging)
{
Dispatcher.BeginInvoke(new Action(() => { _quickDrawButtonIsDragging = false; }),
DispatcherPriority.Background);
}
else
{
_quickDrawButtonIsDragging = false;
}
e.Handled = true;
}
#endregion
}