如何使图片框从一个固定点移动到另一个固定点? [英] How do I make picture boxes move from one fixed point to another fixed point?

查看:60
本文介绍了如何使图片框从一个固定点移动到另一个固定点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个图片框

I have four picture boxes

1 2
4 3



放入格式如上。

当我点击按钮时它只旋转我的图片框一次这样


placed in the format above.
when i click the button it rotates my picture boxes only once like this

2 3
1 4



和计时器将启动并计数最多3秒然后自动点击我的按钮再次旋转但在这种情况下我的代码是错误请帮助所以我可以随机旋转这个图片框次数?



我的尝试:




and timer will start and count up to 3 seconds then auto clicks my button to rotate again but in this case my code is wrong please help so i can rotate this picture boxes random number of times?

What 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 MoveBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (pictureBox1.Location.Equals(pictureBox1.Location))
            {
                //This swaps positions of picture boxes clockwise
                // first -90 degree rotation of four boxes
                pictureBox1.Location = new Point(0, 160);
                pictureBox2.Location = new Point(0, 0);
                pictureBox3.Location = new Point(180, 0);
                pictureBox4.Location = new Point(180, 160);
                moveTimer1.Enabled = true;
            }
            else if (pictureBox1.Location.Equals(pictureBox3.Location))
            {
                //This swaps positions of picture boxes clockwise
                // second -90 degree rotation of four boxes
                pictureBox1.Location = new Point(0, 0);
                pictureBox2.Location = new Point(180, 0);
                pictureBox3.Location = new Point(180, 160);
                pictureBox4.Location = new Point(0, 160);
            }
        }

        private void moveTimer1_Tick(object sender, EventArgs e)
        {
            int timer = Convert.ToInt32(label1.Text);
            label1.Text = Convert.ToString(timer + 1);
            while (timer == 3)
            {
                  moveTimer1.Enabled = false;
                  button1.PerformClick();
            }
            
        }
    }
}

推荐答案

假设你想每隔3秒旋转/移动一次图像,你必须提供一种方法,它可以获得两个随机图片框并替换它们中的图像而不是替换pricuteboxes位置(除非你想移动图片框)。



点击按钮:

Assuming that you want to rotate/move images every 3 seconds, you have to provide a method which get two random pictureboxes and replaces images in them instead of replacing pricuteboxes locations (unless, you want to move pictureboxes).

On button click:
Timer1.Interval = 3000 // Here 3000 milliseconds = 3 seconds  
Timer1.Start()  

< br $>


On Timer tick你必须打电话(例如): MovePicture()方法。

想想吧!它似乎很容易实现。您必须使用列表< Point> 才能对其进行随机播放/system.random(v=vs.110).aspx\">随机类 [ ^ ]:





On Timer tick you have to call (for example): MovePicture() method.
Think of it! It seems to be easy to implement. You have to define a List<Point> to be able to shuffle them by using Random class[^]:

//create initial list of picture locations
List<System.Drawing.Point> picLocations = new List<System.Drawing.Point>()
	{
		new System.Drawing.Point(0, 0),
		new System.Drawing.Point(180, 0),
		new System.Drawing.Point(180, 160),
		new System.Drawing.Point(0, 160)
	};


//...

//MovePicture method
//randomize 
Random r = new Random();
int f = r.Next(0,2); //get random number between 0 and 1 - a picture location from first row
System.Drawing.Point first = picLocations[f]; 
int s = r.Next(2,4); //get random number between 2 and 3 - a picture location from second row
System.Drawing.Point second = picLocations[s]; 

//replace locations
picLocations[f] = second;
picLocations[s] = first;

//finall
PictureBox1.Location = picLocation[0];
PictureBox2.Location = picLocation[1];
PictureBox3.Location = picLocation[2];
PictureBox4.Location = picLocation[3];





一个计时器执行的样本图片位置:



Sample picture locations one timer execution:

X    Y
180 160 
180 0 
0   0 
0   160 





祝你好运!



Good luck!


这篇关于如何使图片框从一个固定点移动到另一个固定点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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