简体   繁体   English

如何在不离开pictureBox区域的情况下在pictureBox1内部移动按钮?

[英]How to move the button inside the pictureBox1 without leave the pictureBox area?

private void timer1_Tick(object sender, EventArgs e)
        {
            int x = r.Next(0, pictureBox1.Width);
            int y = r.Next(0, pictureBox1.Height);
            button1.Top = y;
            button1.Left = x;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

EDIT** 编辑**

int x1 = r.Next(0, pictureBox1.Width);
            int y1 = r.Next(0, pictureBox1.Height);
            randomPoint = new Point(x1, y1);
            if (currentPosition.X != randomPoint.X)
            {
                if (currentPosition.X > randomPoint.X)
                    currentPosition.X -= 1;
                else
                    currentPosition.X += 1;

                button1.Location = currentPosition;
            }
            else if (currentPosition.Y != randomPoint.Y)
            {
                if (currentPosition.Y > randomPoint.Y)
                    currentPosition.Y -= 1;
                else
                    currentPosition.Y += 1;

                button1.Location = currentPosition;
            }
            else
            {
                randomPoint.X = r.Next(0, pictureBox1.Width - button1.Width - 1);
                randomPoint.Y = r.Next(0, pictureBox1.Height - button1.Height - 1);
            }

In top of the form i did: 在表格的顶部,我做了:

int x ;
int y ;
Point currentPosition;
Point randomPoint;

In constructor: 在构造函数中:

x = button1.Location.X; x = button1.Location.X; y = button1.Location.Y; y = button1.Location.Y; currentPosition = new Point(x, y); currentPosition =新Point(x,y);

In your form load event do: 在表单加载事件中执行以下操作:

currentPosition = button1.Location;
randomPoint.X = r.Next(0, PictureBox1.Width - Button1.Width - 1);
randomPoint.Y = r.Next(0, PictureBox1.Height - Button1.Height - 1);

Inside your timer: 在计时器内部:

if(currentPosition.X != randomPoint.X){
    if (currentPosition.X > randomPoint.X)
        currentPosition.X -= 1;
    else
        currentPosition.X += 1;

    Button1.Location = currentPosition;
}
else if(currentPosition.Y != randomPoint.Y){
    if(currentPosition.Y > randomPoint.Y)
        currentPosition.Y -= 1;
    else
        currentPosition.Y += 1;

    Button1.Location = currentPosition;
}
else{
    randomPoint.X = r.Next(0, PictureBox1.Width - Button1.Width - 1);
    randomPoint.Y = r.Next(0, PictureBox1.Height - Button1.Height - 1);
}

As far as the movement, you can increase picturebox size or(and) dicrease button size. 至于运动,您可以增加图片picturebox大小or(and)减小button大小。 Dont forget to declare Random r = new Random(); 不要忘记声明Random r = new Random(); where you declared currentPosition and randomPoint . 您在其中声明currentPositionrandomPoint

valter 瓦尔特

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将pictureBox1中的图像替换为pictureBox1矩形区域上绘制的裁剪图像? - How can i replace the image in pictureBox1 with a crop image of a drawn on the pictureBox1 rectangle area? 如何检查button1是否不会移出pictureBox1边界/边框? - How do i check that the button1 will not move out of the pictureBox1 bounds/borders? 我正在尝试将pictureBox1内的图像保存到我的硬盘上,但是将其保存而没有pictureBox1内的矩形,这是为什么呢? - Im trying to save now the image inside the pictureBox1 to my hard disk but its saving it without the rectangle inside the pictureBox1 why? 单击按钮时如何在随机位置生成picturebox1的多个克隆? - How to spawn multiple clones of picturebox1 in random locations on button click? 如何在将鼠标移到pictureBox1上时自动在pictureBox2上绘制点? - How can i make that when i move the mouse ober pictureBox1 it will draw points automatic on pictureBox2? 如果用户处于鼠标按下事件中,如何检查pictureBox1鼠标离开事件? - How can i check on pictureBox1 mouse leave event if the user is in mouse down event? 如何将图片从PictureBox1复制到Excel? - How to copy image from PictureBox1 to excel? 在图片框内移动图像 - move an image inside a picturebox 图片从picturebox1到picturebox2 - image from picturebox1 to picturebox2 单击C#中的按钮,将图像从“ pictureBox1”保存到文件 - Saving image from “pictureBox1” to a file with a click on a button in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM