如何将图片从上到下移动,然后在其他坐标下得到?
我将更清楚地解释我的问题:我希望图像从表单的右上角开始,然后向下,然后出现在顶部的中间,然后再次下降。
我写的代码,但它只是下降,我不知道如何继续。
private int x = 5;
private void tmrMoving_Tick(object sender, EventArgs e)
{
pictureBox1.Top += x;
pictureBox1.Location = new Point(350,0);
x += 5;
Invalidate();
}有谁可以帮我?谢谢。
发布于 2021-12-15 15:45:32
如果我正确理解你的问题,这可能就是你要找的。基本上,你需要从顶部开始,然后慢慢移动到底部。当你触摸底部时,回到容器的中间,再向下一次。这段代码没有将图片放置在右边,但是如果您了解代码所做的事情,您应该能够自己完成;)
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);
}
}
});
}
}https://stackoverflow.com/questions/70365186
复制相似问题