improve:issue #112
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<UserControl x:Class="Ink_Canvas.Windows.HotkeyItem"
|
||||
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:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
mc:Ignorable="d"
|
||||
d:DesignHeight="60" d:DesignWidth="600">
|
||||
|
||||
<Border Background="White"
|
||||
BorderBrush="#E0E0E0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="5"
|
||||
Margin="0,2">
|
||||
<Grid Margin="15,10">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
<ColumnDefinition Width="Auto"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<!-- 左侧信息 -->
|
||||
<ui:SimpleStackPanel Grid.Column="0" VerticalAlignment="Center">
|
||||
<TextBlock x:Name="TitleTextBlock"
|
||||
Text="快捷键标题"
|
||||
FontSize="14"
|
||||
FontWeight="SemiBold"
|
||||
Foreground="#333333"/>
|
||||
<TextBlock x:Name="DescriptionTextBlock"
|
||||
Text="快捷键描述"
|
||||
FontSize="12"
|
||||
Foreground="#666666"
|
||||
Margin="0,2,0,0"/>
|
||||
</ui:SimpleStackPanel>
|
||||
|
||||
<!-- 当前快捷键显示 -->
|
||||
<Border Grid.Column="1"
|
||||
Background="#F5F5F5"
|
||||
BorderBrush="#D0D0D0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="3"
|
||||
Margin="10,0,0,0"
|
||||
Padding="8,4">
|
||||
<TextBlock x:Name="CurrentHotkeyTextBlock"
|
||||
Text="未设置"
|
||||
FontSize="12"
|
||||
Foreground="#666666"
|
||||
MinWidth="80"
|
||||
TextAlignment="Center"/>
|
||||
</Border>
|
||||
|
||||
<!-- 设置按钮 -->
|
||||
<Button x:Name="BtnSetHotkey"
|
||||
Grid.Column="2"
|
||||
Content="设置"
|
||||
Width="60"
|
||||
Height="28"
|
||||
Background="#0066BF"
|
||||
Foreground="White"
|
||||
FontSize="12"
|
||||
Margin="10,0,0,0"
|
||||
Click="BtnSetHotkey_Click"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,166 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Ink_Canvas.Windows
|
||||
{
|
||||
/// <summary>
|
||||
/// 快捷键项控件
|
||||
/// </summary>
|
||||
public partial class HotkeyItem : UserControl
|
||||
{
|
||||
#region Events
|
||||
/// <summary>
|
||||
/// 快捷键变更事件
|
||||
/// </summary>
|
||||
public event EventHandler<HotkeyChangedEventArgs> HotkeyChanged;
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
public string Title
|
||||
{
|
||||
get => TitleTextBlock.Text;
|
||||
set => TitleTextBlock.Text = value;
|
||||
}
|
||||
|
||||
public string Description
|
||||
{
|
||||
get => DescriptionTextBlock.Text;
|
||||
set => DescriptionTextBlock.Text = value;
|
||||
}
|
||||
|
||||
public string DefaultKey { get; set; }
|
||||
public string DefaultModifiers { get; set; }
|
||||
|
||||
private Key _currentKey = Key.None;
|
||||
private ModifierKeys _currentModifiers = ModifierKeys.None;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public HotkeyItem()
|
||||
{
|
||||
InitializeComponent();
|
||||
UpdateHotkeyDisplay();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
/// <summary>
|
||||
/// 设置当前快捷键
|
||||
/// </summary>
|
||||
/// <param name="key">按键</param>
|
||||
/// <param name="modifiers">修饰键</param>
|
||||
public void SetCurrentHotkey(Key key, ModifierKeys modifiers)
|
||||
{
|
||||
_currentKey = key;
|
||||
_currentModifiers = modifiers;
|
||||
UpdateHotkeyDisplay();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前快捷键
|
||||
/// </summary>
|
||||
/// <returns>快捷键信息</returns>
|
||||
public (Key key, ModifierKeys modifiers) GetCurrentHotkey()
|
||||
{
|
||||
return (_currentKey, _currentModifiers);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private void UpdateHotkeyDisplay()
|
||||
{
|
||||
if (_currentKey == Key.None)
|
||||
{
|
||||
CurrentHotkeyTextBlock.Text = "未设置";
|
||||
CurrentHotkeyTextBlock.Foreground = System.Windows.Media.Brushes.Gray;
|
||||
}
|
||||
else
|
||||
{
|
||||
var modifiersText = _currentModifiers == ModifierKeys.None ? "" : $"{_currentModifiers}+";
|
||||
CurrentHotkeyTextBlock.Text = $"{modifiersText}{_currentKey}";
|
||||
CurrentHotkeyTextBlock.Foreground = System.Windows.Media.Brushes.Black;
|
||||
}
|
||||
}
|
||||
|
||||
private void StartHotkeyCapture()
|
||||
{
|
||||
BtnSetHotkey.Content = "请按键...";
|
||||
BtnSetHotkey.Background = System.Windows.Media.Brushes.Orange;
|
||||
|
||||
// 设置焦点以捕获键盘事件
|
||||
Focus();
|
||||
|
||||
// 添加键盘事件处理器
|
||||
KeyDown += HotkeyItem_KeyDown;
|
||||
KeyUp += HotkeyItem_KeyUp;
|
||||
}
|
||||
|
||||
private void StopHotkeyCapture()
|
||||
{
|
||||
BtnSetHotkey.Content = "设置";
|
||||
BtnSetHotkey.Background = System.Windows.Media.Brushes.DodgerBlue;
|
||||
|
||||
// 移除键盘事件处理器
|
||||
KeyDown -= HotkeyItem_KeyDown;
|
||||
KeyUp -= HotkeyItem_KeyUp;
|
||||
}
|
||||
|
||||
private void HotkeyItem_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
|
||||
// 忽略某些特殊键
|
||||
if (e.Key == Key.LeftShift || e.Key == Key.RightShift ||
|
||||
e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl ||
|
||||
e.Key == Key.LeftAlt || e.Key == Key.RightAlt ||
|
||||
e.Key == Key.LWin || e.Key == Key.RWin)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取修饰键
|
||||
var modifiers = ModifierKeys.None;
|
||||
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
|
||||
modifiers |= ModifierKeys.Control;
|
||||
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
|
||||
modifiers |= ModifierKeys.Shift;
|
||||
if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
|
||||
modifiers |= ModifierKeys.Alt;
|
||||
if (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin))
|
||||
modifiers |= ModifierKeys.Windows;
|
||||
|
||||
// 设置新的快捷键
|
||||
var oldKey = _currentKey;
|
||||
var oldModifiers = _currentModifiers;
|
||||
|
||||
_currentKey = e.Key;
|
||||
_currentModifiers = modifiers;
|
||||
|
||||
UpdateHotkeyDisplay();
|
||||
StopHotkeyCapture();
|
||||
|
||||
// 触发快捷键变更事件
|
||||
HotkeyChanged?.Invoke(this, new HotkeyChangedEventArgs
|
||||
{
|
||||
HotkeyName = Title,
|
||||
Key = _currentKey,
|
||||
Modifiers = _currentModifiers
|
||||
});
|
||||
}
|
||||
|
||||
private void HotkeyItem_KeyUp(object sender, KeyEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
private void BtnSetHotkey_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
StartHotkeyCapture();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
<Window x:Class="Ink_Canvas.Windows.HotkeySettingsWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Ink_Canvas.Windows"
|
||||
xmlns:ui="http://schemas.inkore.net/lib/ui/wpf/modern"
|
||||
ui:ThemeManager.RequestedTheme="Light"
|
||||
Topmost="True"
|
||||
Background="Transparent"
|
||||
AllowsTransparency="True"
|
||||
mc:Ignorable="d"
|
||||
WindowStyle="None"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Title="快捷键设置"
|
||||
Height="600"
|
||||
Width="800">
|
||||
|
||||
<Border Background="#F0F3F9" CornerRadius="10" BorderThickness="1" BorderBrush="#0066BF" Margin="10">
|
||||
<Grid>
|
||||
<!-- 标题栏 -->
|
||||
<Border Height="50" Background="#0066BF" CornerRadius="10,10,0,0" VerticalAlignment="Top">
|
||||
<Grid>
|
||||
<TextBlock Text="快捷键设置"
|
||||
Foreground="White"
|
||||
FontSize="18"
|
||||
FontWeight="Bold"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Center"/>
|
||||
<Button x:Name="BtnClose"
|
||||
Content="✕"
|
||||
Width="30"
|
||||
Height="30"
|
||||
Background="Transparent"
|
||||
Foreground="White"
|
||||
FontSize="14"
|
||||
HorizontalAlignment="Right"
|
||||
Margin="0,0,30,0"
|
||||
Click="BtnClose_Click"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- 内容区域 -->
|
||||
<ScrollViewer Margin="0,50,0,0" VerticalScrollBarVisibility="Auto">
|
||||
<ui:SimpleStackPanel Margin="20">
|
||||
<!-- 说明文字 -->
|
||||
<TextBlock Text="在这里可以自定义全局快捷键设置。全局快捷键在任何情况下都能生效,即使应用程序不在焦点状态。"
|
||||
TextWrapping="Wrap"
|
||||
Margin="0,0,0,20"
|
||||
Foreground="#666666"/>
|
||||
|
||||
<!-- 快捷键列表 -->
|
||||
<ui:SimpleStackPanel x:Name="HotkeyList" Margin="0,0,0,20">
|
||||
<!-- 基本操作 -->
|
||||
<GroupBox Header="基本操作" Margin="0,0,0,15">
|
||||
<ui:SimpleStackPanel>
|
||||
<local:HotkeyItem x:Name="UndoHotkey"
|
||||
Title="撤销"
|
||||
Description="撤销上一步操作"
|
||||
DefaultKey="Z"
|
||||
DefaultModifiers="Control"/>
|
||||
<local:HotkeyItem x:Name="RedoHotkey"
|
||||
Title="重做"
|
||||
Description="重做上一步操作"
|
||||
DefaultKey="Y"
|
||||
DefaultModifiers="Control"/>
|
||||
<local:HotkeyItem x:Name="ClearHotkey"
|
||||
Title="清空"
|
||||
Description="清空当前画板内容"
|
||||
DefaultKey="E"
|
||||
DefaultModifiers="Control"/>
|
||||
<local:HotkeyItem x:Name="PasteHotkey"
|
||||
Title="粘贴"
|
||||
Description="粘贴剪贴板内容"
|
||||
DefaultKey="V"
|
||||
DefaultModifiers="Control"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</GroupBox>
|
||||
|
||||
<!-- 工具切换 -->
|
||||
<GroupBox Header="工具切换" Margin="0,0,0,15">
|
||||
<ui:SimpleStackPanel>
|
||||
<local:HotkeyItem x:Name="SelectToolHotkey"
|
||||
Title="选择工具"
|
||||
Description="切换到选择工具"
|
||||
DefaultKey="S"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="DrawToolHotkey"
|
||||
Title="绘图工具"
|
||||
Description="切换到绘图工具"
|
||||
DefaultKey="D"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="EraserToolHotkey"
|
||||
Title="橡皮擦工具"
|
||||
Description="切换到橡皮擦工具"
|
||||
DefaultKey="E"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="BlackboardToolHotkey"
|
||||
Title="黑板工具"
|
||||
Description="切换到黑板工具"
|
||||
DefaultKey="B"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="QuitDrawToolHotkey"
|
||||
Title="退出绘图"
|
||||
Description="退出绘图模式"
|
||||
DefaultKey="Q"
|
||||
DefaultModifiers="Alt"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</GroupBox>
|
||||
|
||||
<!-- 画笔设置 -->
|
||||
<GroupBox Header="画笔设置" Margin="0,0,0,15">
|
||||
<ui:SimpleStackPanel>
|
||||
<local:HotkeyItem x:Name="Pen1Hotkey"
|
||||
Title="画笔1"
|
||||
Description="选择画笔1"
|
||||
DefaultKey="D1"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="Pen2Hotkey"
|
||||
Title="画笔2"
|
||||
Description="选择画笔2"
|
||||
DefaultKey="D2"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="Pen3Hotkey"
|
||||
Title="画笔3"
|
||||
Description="选择画笔3"
|
||||
DefaultKey="D3"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="Pen4Hotkey"
|
||||
Title="画笔4"
|
||||
Description="选择画笔4"
|
||||
DefaultKey="D4"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="Pen5Hotkey"
|
||||
Title="画笔5"
|
||||
Description="选择画笔5"
|
||||
DefaultKey="D5"
|
||||
DefaultModifiers="Alt"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</GroupBox>
|
||||
|
||||
<!-- 功能快捷键 -->
|
||||
<GroupBox Header="功能快捷键" Margin="0,0,0,15">
|
||||
<ui:SimpleStackPanel>
|
||||
<local:HotkeyItem x:Name="DrawLineHotkey"
|
||||
Title="绘制直线"
|
||||
Description="绘制直线工具"
|
||||
DefaultKey="L"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="ScreenshotHotkey"
|
||||
Title="截图"
|
||||
Description="保存屏幕截图到桌面"
|
||||
DefaultKey="C"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="HideHotkey"
|
||||
Title="隐藏"
|
||||
Description="隐藏应用程序"
|
||||
DefaultKey="V"
|
||||
DefaultModifiers="Alt"/>
|
||||
<local:HotkeyItem x:Name="ExitHotkey"
|
||||
Title="退出"
|
||||
Description="退出当前模式或应用程序"
|
||||
DefaultKey="Escape"
|
||||
DefaultModifiers="None"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</GroupBox>
|
||||
</ui:SimpleStackPanel>
|
||||
</ui:SimpleStackPanel>
|
||||
</ScrollViewer>
|
||||
|
||||
<!-- 底部按钮 -->
|
||||
<Border Height="60" Background="#F8F9FA" CornerRadius="0,0,10,10" VerticalAlignment="Bottom">
|
||||
<ui:SimpleStackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,0,20,0">
|
||||
<Button x:Name="BtnResetToDefault"
|
||||
Content="重置为默认"
|
||||
Width="100"
|
||||
Height="35"
|
||||
Margin="0,0,10,0"
|
||||
Click="BtnResetToDefault_Click"/>
|
||||
<Button x:Name="BtnSave"
|
||||
Content="保存设置"
|
||||
Width="100"
|
||||
Height="35"
|
||||
Background="#0066BF"
|
||||
Foreground="White"
|
||||
Click="BtnSave_Click"/>
|
||||
</ui:SimpleStackPanel>
|
||||
</Border>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Window>
|
||||
@@ -0,0 +1,357 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using Ink_Canvas.Helpers;
|
||||
|
||||
namespace Ink_Canvas.Windows
|
||||
{
|
||||
/// <summary>
|
||||
/// 快捷键设置窗口
|
||||
/// </summary>
|
||||
public partial class HotkeySettingsWindow : Window
|
||||
{
|
||||
#region Private Fields
|
||||
private readonly MainWindow _mainWindow;
|
||||
private readonly GlobalHotkeyManager _hotkeyManager;
|
||||
private readonly Dictionary<string, HotkeyItem> _hotkeyItems;
|
||||
#endregion
|
||||
|
||||
#region Constructor
|
||||
public HotkeySettingsWindow(MainWindow mainWindow, GlobalHotkeyManager hotkeyManager)
|
||||
{
|
||||
InitializeComponent();
|
||||
_mainWindow = mainWindow;
|
||||
_hotkeyManager = hotkeyManager;
|
||||
_hotkeyItems = new Dictionary<string, HotkeyItem>();
|
||||
|
||||
// 隐藏主窗口的设置页面
|
||||
HideMainWindowSettings();
|
||||
InitializeHotkeyItems();
|
||||
LoadCurrentHotkeys();
|
||||
SetupEventHandlers();
|
||||
|
||||
// 注册窗口关闭事件
|
||||
this.Closed += HotkeySettingsWindow_Closed;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
private void InitializeHotkeyItems()
|
||||
{
|
||||
// 初始化快捷键项
|
||||
_hotkeyItems["Undo"] = UndoHotkey;
|
||||
_hotkeyItems["Redo"] = RedoHotkey;
|
||||
_hotkeyItems["Clear"] = ClearHotkey;
|
||||
_hotkeyItems["Paste"] = PasteHotkey;
|
||||
_hotkeyItems["SelectTool"] = SelectToolHotkey;
|
||||
_hotkeyItems["DrawTool"] = DrawToolHotkey;
|
||||
_hotkeyItems["EraserTool"] = EraserToolHotkey;
|
||||
_hotkeyItems["BlackboardTool"] = BlackboardToolHotkey;
|
||||
_hotkeyItems["QuitDrawTool"] = QuitDrawToolHotkey;
|
||||
_hotkeyItems["Pen1"] = Pen1Hotkey;
|
||||
_hotkeyItems["Pen2"] = Pen2Hotkey;
|
||||
_hotkeyItems["Pen3"] = Pen3Hotkey;
|
||||
_hotkeyItems["Pen4"] = Pen4Hotkey;
|
||||
_hotkeyItems["Pen5"] = Pen5Hotkey;
|
||||
_hotkeyItems["DrawLine"] = DrawLineHotkey;
|
||||
_hotkeyItems["Screenshot"] = ScreenshotHotkey;
|
||||
_hotkeyItems["Hide"] = HideHotkey;
|
||||
_hotkeyItems["Exit"] = ExitHotkey;
|
||||
}
|
||||
|
||||
private void LoadCurrentHotkeys()
|
||||
{
|
||||
try
|
||||
{
|
||||
var registeredHotkeys = _hotkeyManager.GetRegisteredHotkeys();
|
||||
foreach (var hotkey in registeredHotkeys)
|
||||
{
|
||||
if (_hotkeyItems.TryGetValue(hotkey.Name, out var hotkeyItem))
|
||||
{
|
||||
hotkeyItem.SetCurrentHotkey(hotkey.Key, hotkey.Modifiers);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"加载当前快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupEventHandlers()
|
||||
{
|
||||
// 为每个快捷键项设置事件处理器
|
||||
foreach (var hotkeyItem in _hotkeyItems.Values)
|
||||
{
|
||||
hotkeyItem.HotkeyChanged += OnHotkeyChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHotkeyChanged(object sender, HotkeyChangedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 检查快捷键冲突
|
||||
if (IsHotkeyConflict(e.Key, e.Modifiers, e.HotkeyName))
|
||||
{
|
||||
MessageBox.Show($"快捷键 {e.Modifiers}+{e.Key} 已被其他功能使用,请选择其他组合。",
|
||||
"快捷键冲突", MessageBoxButton.OK, MessageBoxImage.Warning);
|
||||
return;
|
||||
}
|
||||
|
||||
// 更新快捷键管理器
|
||||
UpdateHotkeyInManager(e.HotkeyName, e.Key, e.Modifiers);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"处理快捷键变更时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsHotkeyConflict(Key key, ModifierKeys modifiers, string excludeHotkeyName)
|
||||
{
|
||||
var registeredHotkeys = _hotkeyManager.GetRegisteredHotkeys();
|
||||
foreach (var hotkey in registeredHotkeys)
|
||||
{
|
||||
if (hotkey.Name != excludeHotkeyName &&
|
||||
hotkey.Key == key &&
|
||||
hotkey.Modifiers == modifiers)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void UpdateHotkeyInManager(string hotkeyName, Key key, ModifierKeys modifiers)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 根据快捷键名称获取对应的动作
|
||||
var action = GetActionForHotkey(hotkeyName);
|
||||
if (action != null)
|
||||
{
|
||||
_hotkeyManager.RegisterHotkey(hotkeyName, key, modifiers, action);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"更新快捷键管理器时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private Action GetActionForHotkey(string hotkeyName)
|
||||
{
|
||||
switch (hotkeyName)
|
||||
{
|
||||
case "Undo":
|
||||
return () => _mainWindow.SymbolIconUndo_MouseUp(null, null);
|
||||
case "Redo":
|
||||
return () => _mainWindow.SymbolIconRedo_MouseUp(null, null);
|
||||
case "Clear":
|
||||
return () => _mainWindow.SymbolIconDelete_MouseUp(null, null);
|
||||
case "Paste":
|
||||
return () => _mainWindow.HandleGlobalPaste(null, null);
|
||||
case "SelectTool":
|
||||
return () => _mainWindow.SymbolIconSelect_MouseUp(null, null);
|
||||
case "DrawTool":
|
||||
return () => _mainWindow.PenIcon_Click(null, null);
|
||||
case "EraserTool":
|
||||
return () => _mainWindow.EraserIcon_Click(null, null);
|
||||
case "BlackboardTool":
|
||||
return () => _mainWindow.ImageBlackboard_MouseUp(null, null);
|
||||
case "QuitDrawTool":
|
||||
return () => _mainWindow.CursorIcon_Click(null, null);
|
||||
case "Pen1":
|
||||
return () => SwitchToPenType(0);
|
||||
case "Pen2":
|
||||
return () => SwitchToPenType(1);
|
||||
case "Pen3":
|
||||
return () => SwitchToPenType(2);
|
||||
case "Pen4":
|
||||
return () => SwitchToPenType(3);
|
||||
case "Pen5":
|
||||
return () => SwitchToPenType(4);
|
||||
case "DrawLine":
|
||||
return () => _mainWindow.BtnDrawLine_Click(null, null);
|
||||
case "Screenshot":
|
||||
return () => _mainWindow.SaveScreenShotToDesktop();
|
||||
case "Hide":
|
||||
return () => _mainWindow.SymbolIconEmoji_MouseUp(null, null);
|
||||
case "Exit":
|
||||
return () => _mainWindow.KeyExit(null, null);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换到指定笔类型
|
||||
/// </summary>
|
||||
/// <param name="penTypeIndex">笔类型索引</param>
|
||||
private void SwitchToPenType(int penTypeIndex)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 通过反射访问主窗口的penType字段
|
||||
var penTypeField = _mainWindow.GetType().GetField("penType",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||||
|
||||
if (penTypeField != null)
|
||||
{
|
||||
penTypeField.SetValue(_mainWindow, penTypeIndex);
|
||||
|
||||
// 调用CheckPenTypeUIState方法更新UI状态
|
||||
var checkPenTypeMethod = _mainWindow.GetType().GetMethod("CheckPenTypeUIState",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
|
||||
|
||||
if (checkPenTypeMethod != null)
|
||||
{
|
||||
checkPenTypeMethod.Invoke(_mainWindow, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"切换到笔类型{penTypeIndex}时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region MainWindow Settings Management
|
||||
/// <summary>
|
||||
/// 隐藏主窗口的设置页面
|
||||
/// </summary>
|
||||
private void HideMainWindowSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 通过反射访问主窗口的设置面板
|
||||
var settingsBorder = _mainWindow.GetType().GetField("BorderSettings",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(_mainWindow) as System.Windows.Controls.Border;
|
||||
|
||||
if (settingsBorder != null)
|
||||
{
|
||||
settingsBorder.Visibility = System.Windows.Visibility.Collapsed;
|
||||
}
|
||||
|
||||
// 隐藏设置蒙版
|
||||
var settingsMask = _mainWindow.GetType().GetField("BorderSettingsMask",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(_mainWindow) as System.Windows.Controls.Border;
|
||||
|
||||
if (settingsMask != null)
|
||||
{
|
||||
settingsMask.Visibility = System.Windows.Visibility.Collapsed;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"隐藏主窗口设置页面时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 显示主窗口的设置页面
|
||||
/// </summary>
|
||||
private void ShowMainWindowSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
// 通过反射访问主窗口的设置面板
|
||||
var settingsBorder = _mainWindow.GetType().GetField("BorderSettings",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(_mainWindow) as System.Windows.Controls.Border;
|
||||
|
||||
if (settingsBorder != null)
|
||||
{
|
||||
settingsBorder.Visibility = System.Windows.Visibility.Visible;
|
||||
}
|
||||
|
||||
// 显示设置蒙版
|
||||
var settingsMask = _mainWindow.GetType().GetField("BorderSettingsMask",
|
||||
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)?.GetValue(_mainWindow) as System.Windows.Controls.Border;
|
||||
|
||||
if (settingsMask != null)
|
||||
{
|
||||
settingsMask.Visibility = System.Windows.Visibility.Visible;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"显示主窗口设置页面时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Window Event Handlers
|
||||
/// <summary>
|
||||
/// 窗口关闭事件处理
|
||||
/// </summary>
|
||||
private void HotkeySettingsWindow_Closed(object sender, EventArgs e)
|
||||
{
|
||||
// 恢复主窗口设置页面的显示
|
||||
ShowMainWindowSettings();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Event Handlers
|
||||
private void BtnClose_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
private void BtnResetToDefault_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = MessageBox.Show("确定要重置所有快捷键为默认设置吗?",
|
||||
"确认重置", MessageBoxButton.YesNo, MessageBoxImage.Question);
|
||||
if (result == MessageBoxResult.Yes)
|
||||
{
|
||||
// 重置为默认快捷键
|
||||
_hotkeyManager.RegisterDefaultHotkeys();
|
||||
|
||||
// 更新UI显示
|
||||
LoadCurrentHotkeys();
|
||||
|
||||
MessageBox.Show("快捷键已重置为默认设置。", "重置完成", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"重置快捷键时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
MessageBox.Show($"重置快捷键时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnSave_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 保存快捷键配置
|
||||
_hotkeyManager.SaveHotkeysToSettings();
|
||||
|
||||
MessageBox.Show("快捷键设置已保存。", "保存成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
||||
Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"保存快捷键设置时出错: {ex.Message}", LogHelper.LogType.Error);
|
||||
MessageBox.Show($"保存快捷键设置时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region Hotkey Changed Event Args
|
||||
/// <summary>
|
||||
/// 快捷键变更事件参数
|
||||
/// </summary>
|
||||
public class HotkeyChangedEventArgs : EventArgs
|
||||
{
|
||||
public string HotkeyName { get; set; }
|
||||
public Key Key { get; set; }
|
||||
public ModifierKeys Modifiers { get; set; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user