微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 是否有一种流畅的方法来处理WinForm事件?

当我想知道它是否存在用于键入用于处理事件的流畅代码的库时,我正在处理用户控件中的另一个KeyDown事件
editor.When(Keys.F).IsDown().With(Keys.Control).Do((sender,e) => ShowFindWindow());

那存在吗?

解决方法

我认为这是一个挑战,这可以让我了解Fluent是什么,所以我编写了一个Fluent键盘类.我不认为我遵循所有流利的语言结构和规则,但它有效.

助手扩展方法

/// <summary>
/// Provides a set of static (Shared in Visual Basic) methods for starting a fluent expression on a <see cref="System.Windows.Form.Control"/> object.
/// </summary>
public static class ControlExtensions
{
    /// <summary>
    /// Starts a fluent expression that occurs when a key is pressed.
    /// </summary>
    /// <param name="control">The control on which the fluent keyboard expression is occuring.</param>
    /// <param name="keys">The key when it will happen.</param>
    /// <returns>A <see cref="KeyboardFluent"/> object that makes it possible to write a fluent expression.</returns>
    public static KeyboardFluent When(this Control control,Keys keys)
    {
        return new KeyboardFluent(control).When(keys);
    }
}

流利的课程

/// <summary>
/// Represents a fluent expression for handling keyboard event.
/// </summary>
public class KeyboardFluent
{
    /// <summary>
    /// The control on which the fluent keyboard expression is occuring.
    /// </summary>
    private Control control;

    /// <summary>
    /// The KeyDown and KeyUp handler.
    /// </summary>
    private KeyEventHandler keyHandler;

    /// <summary>
    /// Stores if the IsDown method was called and that the KeyDown event is registered.
    /// </summary>
    private bool isDownRegistered = false;

    /// <summary>
    /// Stores if the IsUp method was called and that the KeyUp event is registered.
    /// </summary>
    private bool isUpRegistered = false;

    /// <summary>
    /// The list of keys that will make the actions be executed when they are down or up.
    /// </summary>
    private List<Keys> triggerKeys;

    /// <summary>
    /// The modifiers keys that must be down at the same time than the trigger keys for the actions to be executed.
    /// </summary>
    private Keys modifiers;

    /// <summary>
    /// The list of actions that will be executed when the trigger keys and modifiers are down or up.
    /// </summary>
    private List<Action<object,KeyEventArgs>> actions;

    /// <summary>
    /// Initializes a new instance of the <see cref="KeyboardFluent"/> class.
    /// </summary>
    /// <param name="control">The control on which the fluent keyboard expression is occuring.</param>
    public KeyboardFluent(Control control)
    {
        this.control = control;
        this.triggerKeys = new List<Keys>();
        this.actions = new List<Action<object,KeyEventArgs>>();
        this.keyHandler = new KeyEventHandler(OnKeyHandler);
    }

    /// <summary>
    /// Handles the KeyDown or KeyUp event on the control.
    /// </summary>
    /// <param name="sender">The control on which the event is occuring.</param>
    /// <param name="e">A <see cref="KeyEventArgs"/> that gives information about the keyboard event.</param>
    private void OnKeyHandler(object sender,KeyEventArgs e)
    {
        if (this.triggerKeys.Contains(e.KeyCode) && e.Modifiers == this.modifiers)
        {
            this.actions.ForEach(action => action(sender,e));
        }
    }

    /// <summary>
    /// Makes the keyboard event occured when a key is pressed down.
    /// </summary>
    /// <returns>Returns itself to allow a fluent expression structure.</returns>
    public KeyboardFluent IsDown()
    {
        if (!isDownRegistered)
        {
            this.control.KeyDown += this.keyHandler;
            isDownRegistered = true;
        }
        return this;
    }

    /// <summary>
    /// Makes the keyboard event occured when a key is pressed up.
    /// </summary>
    /// <returns>Returns itself to allow a fluent expression structure.</returns>
    public KeyboardFluent IsUp()
    {
        if (!isUpRegistered)
        {
            this.control.KeyUp += this.keyHandler;
            isUpRegistered = true;
        }
        return this;
    }

    /// <summary>
    /// Creates a new trigger on a key.
    /// </summary>
    /// <param name="key">The key on which the actions will occur.</param>
    /// <returns>Returns itself to allow a fluent expression structure.</returns>
    public KeyboardFluent When(Keys key)
    {
        this.triggerKeys.Add(key);
        return this;
    }

    /// <summary>
    /// Adds a modifier filter that is checked before the action are executed.
    /// </summary>
    /// <param name="modifiers">The modifier key.</param>
    /// <returns>Returns itself to allow a fluent expression structure.</returns>
    public KeyboardFluent With(Keys modifiers)
    {
        this.modifiers |= modifiers;
        return this;
    }

    /// <summary>
    /// Executes the action when the specified keys and modified are either pressed down or up.
    /// </summary>
    /// <param name="action">The action to be executed.</param>
    /// <returns>Returns itself to allow a fluent expression structure.</returns>
    public KeyboardFluent Do(Action<object,KeyEventArgs> action)
    {
        this.actions.Add(action);
        return this;
    }
}

我现在可以打字了

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.When(Keys.F).With(Keys.Control).IsDown().Do((sender,e) => MessageBox.Show(e.KeyData.ToString()));
    }
}

只有在Ctrl F关闭时它才会显示消息框.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


原文地址:http://msdn.microsoft.com/en-us/magazine/cc163791.aspx 原文发布日期: 9/19/2005 原文已经被 Microsoft 删除了,收集过程中发现很多文章图都不全,那是因为原文的图都不全,所以特收集完整全文。 目录 前言 CLR启动程序
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采纳和使用,它的确提供了很多的优势也解决了很多的问题,但是我们也知道也并不是银弹,提供优势的同时它也给我们的开发人员和团队也带来了很多的挑战。 为了迎接或者采用这些新技术,开发团队需要更加注重一些流程或工具的使用,这样才能更好的适应这些新技术所
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下PLINQ中的分区。上一篇介绍了并行编程,这边详细介绍一下并行编程中的分区和自定义分区。 先做个假设,假设我们有一个200Mb的文本文件需要读取,怎么样才能做到最优的速度呢?对,很显然就是拆分,把文本文件拆分成很多个小文件,充分利用我们计算机中
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Microsoft为了利用这个硬件特性,于是在Visual Studio 2010 和 .NET Framework 4的发布及以上版本中,添加了并行编程这个新特性,我想它以后势必会改变我们的开发方式。 在以前或者说现在,我们在并行开发的时候可
c语言输入成绩怎么判断等级
字符型数据在内存中的存储形式是什么
c语言怎么求字符串的长度并输出
c语言函数的三种调用方式是什么
c语言中保留两位小数怎么表示
double的输入格式符是什么