控制,使其移动图片框 [英] Controling the Moving PictureBox

查看:98
本文介绍了控制,使其移动图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在我的运动图像框还挺stucked;此刻我的表格2加载图片框开始移动,这正是我想要的,但它不是在我的控制,这就是我想要做的,但我失败了.....所有我想要的,是移动图片框的方向由钥匙......谢谢

 使用系统进行控制; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;

命名空间WindowsFormsApplication1
{
公共部分类窗体2:表
{

公共窗体2()
{
的InitializeComponent();
}

私人无效Form2_Load(对象发件人,EventArgs五)
{
timer1.Start();
}

私人无效Form2_KeyDown(对象发件人,发送KeyEventArgs E)
{
如果(e.KeyCode == Keys.Right)
{
pictureBox1.Location =新的点(pictureBox1.Location.X + 5,pictureBox1.Location.Y);
}

如果(e.KeyCode == Keys.Up)
{
pictureBox1.Location =新的点(pictureBox1.Location.X,pictureBox1.Location。 Ÿ - 5);
}

如果(e.KeyCode == Keys.Left)
{
pictureBox1.Location =新的点(pictureBox1.Location.X - 5,pictureBox1。 Location.Y);
}

如果(e.KeyCode == Keys.Down)
{
pictureBox1.Location =新的点(pictureBox1.Location.X,pictureBox1.Location。 Y + 5);
}

}

私人无效timer1_Tick(对象发件人,EventArgs五)
{

pictureBox1.Location =新点(pictureBox1.Location.X + 25,pictureBox1.Location.Y);
}

私人无效pictureBox1_Click(对象发件人,EventArgs五)
{

}

}
}


解决方案

您需要一个变量,指示哪个方向框移动:

 私人枚举方向{无,左,右,上,下​​}; 
私人方向currentDir = Direction.None;



计时器的Tick事件需要检查这个变量就知道往哪个方向移动的方块:

 私人无效timer1_Tick(对象发件人,EventArgs五){
INT VEL = 5;
开关(currentDir){
情况下Direction.Left:pictureBox1.Left - = Math.Min(VEL,pictureBox1.Left);打破;
情况下Direction.Right:pictureBox1.Left + = Math.Min(VEL,this.ClientSize.Width - pictureBox1.Right);打破;
情况下Direction.Up:pictureBox1.Top - = Math.Min(VEL,pictureBox1.Top);打破;
情况下Direction.Down:pictureBox1.Top + = Math.Min(VEL,this.ClientSize.Height - pictureBox1.Bottom);打破;
}
}

您KeyDown事件处理程序应简单地设置变量:

 私人无效Form1_KeyDown(对象发件人,发送KeyEventArgs E){
开关(e.KeyData){
案Keys.Left:currentDir = Direction.Left;打破;
情况下Keys.Right:currentDir = Direction.Right;打破;
情况下Keys.Up:currentDir = Direction.Up;打破;
情况下Keys.Down:currentDir = Direction.Down;打破;
}
}

您还需要KeyUp事件,应立即停止运动再次框:

 私人无效Form1_KeyUp(对象发件人,发送KeyEventArgs E){
开关(e.KeyData){
情况下Keys.Left:如果(currentDir == Direction.Left)currentDir = Direction.None;打破;
情况下Keys.Right:如果(currentDir == Direction.Right)currentDir = Direction.None;打破;
情况下Keys.Up:如果(currentDir == Direction.Up)currentDir = Direction.None;打破;
情况下Keys.Down:如果(currentDir == Direction.Down)currentDir = Direction.None;打破;
}
}


So, I am kinda stucked on my moving PICTURE box; at the moment my form 2 loads the picture box starts moving, that is exactly what I wanted, However it isn't in my Control and that's what I am trying to do, but I've failed.....All I want is the direction of the moving picture Box to be controlled by the keys......Thanks.

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

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Right) 
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X + 5, pictureBox1.Location.Y);
            }

            if (e.KeyCode == Keys.Up)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - 5);
            }

            if (e.KeyCode == Keys.Left)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X - 5, pictureBox1.Location.Y);
            }

            if (e.KeyCode == Keys.Down)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + 5);
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {

           pictureBox1.Location = new Point(pictureBox1.Location.X + 25, pictureBox1.Location.Y);
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

    }
}

解决方案

You need a variable that indicates in which direction the box is moving:

    private enum Direction { None, Left, Right, Up, Down };
    private Direction currentDir = Direction.None;

The timer's Tick event needs to check this variable to know in which direction to move the box:

    private void timer1_Tick(object sender, EventArgs e) {
        int vel = 5;
        switch (currentDir) {
            case Direction.Left:  pictureBox1.Left -= Math.Min(vel, pictureBox1.Left); break;
            case Direction.Right: pictureBox1.Left += Math.Min(vel, this.ClientSize.Width - pictureBox1.Right); break;
            case Direction.Up:    pictureBox1.Top  -= Math.Min(vel, pictureBox1.Top); break;
            case Direction.Down:  pictureBox1.Top  += Math.Min(vel, this.ClientSize.Height - pictureBox1.Bottom); break;
        }
    }

Your KeyDown event handler should simply set the variable:

    private void Form1_KeyDown(object sender, KeyEventArgs e) {
        switch (e.KeyData) {
            case Keys.Left:  currentDir = Direction.Left; break;
            case Keys.Right: currentDir = Direction.Right; break;
            case Keys.Up:    currentDir = Direction.Up; break;
            case Keys.Down:  currentDir = Direction.Down; break;
        }
    }

You also need the KeyUp event, it should stop moving the box again:

    private void Form1_KeyUp(object sender, KeyEventArgs e) {
        switch (e.KeyData) {
            case Keys.Left:  if (currentDir == Direction.Left)  currentDir = Direction.None; break;
            case Keys.Right: if (currentDir == Direction.Right) currentDir = Direction.None; break;
            case Keys.Up:    if (currentDir == Direction.Up)    currentDir = Direction.None; break;
            case Keys.Down:  if (currentDir == Direction.Down)  currentDir = Direction.None; break;
        }
    }

这篇关于控制,使其移动图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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