首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将PictureBox在TableLayoutPanel网格中从键盘上移动?

如何将PictureBox在TableLayoutPanel网格中从键盘上移动?
EN

Stack Overflow用户
提问于 2015-12-30 20:53:24
回答 2查看 618关注 0票数 2

我在C#很新,但我正在尝试移动一个picturebox,从键盘上按下左右键,在网格TableLayoutPanel中(我在运行时创建了网格)。网格为23x23,具有块图像边框(边框中的每个单元格都包含图像框中的块图像)和中间有鼠标的图像框。我要做的是通过按上面提到的一个控制键,将鼠标图像从中央单元格( 11x11)移动到另一个单元格中。似乎我无法控制eventHandler的想法.。在我想让picturebox移动之前,代码运行得很好。我已经将整个代码放到了一个我没有注意到的地方,但我认为问题是KeyDown += new KeyEventHandler(Form2_KeyDown)或/EndPrivatevoidForm2_KeyDown(对象发送方,KeyEventArgs e){.}。

代码语言:javascript
运行
复制
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 IndividualProject
{
  public partial class Form2 : Form
  {
    PictureBox picturebox5 = new PictureBox
    {
        Visible = true,
        Anchor = AnchorStyles.Top,
        SizeMode = PictureBoxSizeMode.Normal,
        Dock = DockStyle.Fill,
        Margin = new Padding(0)
    };

    public Form2()
    {
        InitializeComponent();
       // MaximizeBox = false;

        //size
        int h = Screen.PrimaryScreen.WorkingArea.Height / 2;
        int w = Screen.PrimaryScreen.WorkingArea.Width / 2;
        Size = new Size(w / 2 + w / 7, h + h / 4 + h / 7);

        //location
        StartPosition = FormStartPosition.CenterScreen;

        //form style
        FormBorderStyle = FormBorderStyle.FixedSingle;

        //menuStrip1.BackColor = Color.Beige;

        //lives and score container
        #region livesAndScore
        lasContainer.Size = new Size(Width / 2 + Width / 3 + Width / 7, Height / 13);
        lasContainer.BackColor = Color.Lavender;
        lasContainer.BorderStyle = BorderStyle.Fixed3D;
        lasContainer.Dock = DockStyle.Top;
        lasContainer.SplitterDistance = Width / 2 - Width / 69;
          //labels
        lives.Location = new Point(lasContainer.Panel1.Width / 12, lives.Height / 2);
        score.Location = new Point(lasContainer.Panel2.Width / 12, score.Height / 2);
          //picturebox
        live3.Location = new Point(lasContainer.Panel1.Width / 3, lives.Height / 2);
        live2.Location = new Point(lasContainer.Panel1.Width / 2, lives.Height / 2);
        live1.Location = new Point(lasContainer.Panel1.Width / 2 + lasContainer.Panel1.Width / 6, lives.Height / 2);
        #endregion livesAndScore

        //gamePanel
        gamePanel.Dock = DockStyle.Fill;
        gamePanel.BackColor = Color.SkyBlue;
        gamePanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; // REMOVE WHEN FINISHED !!!!!!!!!!!

        //making the grid
        #region grid
        gamePanel.ColumnCount = 23;
        gamePanel.RowCount = 23;
        gamePanel.ColumnStyles.Clear();
        gamePanel.RowStyles.Clear();
        int iIndex, jIndex = 0;

        for (iIndex = 0; iIndex < gamePanel.ColumnCount; iIndex++)
        {
            gamePanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 4.34F));
            gamePanel.RowStyles.Add(new RowStyle(SizeType.Percent, 4.34F));
        }
        #endregion grid

        while(jIndex < gamePanel.ColumnCount)
        {
            #region picturebox1
            PictureBox picturebox1 = new PictureBox
            {
                Visible = true,
                Anchor = AnchorStyles.Top,
                SizeMode = PictureBoxSizeMode.Normal,
                BackColor = Color.Sienna,
                Dock = DockStyle.Fill,
                Margin = new Padding(0)
            };
            if(jIndex < gamePanel.ColumnCount - 1)
            {
                gamePanel.Controls.Add(picturebox1, jIndex, 0);
                picturebox1.ImageLocation = @"..\..\ResourcesPh\brickblock.png";
            }
            #endregion picturebox1

            #region picturebox2
            PictureBox picturebox2 = new PictureBox
            {
                Visible = true,
                Anchor = AnchorStyles.Top,
                SizeMode = PictureBoxSizeMode.Normal,
                BackColor = Color.Sienna,
                Dock = DockStyle.Fill,
                Margin = new Padding(0)
            };
            if (jIndex < gamePanel.ColumnCount - 1)
            {
                gamePanel.Controls.Add(picturebox2, 0, jIndex + 1);
                picturebox2.ImageLocation = @"..\..\ResourcesPh\brickblock.png";
            }
            #endregion picturebox2

            #region picturebox3
            PictureBox picturebox3 = new PictureBox
            {
                Visible = true,
                Anchor = AnchorStyles.Top,
                SizeMode = PictureBoxSizeMode.Normal,
                BackColor = Color.Sienna,
                Dock = DockStyle.Fill,
                Margin = new Padding(0)
            };
            if(jIndex < gamePanel.ColumnCount - 1)
            {
                gamePanel.Controls.Add(picturebox3, gamePanel.ColumnCount - 1 - jIndex, gamePanel.RowCount - 1);
                picturebox3.ImageLocation = @"..\..\ResourcesPh\brickblock.png";
            }
            #endregion picturebox3

            #region picturebox4
            PictureBox picturebox4 = new PictureBox
            {
                Visible = true,
                Anchor = AnchorStyles.Top,
                SizeMode = PictureBoxSizeMode.Normal,
                BackColor = Color.Sienna,
                Dock = DockStyle.Fill,
                Margin = new Padding(0),
            };
            if(jIndex < gamePanel.ColumnCount - 1)
            {
                gamePanel.Controls.Add(picturebox4, gamePanel.ColumnCount - 1, gamePanel.RowCount - 1 - jIndex - 1);
                picturebox4.ImageLocation = @"..\..\ResourcesPh\brickblock.png";
            }
            #endregion picturebox4

            jIndex++;
        }

        //the starting point of the mouse
        #region mouseStartPoint
        //PictureBox picturebox5 = new PictureBox
        //{
        //    Visible = true,
        //    Anchor = AnchorStyles.Top,
        //    SizeMode = PictureBoxSizeMode.Normal,
        //    BackColor = Color.Sienna,
        //    Dock = DockStyle.Fill,
        //    Margin = new Padding(0)
        //};
        gamePanel.Controls.Add(picturebox5, 11, 11);
        picturebox5.ImageLocation = @"..\..\ResourcesPh\mouse.png";
        #endregion mouseStartPoint

        KeyDown += new KeyEventHandler(Form2_KeyDown);
    }

    private void Form2_KeyDown(object sender, KeyEventArgs e)
    {
        int x = 11, y = 11;

        if (e.KeyCode == Keys.Right)
            x += 1;
        if (e.KeyCode == Keys.Left)
            x -= 1;
        if (e.KeyCode == Keys.Up)
            y -= 1;
        if (e.KeyCode == Keys.Down)
            y += 1;

        gamePanel.Controls.Remove(picturebox5);
        gamePanel.Controls.Add(picturebox5, x, y);
        picturebox5.ImageLocation = @"..\..\ResourcesPh\mouse.png";
        Refresh();
    }

    private void howToPlayToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Hide();
        Form3 f3 = new Form3();
        f3.FormClosed += (s, args) => Close(); //event handler on closing Form2 after Form3 is opened
        f3.Show();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

  }
}

