黄泉快递 2021-06-21 03:24 采纳率: 66.7%
浏览 80
已采纳

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

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

  • 写回答

4条回答 默认 最新

  • CSDN专家-cpp_learner 2021-06-21 09:05
    关注

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

    这是我刚写的代码,控制两张图片同时移动的,你也可以新加一些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;
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效