当前位置:编程学习 > C#/ASP.NET >>

键盘移动窗体

我想通过键盘方向健移动窗体,下面是代码:
 private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Point point = this.Location;
           
            switch (e.KeyData)
            {
                case Keys.Up:
                    point.Y -= 1;
                    break;
                case Keys.Down:
                    point.Y += 1;
                    break;
                case Keys.Right:
                    point.X += 1;
                    break;
                case Keys.Left:
                    point.X -= 1;
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                default:
                    break;
            }
            this.Location = point;
        }
事件也激活了,但是按方向键却没有反应,乍回事呀? --------------------编程问答-------------------- Form1.KeyPreview = true;

默认窗体不接收按键事件... --------------------编程问答-------------------- 不行呀,出错,要求对象引用,你再看看。 --------------------编程问答-------------------- 有点小问题,现在是不出错了,但就是窗体没有反应 --------------------编程问答-------------------- private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Point point = this.Location;
           
            switch (e.KeyData)
            {
                case Keys.Up:
                    this.Location=new Point(point.X,point.Y-1);
                    break;
                case Keys.Down:
                    this.Location=new Point(point.X,point.Y+1);
                    break;
                case Keys.Right:
                    this.Location=new Point(point.X+1,point.Y);
                    break;
                case Keys.Left:
                    this.Location=new Point(point.X-1,point.Y);
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                default:
                    break;
            }
        }
--------------------编程问答-------------------- up down那个几键系统自动捕获了,用来切换按钮
你窗体上有Button吧 --------------------编程问答-------------------- Mark --------------------编程问答-------------------- 是呀,我窗体是有按纽,我把按纽去了就行了?难道就是你说的系统自动捕获了?谢谢了!!!
又长了点知识 --------------------编程问答-------------------- 呵呵!我也正做它呢,先参考一下!
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,