从上到下移动图片
Move the picture top to bottom
如何从上到下移动图片,然后在其他坐标下获取?
我会更清楚地解释我的问题:我希望图像从表格的右上角开始向下移动,然后出现在顶部中间并再次向下移动。
我写了代码,但是它就挂了,我不知道如何继续。
private int x = 5;
private void tmrMoving_Tick(object sender, EventArgs e)
{
pictureBox1.Top += x;
pictureBox1.Location = new Point(350,0);
x += 5;
Invalidate();
}
谁能帮帮我?谢谢
如果我对你的问题的理解正确,这可能就是你要找的。基本上你需要从顶部开始,然后慢慢移动到底部。当你触及底部时,回到容器的中间并再次下降。此代码不会将图片放在右侧,但如果您了解代码的作用,您应该可以自己做 ;)
public partial class Form1 : Form
{
private readonly System.Threading.Timer timer;
private const int dtY = 5;
private bool resetOnce;
public Form1()
{
InitializeComponent();
timer = new System.Threading.Timer(Callback, null, 0, 50);
}
private void Callback(object state)
{
BeginInvoke((MethodInvoker)delegate
{
pictureBox1.Location = new Point(0, pictureBox1.Location.Y + dtY);
// when it touches the bottom of the container
if (pictureBox1.Location.Y + pictureBox1.Size.Height > pictureBox1.Parent.Height)
{
// we already reset once, so no more going back up: stop the timer
if (resetOnce)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
// we did not reset yet, so go to middle of container
else
{
resetOnce = true;
pictureBox1.Location = new Point(0, pictureBox1.Parent.Height / 2 - pictureBox1.Size.Height / 2);
}
}
});
}
}
如何从上到下移动图片,然后在其他坐标下获取?
我会更清楚地解释我的问题:我希望图像从表格的右上角开始向下移动,然后出现在顶部中间并再次向下移动。
我写了代码,但是它就挂了,我不知道如何继续。
private int x = 5;
private void tmrMoving_Tick(object sender, EventArgs e)
{
pictureBox1.Top += x;
pictureBox1.Location = new Point(350,0);
x += 5;
Invalidate();
}
谁能帮帮我?谢谢
如果我对你的问题的理解正确,这可能就是你要找的。基本上你需要从顶部开始,然后慢慢移动到底部。当你触及底部时,回到容器的中间并再次下降。此代码不会将图片放在右侧,但如果您了解代码的作用,您应该可以自己做 ;)
public partial class Form1 : Form
{
private readonly System.Threading.Timer timer;
private const int dtY = 5;
private bool resetOnce;
public Form1()
{
InitializeComponent();
timer = new System.Threading.Timer(Callback, null, 0, 50);
}
private void Callback(object state)
{
BeginInvoke((MethodInvoker)delegate
{
pictureBox1.Location = new Point(0, pictureBox1.Location.Y + dtY);
// when it touches the bottom of the container
if (pictureBox1.Location.Y + pictureBox1.Size.Height > pictureBox1.Parent.Height)
{
// we already reset once, so no more going back up: stop the timer
if (resetOnce)
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
// we did not reset yet, so go to middle of container
else
{
resetOnce = true;
pictureBox1.Location = new Point(0, pictureBox1.Parent.Height / 2 - pictureBox1.Size.Height / 2);
}
}
});
}
}