首页 > 其他分享 >WPF Menu实现快捷键操作

WPF Menu实现快捷键操作

时间:2024-07-05 14:26:35浏览次数:3  
标签:Windows Menu System private 快捷键 Key using WPF public

很多小伙伴说,在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)
        {
            // 先 Cttrl
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
            {
                _isCtrlPressed = true;
            }
            // 再 Ctrl + K 
            else if (e.Key == Key.K && _isCtrlPressed)
            {
                _isAPressed = true;
            }
            // 最后 Cttrl + K + D
            else 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控件中,所有快捷键的使用方法了,感觉有用的话,点个赞呗。

标签:Windows,Menu,System,private,快捷键,Key,using,WPF,public
From: https://blog.csdn.net/qq_48148522/article/details/140188978

相关文章

  • WPF DataContext
    后台代码:publicclassStudent{publicintId{get;set;}publicstringName{get;set;}publicintAge{get;set;}} 前台代码:<Windowx:Class="BindingTest.MainWindow"xmlns="http://schem......
  • 学懂C#编程:WPF应用开发系列——WPF之ComboBox控件的详细用法
    WPF(WindowsPresentationFoundation)中的ComboBox控件是一个下拉列表控件,允许用户从一组预定义的选项中选择一个选项。以下是ComboBox控件的详细用法,并附带示例说明。ComboBox的基本用法1.XAML定义:在XAML中定义一个ComboBox控件,并添加一些选项。<Windowx:Class="ComboBox......
  • WPF Performance Suite, Microsoft Windows Performance Toolkit
    Copyfrom https://www.cnblogs.com/lindexi/p/12086719.htmlhttps://learn.microsoft.com/en-us/previous-versions/aa969767(v=vs.110) 1.Downloadurl:  https://download.microsoft.com/download/A/6/A/A6AC035D-DA3F-4F0C-ADA4-37C8E5D34E3D/setup/WinSDKPerformanceT......
  • WPF Datagrid ContextMenu MenuItem Command CommandParameter MultiBinding
     //xaml<Windowx:Class="WpfApp194.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • 推荐一款Win11主题WPF UI框架
    最近在微软商店,官方上架了新款Win11风格的WPF版UI框架【WPFGalleryPreview1.0.0.0】,这款应用引入了前沿的FluentDesignUI设计,为用户带来全新的视觉体验。WPFGallery简介做为一关注前沿资讯的开发人员,首先关注的是应用WPFGallery到我们的程序中,需要具备的条件:.N......
  • WPF AllowsTransparency DragMove
     //xaml<Windowx:Class="WpfApp191.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas......
  • WPF open image and print as pdf file
    //xaml<Windowx:Class="WpfApp189.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF OpenFielDialog Filter InitialiaDirectory Title
    //xaml<Windowx:Class="WpfApp189.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF Prism PubSubEvent(订阅)
    Prism提供了事件聚合器(EventAggregator)来实现事件的订阅和发布,允许模块之间进行松耦合的通信。主要作用:解耦合:通过事件订阅和发布,模块之间可以实现解耦合,避免直接依赖于彼此的实现细节。示例用法:定义事件类:publicclassMessageEvent:PubSubEvent<string>{}订......
  • WPF进度条中间写百分比数字
    我发现很多同学把思维固话了,通常我们需要实现的进度条是我在网上看到好多例子,但是都没有我的简单,他们不是重写ProcessBar就是使用模板,可以将TextBlock提取出来啊,灵活一点单独绑定然后一句代码Panel.ZIndex="1"就搞定了<StackPanel><ButtonContent="执行耗时......