posts - 124,  comments - 29,  trackbacks - 0
        某些键,如 TabReturnEsc箭头键,由控件自动处理。为使这些键引发 KeyDown 事件,必须在窗体上的每个控件中重写 IsInputKey 方法。用于重写 IsInputKey 的代码需要确定是否按下了某个特殊键,并且需要返回一个 true 值。
 1class MyButton :System.Windows.Forms.Button
 2    {
 3        protected override bool IsInputKey(System.Windows.Forms.Keys keyData)
 4        {
 5            if (keyData == System.Windows.Forms.Keys.Left ||
 6                keyData == System.Windows.Forms.Keys.Right)
 7                return true;
 8            return base.IsInputKey(keyData);
 9        }

10    }
重写之后就可以让Button控件KeyDown事件中箭头键响应了。

 1        private void button1_KeyDown(object sender, KeyEventArgs e)
 2        {
 3            if(e.KeyCode == Keys.Left)
 4            {
 5                if (button1.Location.X >=2)
 6                {
 7                    button1.Location = new Point(button1.Location.X - 2, button1.Location.Y) ;
 8                }

 9            }

10            if (e.KeyCode == Keys.Right)
11            {
12                if (button1.Location.X <= 500)
13                {
14                    button1.Location = new Point(button1.Location.X + 2, button1.Location.Y);
15                }

16            }

17        }

posted on 2008-06-14 11:08 天书 阅读(4596) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2008年10月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(5)

随笔档案

文章分类

文章档案

好友的Bolg

搜索

  •  

最新评论

阅读排行榜

评论排行榜