如何使用向上,向下,向左和向右移动表单正确的钥匙? [英] How do I move forms with up, down, left & right keys?

查看:95
本文介绍了如何使用向上,向下,向左和向右移动表单正确的钥匙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想用键盘上的箭头在屏幕上移动一个表格。



我尝试了什么:



首先我尝试了我已注释掉的行,然后我尝试了下面的其他代码,但我尝试过的任何解决方案都没有任何反应。



I just want to move a form around on the screen with the arrows on the keyboard.

What I have tried:

First i tried the lines i have comment out and then i have tried the other code below, but nothing happens with any of the solution i have tried.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace SC1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; }
        }

        Point lastPoint;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            lastPoint = new Point(e.X, e.Y);
        }

        //Move form to the Right
        //private void RightButton_Click(object sender, EventArgs e)
        //{
        //    this.Location = new Point(this.Location.X + 10, this.Location.Y);
        //}
        
        //Move form to the left:
        //private void LeftButton_Click(object sender, EventArgs e)
        //{
        //     this.Location = new Point(this.Location.X - 10, this.Location.Y);
        //}
  
        //Move form up:
        //private void UpButton_Click(object sender, EventArgs e)
        //{
        //     this.Location = new Point(this.Location.X, this.Location.Y - 10);
        //}  

        //Move form down:
        //private void DownButton_Click(object sender, EventArgs e)
        //{
        //    this.Location = new Point(this.Location.X, this.Location.Y + 10);
        //}

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            //capture up arrow key
            if (keyData == Keys.Up)
            {
                MessageBox.Show("You pressed Up arrow key");
                return true;
            }
            //capture down arrow key
            if (keyData == Keys.Down)
            {
                MessageBox.Show("You pressed Down arrow key");
                return true;
            }
            //capture left arrow key
            if (keyData == Keys.Left)
            {
                MessageBox.Show("You pressed Left arrow key");
                return true;
            }
            //capture right arrow key
            if (keyData == Keys.Right)
            {
                MessageBox.Show("You pressed Right arrow key");
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}

推荐答案

尝试:

Try:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
    if (keyData == Keys.Up)
        {
        Top -= 20;
        return true;
        }
    if (keyData == Keys.Down)
        {
        Top += 20;
        return true;
        }
    if (keyData == Keys.Left)
        {
        Left -= 20;
        }
    if (keyData == Keys.Right)
        {
        Left += 20;
        return true;
        }
    return base.ProcessCmdKey(ref msg, keyData);
    }


这篇关于如何使用向上,向下,向左和向右移动表单正确的钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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