Windows Forms .NET中的热键(非全局)

前端之家收集整理的这篇文章主要介绍了Windows Forms .NET中的热键(非全局)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 Windows窗体应用程序中,我希望一个特殊的按钮可以在我按下它的时候运行测试.有几十个控件,因此在每个控件中实现它需要太多时间.

有没有办法我可以设置一个热键,所以无论我在应用程序中做什么,我可以按键,它会启动我的活动?

您可以覆盖ProcessCmdKey并在控件或表单中处理您的热键.

MSDN

The ProcessCmdKey method first
determines whether the control has a
ContextMenu,and if so,enables the
ContextMenu to process the command
key. If the command key is not a menu
shortcut and the control has a parent,
the key is passed to the parent’s
ProcessCmdKey method. The net effect
is that command keys are “bubbled” up
the control hierarchy. In addition to
the key the user pressed,the key data
also indicates which,if any,modifier
keys were pressed at the same time as
the key. Modifier keys include the
SHIFT,CTRL,and ALT keys.

例如:

protected override bool ProcessCmdKey(ref Message msg,Keys keyData)
{
    // if it is a hotkey,return true; otherwise,return false
    switch (keyData)
    {
        case Keys.Control | Keys.C:
            // do something
            return true;
        default:
            break;
    }

    return base.ProcessCmdKey(ref msg,keyData);
}

猜你在找的Windows相关文章