当前位置: 首页 > news >正文

WPF Menu实现快捷键操作

很多小伙伴说,在Menu中,实现单个快捷键操作很简单,怎么实现多个快捷键操作和,组合快捷键呢,今天他来了。

上代码和效果图

一、Ctrl + Shift + 任意子母键实现快捷键组合

<Window x:Class="XH.TemplateLesson.MenuWindow"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:XH.TemplateLesson"mc:Ignorable="d"Title="MenuWindow" Height="450" Width="800"><Window.InputBindings><!-- 定义组合快捷键 Ctrl+Shift+S --><!--先要什么组合修改key 或者 Modifiers 即可注意:这里只可以改为Ctrl Alt Shift Windows + 任意字母键的快捷方式--><KeyBinding Key="S" Modifiers="Control+Shift" Command="{Binding NewCommand}" /></Window.InputBindings><Grid><Menu><MenuItem Header="新建(_F)"><MenuItem Header="新建" /><MenuItem Header="全部保存" Command="{Binding NewCommand}" InputGestureText="Ctrl+Shift+S"  /></MenuItem><MenuItem Header="编辑(_E)"/></Menu></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace XH.TemplateLesson
{/// <summary>/// MenuWindow.xaml 的交互逻辑/// </summary>public partial class MenuWindow : Window{public ICommand NewCommand { get; set; }public MenuWindow(){InitializeComponent();DataContext = this;NewCommand = new RelayCommand(ExecuteNew);}private void ExecuteNew(object parameter){MessageBox.Show("New Command Executed");}public class RelayCommand : ICommand{private readonly Action<object> _execute;private readonly Func<object, bool> _canExecute;public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null){_execute = execute;_canExecute = canExecute;}public bool CanExecute(object parameter){return _canExecute == null || _canExecute(parameter);}public void Execute(object parameter){_execute(parameter);}public event EventHandler CanExecuteChanged{add { CommandManager.RequerySuggested += value; }remove { CommandManager.RequerySuggested -= value; }}}}
}

直接Ctrl + Shift + S弹出:

二、实现Ctrl + 多个字母键组合

<Window x:Class="XH.TemplateLesson.MenuWindow"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:XH.TemplateLesson"mc:Ignorable="d" <!--绑定键盘事件-->PreviewKeyDown="Window_PreviewKeyDown"PreviewKeyUp="Window_PreviewKeyUp"Title="MenuWindow" Height="450" Width="800"><Grid><Menu><MenuItem Header="新建(_F)"><MenuItem Header="新建" /><MenuItem Header="整理代码格式" InputGestureText="Ctrl+K+D"  /></MenuItem></Menu></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace XH.TemplateLesson
{/// <summary>/// MenuWindow.xaml 的交互逻辑/// </summary>public partial class MenuWindow : Window{private bool _isCtrlPressed;private bool _isAPressed;public MenuWindow(){InitializeComponent();}// 下方的K 和D 根据自己写的代码来进行修改private void Window_PreviewKeyDown(object sender, KeyEventArgs e){// 先 Cttrlif (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl){_isCtrlPressed = true;}// 再 Ctrl + K else if (e.Key == Key.K && _isCtrlPressed){_isAPressed = true;}// 最后 Cttrl + K + Delse if (e.Key == Key.D && _isCtrlPressed && _isAPressed){ExecuteCustomCommand();}}// 键盘释放 如果释放 无效private void Window_PreviewKeyUp(object sender, KeyEventArgs e){if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl){_isCtrlPressed = false;}else if (e.Key == Key.K){_isAPressed = false;}}private void ExecuteCustomCommand(){MessageBox.Show("快捷键调用成功: Ctrl + K + D");}}
}

按下Ctrl + K + D:

单键 Alt + 字母弹出

<Menu><!--直接下划线 + 字母 就可以alt + 字母弹出--><MenuItem Header="新建(_F)"><MenuItem Header="新建" /><MenuItem Header="整理代码格式" InputGestureText="Ctrl+K+D"  /></MenuItem>
</Menu>

演示 :

好了,以上就是WPF中Menu控件中,所有快捷键的使用方法了,感觉有用的话,点个赞呗。

http://www.lryc.cn/news/391918.html

相关文章:

  • VSCode常用的一些插件
  • OV通配符证书用于什么单位
  • 【数据结构】06.栈队列
  • 完全理解C语言函数
  • 性能测试:JMeter与Gatling的高级配置
  • Linux 软件管理
  • 五.核心动画 - 图层的变换(平移,缩放,旋转,3D变化)
  • Linux系统编程——线程基本概念
  • 【HALCON】如何实现hw窗口自适应相机拍照成像的大小
  • 【Spring cloud】 认识微服务
  • 一个pdf分割成多个pdf,一个pdf分成多个pdf
  • rtsp client c++
  • 实现好友关注功能的Feed流设计
  • 【STM32修改串口波特率】
  • 印章谁在管、谁用了、用在哪?契约锁让您打开手机一看便知
  • [C++初阶]vector的初步理解
  • 【等保2.0是什么意思?等保2.0的基本要求有哪些? 】
  • VMware中的三种虚拟网络模式
  • 深度学习基准模型Transformer
  • 如何实现公网环境远程连接本地局域网宝塔FTP服务远程管理文件
  • dledger原理源码分析系列(一)-架构,核心组件和rpc组件
  • Github 2024-07-05开源项目日报 Top10
  • WHAT - React useEffect 依赖的 Object.is
  • 【Java EE】Spring IOCDI
  • 【FreeRTOS】同步互斥与通信 有缺陷的同步示例
  • Lambda表达式讲解
  • 深入了解Linux中的dnsmasq:配置与优化指南
  • 【React】Ant Design -- Table分页功能实现
  • 400G SR4和800G SR8光模块在AI集群中的应用
  • ARM功耗管理软件之DVFSAVS