加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Windows > 正文

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

发布时间:2021-05-22 17:20:24 所属栏目:Windows 来源:网络整理
导读:在我的 Windows窗体应用程序中,我希望一个特殊的按钮可以在我按下它的时候运行测试.有几十个控件,因此在每个控件中实现它需要太多时间. 有没有办法我可以设置一个热键,所以无论我在应用程序中做什么,我可以按键,它会启动我的活动? 您可以覆盖ProcessCmdKe

在我的 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);
}

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读