如何将图片框移动到面板中的随机位置。 [英] How to move a picturebox to an random location in a panel.

查看:91
本文介绍了如何将图片框移动到面板中的随机位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个这样的程序:



如果我点击图片框,它将移动到panel1中的另一个随机位置。然后我再次点击它移动到另一个随机位置。任何人都可以帮助我。

I need to do a program like this:

If I click on on the picturebox it will move to an other random location in panel1. then I again click and It move to an other random location. Can anyone help me.

推荐答案

我是这样做的:





I did it like this:


MainForm(void)
		{
			InitializeComponent();
			this->pictureBox1->Click += gcnew System::EventHandler(this, &AI_Events::MainForm::OnClick);
			this->rnd = gcnew Random(1234);
			this->imgFile = gcnew String("D:\\Code\\star.png");
			this->Init();
		}


void MainForm::Init()
	{
		this->pictureBox1->SizeMode = PictureBoxSizeMode::Zoom;
		this->pictureBox1->Image = Image::FromFile(this->imgFile);
	}

void MainForm::OnClick(System::Object ^sender, System::EventArgs ^e)
	{
		Rectangle rt = this->panel1->Bounds;
		Rectangle rp = this->pictureBox1->Bounds;
		int xrnd = this->rnd->Next(rt.Left, rt.Right - rp.Width);
		int yrnd = this->rnd->Next(rt.Top, rt.Bottom - rp.Height);
		Point p(xrnd, yrnd);
		this->pictureBox1->Location = p;
		String^ t = xrnd.ToString();
		t += "," + yrnd.ToString();
		this->Msg(t);
	}


这篇关于如何将图片框移动到面板中的随机位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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