图片框的随机运动 [英] Random motion of a picture box

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

问题描述

大家好,
谁能告诉我在c#(Visual Studio 2008)中如何使图片框非常精确,平滑地移动(我猜想使用线程或计时器),以便在随机方向上移动图片框.如果有人可以提交带有解释的代码,而不是任何专家制定的项目,那将是很有意义的.
谢谢,
Akkywadhwa

Hi everyone,
Can anybody tell me that how can i make a picturebox to move in random directions very precisely and smoothly(i guess using threads or timer) in c#(visual studio 2008). It would be appreciable if anybody can submit code with explanation rather than a project made by any expert.
Thanks,
Akkywadhwa

推荐答案

当然,您需要使用该线程.举个简单的例子,请看我过去的回答:
逐步更改以显示控件 [ ^ ].

仅使用标签PictureBox代替标签.要随机移动,请使用类System.Random:
http://msdn.microsoft.com/en-us/library/system.random.aspx [ ^ ].

但是,不要只是跳到随机坐标.取而代之的是,跳到相对于移动对象的当前位置的很小的随机矢量,只有几个像素.为范围内的ΔyΔx生成一个随机值,例如-5 .. +5像素,并移动此值:
Certainly, you need to use the thread. For a simple example, please see my past answer:
Changes step by step to show a control[^].

Only instead of label, use your PictureBox object. For a random move, use the class System.Random:
http://msdn.microsoft.com/en-us/library/system.random.aspx[^].

But don''t just jump to random coordinate. Instead, jump to a small random vector relative to a current location of you moving object, just a few pixels. Generate a random value for Δy and Δx in the range, say, -5.. +5 pixels and move by this value:
myPictureBox.Left += deltaX;
myPictureBox.Top += deltaY;



要消除可能的闪烁,请使用双重缓冲:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx [ ^ ].

对于UI,此处的关键是使用UI调用机制或System.Threading.Dispatcher.有关更多信息,请参阅我过去的答案:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

祝你好运,

-SA



To fight quite possible flicker, use double buffering:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.doublebuffered.aspx[^].

With the UI, the key here is using the UI invocation mechanism or System.Threading.Dispatcher. For more information, please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

Good luck,

—SA


您可以查看
You could take a look at the Windows Animation API[^]

Best regards
Espen Harlinn


您好,

试试这个:
Hello,

Try this:
Random rnd1 = new Random();
private void timer1_Tick(object sender, EventArgs e)
{
    timer1.Enabled = false;

    int x = rnd1.Next(0, Width - pictureBox1.Width);
    int y = rnd1.Next(0, (Height - 28) - pictureBox1.Height);

    pictureBox1.Location = new Point(x, y);

    timer1.Enabled = true;
}


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

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