winForm 应用开发,大都离不开winFrom + toolBar/Menu + some other Controls的模式。
其中快捷键的处理,我5年前就遇到了,现在发现还有人犯这个错误,觉得着实需要讲一下:
很多输入型控件都支持常用的Ctrl + C, Ctrl + V, Ctrl + X等热键,WinForm 在KeyPreview=true时也会捕捉这些快捷键被触发的Windows消息。

 

如果winFrom/ToolBar/Menu/Other Control这些控件,对快捷键都有处理,上级控件只要设置e.SuppressKeyPress属性值(Boolean 类型),就可以选择是否把key down消息传递给下级控件。这里的上下级是按控件的包含关系来说的。如winForm上有一个TextBox,winForm为上级控件,TextBox为下级控件。

唯一的例外是ToolBar/Menu控件,它最先截获消息,而且默认情况下,是它触发后,不再把消息往别的控件传送!

 

 

 

也就是说一旦快捷键设置在Menu上,除了相应的menuItem外,其他控件(包括winForm)将都收不到快捷键被触发的消息。这也是最容易导致bug的地方。为解决这个问题,我一般是在menuitem.click的处理事件中通过sendkeys.send()方法,把消息继续传递下去。

 

https://files.cnblogs.com/ColorSky/shortKey.zip

private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Menu item click event is trigged!");
            SendKeys.Send("^V");
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.V)
            {
                Console.WriteLine("Form Key Down event is trigged!");
                //e.SuppressKeyPress = true;
            }
        }

我做了一个事列程序,只在一个winForm上放了 menu 和 textbox控件,其中一个menuItem.shortkey属性设置为Ctrl + V。
在menuItem.click和winForm.Keydown事件中都加上Console.WriteLine()。

 

示例程序中很明显的说明了这个问题,outPut窗口的输出结果是:

Menu item click event is triggered!
Form Key Down event is triggered!

posted on 2012-11-08 21:11  Asa.Zhu  阅读(1228)  评论(0编辑  收藏  举报