尝试通过使用ProcessCmdKey以MDI父/子形式和其他形式实现全局键盘快捷方式 [英] Try to implement global keyboard shortcut in MDI parent/child form and other form by using ProcessCmdKey

查看:97
本文介绍了尝试通过使用ProcessCmdKey以MDI父/子形式和其他形式实现全局键盘快捷方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MDI父窗体类中重写ProcessCmdKey(),并且在同一类中有一些键盘快捷键调用方法.但是我希望使这些热键能够以父/子形式和其他形式工作.现在的情况是,当专注于其他形式(常规形式,而不是MDI)时,ProcessCmdKey()不再捕获键盘.我应该将哪个类放入ProcessCmdKey()并使其有效?谢谢!

I override ProcessCmdKey() in my MDI parent form class and have some keyboard shortcut calling method in same class. But I wish to make these hotkeys working in parent/child form and other form. The case now is when focus on the other form(regular form, not MDI), ProcessCmdKey() doesn't capture keyboard anymore. Which class should I put ProcessCmdKey() in and anything to make it work? Thanks!

namespace myNamespace
{
    public class MDIParent : System.Windows.Forms.Form
    {

        public bool NextTab(){...}
        public bool PreviousTab(){...}

        protected override bool ProcessCmdKey(ref Message message, Keys keys)
        {
            switch (keys)
            {
                case Keys.Control | Keys.Tab:
                    NextTab();
                    return true;

                case Keys.Control | Keys.Shift | Keys.Tab:
                    PreviousTab();
                    return true;
            }
            return base.ProcessCmdKey(ref message, keys);
        }
    }

    public class mySecondForm : System.Windows.Forms.Form
    {
        ...
    }
}

推荐答案

您可以在实现ProcessCmdKey处理程序的情况下定义基本"表单,然后创建所有其他表单:MDI父级,MDI父级的子窗口,并且您创建的任何独立"表单(即不是MDI表单的子表单)都将从基本表单"继承.只需确保在希望成为MDI的表单上设置了IsMdiContainer属性,并且添加到MDI表单的子窗口不是TopLevel,而是将其父窗口设置为MDI表单.

You can define a "base" Form with your ProcessCmdKey handler implemented, and then make all your other Forms : the MDI Parent, the Child Windows of the MDI Parent, and any "Independent" Forms you create (i.e., not children of the MDI Form) inherit from the "base Form." Just make sure the IsMdiContainer property is set on the form you wish to be MDI, and the child windows you add to the MDI Form are not TopLevel and have their parent set to the MDI Form.

然后的问题是,您想在哪里处理由您启用的键组合触发的事件,因为...如果您在基本Form中定义了要由捕获的键组合触发的方法...每个从基本Form继承的Form将在它们自己的上下文中执行它们.

The question is, then, where do you want to handle the events triggered by the key combinations you've enabled because ... if you define methods to be triggered by the trapped key combinations in the base Form ... each Form that inherits from the base Form is going to execute them in their own context.

如果要在整个应用程序范围内处理捕获的键组合,请使用定义为静态方法的键组合处理程序实现一个静态公共类.或者,由于您可能想知道从哪种形式发出的特殊键组合,只需将指向Form的指针传递给静态处理程序即可.

If you want to handle the trapped key-combinations on an application wide basis, then implement a static public class with the key-combination handlers defined as static methods there. Or, since you may want to know from which form the special key-combinations issued just pass a pointer to calling Form to the static handler.

因此,您在基本Form中的ProcessCmdKey覆盖中用于控件+ Tab的处理程序可能看起来像这样:

So, your handler for the control + Tab in the ProcessCmdKey override in the base Form might look like this :

    // in ProcessCmdKey override in base Form
    case Keys.Control | Keys.Tab:
        KeyHandler.NextTabHandler(this);
        return true;

您的静态类可能看起来像这样:

Your static class might look something like this :

public static class KeyHandler
{
    public static void NextTabHandler(Form theCallingForm)
    {
        Console.WriteLine("called from : " + theCallingForm.Text + " : ActiveControl : " + theCallingForm.ActiveControl.Name);

        if (theCallingForm is MDIForm)
        {
            // handle Next Tab on MDIForm control
        }
        else if (theCallingForm is childForm)
        {
            // handle Next Tab on ChildForm control
        }
        else
        {
            if(theCallingForm is independentForm)
            {
                // handle Next Tab on "independent Form" control
            }
        }
    }
}

如上面的代码所示,可以使用调用Form的ActiveControl属性来了解给定类型的Form上的哪个控件获得了组合键.

As you can see in the code above, you can use the ActiveControl property of the calling Form to know which control on a given type of Form got the key-combination.

当然,如果您不想像这样全局"地处理键组合,只需根据需要在其他Forms中插入您的ProcessCmdKey替代,不要让它们继承自基本Form

Of course, if you don't want to handle the key-combinations "globally," like this, just insert your ProcessCmdKey over-rides as needed in the other Forms, and don't have them inherit from the base Form.

处理应用程序范围内"的关键事件可能是,也可能不是您特定解决方案的最佳策略,但这是可行的策略.最好,

Handling the key events "application wide" may, or may not be the best strategy for your particular solution, but it is a viable strategy. best,

这篇关于尝试通过使用ProcessCmdKey以MDI父/子形式和其他形式实现全局键盘快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