C# 怎么控制两张图片的移动

C# 怎么控制两张图片的移动,比如第一张图片移动是字母AWSD控制,而第二张图片又是↑↓←→控制移动,而且,如果同时要按下字母和箭头,那么两张图片都要会动,大佬教教小白

 如果符合你的需求,麻烦点个采纳,谢谢!

这是我刚写的代码,控制两张图片同时移动的,你也可以新加一些if判断,可以控制图片单独移动!

 

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 两张图片同时移动 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();

            // 绑定键盘按下事件
            this.PreviewKeyDown += Form1_PreviewKeyDown;

            // 往两个控件添加图片
            this.pictureBox1.Load("007.jpg");
            // 设置图片自动缩放符合控件大小
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.pictureBox2.Load("007.jpg");
            this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

        }

        private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            if (e.KeyCode == Keys.W) {
                int x = pictureBox1.Location.X;
                int y = pictureBox1.Location.Y - 5;
                Point point = new Point(x, y);
                pictureBox1.Location = point;

                int x1 = pictureBox2.Location.X;
                int y1 = pictureBox2.Location.Y - 5;
                Point point1 = new Point(x1, y1);
                pictureBox2.Location = point1;

            } else if (e.KeyCode == Keys.S) {
                int x = pictureBox1.Location.X;
                int y = pictureBox1.Location.Y + 5;
                Point point = new Point(x, y);
                pictureBox1.Location = point;

                int x1 = pictureBox2.Location.X;
                int y1 = pictureBox2.Location.Y + 5;
                Point point1 = new Point(x1, y1);
                pictureBox2.Location = point1;

            } else if (e.KeyCode == Keys.A) {
                int x = pictureBox1.Location.X - 5;
                int y = pictureBox1.Location.Y;
                Point point = new Point(x, y);
                pictureBox1.Location = point;

                int x1 = pictureBox2.Location.X - 5;
                int y1 = pictureBox2.Location.Y;
                Point point1 = new Point(x1, y1);
                pictureBox2.Location = point1;

            } else if (e.KeyCode == Keys.D) {
                int x = pictureBox1.Location.X + 5;
                int y = pictureBox1.Location.Y;
                Point point = new Point(x, y);
                pictureBox1.Location = point;

                int x1 = pictureBox2.Location.X + 5;
                int y1 = pictureBox2.Location.Y;
                Point point1 = new Point(x1, y1);
                pictureBox2.Location = point1;
            }
        }
    }
}

 

直接设置x,y就好了呀

用一个Dictionary记录按键状态,KeyDown,KeyUp设置Dictionary按键状态,FormLoad时开启2个线程定时检查Dictionary状态执行相关操作就行。

 

这样! 

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 两张图片同时移动 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();

            // 绑定键盘按下事件
            this.PreviewKeyDown += Form1_PreviewKeyDown;

            // 往两个控件添加图片
            this.pictureBox1.Load("007.jpg");
            // 设置图片自动缩放符合控件大小
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            this.pictureBox2.Load("007.jpg");
            this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;

        }

        private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            
            switch (e.KeyCode) {
                case Keys.W: {
                    int x = pictureBox1.Location.X;
                    int y = pictureBox1.Location.Y - 5;
                    Point point = new Point(x, y);
                    pictureBox1.Location = point;
                }
                break;
                case Keys.S: {
                    int x = pictureBox1.Location.X;
                    int y = pictureBox1.Location.Y + 5;
                    Point point = new Point(x, y);
                    pictureBox1.Location = point;
                }
                break;
                case Keys.A: {
                    int x = pictureBox1.Location.X - 5;
                    int y = pictureBox1.Location.Y;
                    Point point = new Point(x, y);
                    pictureBox1.Location = point;
                }
                break;
                case Keys.D: {
                    int x = pictureBox1.Location.X + 5;
                    int y = pictureBox1.Location.Y;
                    Point point = new Point(x, y);
                    pictureBox1.Location = point;
                }
                break;
                case Keys.Up: {
                    int x1 = pictureBox2.Location.X;
                    int y1 = pictureBox2.Location.Y - 5;
                    Point point1 = new Point(x1, y1);
                    pictureBox2.Location = point1;
                }
                break;
                case Keys.Down: {
                    int x1 = pictureBox2.Location.X;
                    int y1 = pictureBox2.Location.Y + 5;
                    Point point1 = new Point(x1, y1);
                    pictureBox2.Location = point1;
                }
                break;
                case Keys.Left: {
                    int x1 = pictureBox2.Location.X - 5;
                    int y1 = pictureBox2.Location.Y;
                    Point point1 = new Point(x1, y1);
                    pictureBox2.Location = point1;
                }
                break;
                case Keys.Right: {
                    int x1 = pictureBox2.Location.X + 5;
                    int y1 = pictureBox2.Location.Y;
                    Point point1 = new Point(x1, y1);
                    pictureBox2.Location = point1;
                }
                break;
                default:
                    break;
            }
        }
    }
}