TableLayoutPanel网格

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-30 21:33:24

每次按一下键,你总是将起始位置设置为11,11。将声明移出该范围:

代码语言:javascript
运行
复制
int x = 11, y = 11;

private void Form2_KeyDown(object sender, KeyEventArgs e) {
票数 0
EN

Stack Overflow用户

发布于 2015-12-30 21:55:54

以下是移动鼠标的一种非常简化的方法,我直截了当地把事情降到最低,但效果很好:D

下面是我用过的瓷砖:

这是密码!

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private Dictionary<int, Image> _dictionary;
        private Size _imageSize;
        private int[] _level;
        private Size _levelSize;
        private Point _mousePos;

        public Form1()
        {
            InitializeComponent();
            pictureBox1.Paint += PictureBox1_Paint;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializeLevel();
            Redraw();
        }

        private void InitializeLevel()
        {
            var imageEmpty = Image.FromFile("empty.png");
            var imageMouse = Image.FromFile("mouse.png");
            var imageWall = Image.FromFile("wall.png");
            _level = new[]
            {
                0, 0, 0, 0, 0,
                0, 1, 1, 1, 0,
                0, 1, 1, 1, 0,
                0, 1, 1, 1, 0,
                0, 0, 0, 0, 0
            };
            _levelSize = new Size(5, 5);
            _imageSize = new Size(25, 25);
            _dictionary = new Dictionary<int, Image>();
            _dictionary.Add(0, imageWall);
            _dictionary.Add(1, imageEmpty);
            _dictionary.Add(2, imageMouse);
            _mousePos = new Point();
        }

        private void Redraw()
        {
            pictureBox1.Invalidate();
        }

        private void PictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var graphics = e.Graphics;
            graphics.Clear(Color.Transparent);
            // draw level
            var i = 0;
            foreach (var tile in _level)
            {
                var x = i % _levelSize.Width;
                var y = i / _levelSize.Width;
                var image = _dictionary[tile];
                graphics.DrawImage(image, new Point(x * _imageSize.Width, y * _imageSize.Height));
                i++;
            }
            // draw hero !
            graphics.DrawImage(_dictionary[2], _mousePos);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            var up = keyData == Keys.Up;
            var down = keyData == Keys.Down;
            var left = keyData == Keys.Left;
            var right = keyData == Keys.Right;

            var processed = up || down || left || right;
            if (!processed) return base.ProcessCmdKey(ref msg, keyData);

            // move the hero !
            var x = 0;
            var y = 0;
            if (left) x--;
            if (right) x++;
            if (up) y--;
            if (down) y++;
            _mousePos.X += x;
            _mousePos.Y += y;
            Redraw();
            return true;
        }
    }
}

很明显你还是想检查碰撞,定义你的逻辑等等.

继续试一试!

欢迎光临,祝您好运:D

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34537079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档