如何從上到下移動圖片,然后在其他坐標中獲取它?
我會更清楚地解釋我的問題:我希望影像從表單的右上角開始向下,然后出現在頂部的中間并再次向下。
我寫了代碼,但它只是失敗了,我不知道如何繼續。
private int x = 5;
private void tmrMoving_Tick(object sender, EventArgs e)
{
pictureBox1.Top = x;
pictureBox1.Location = new Point(350,0);
x = 5;
Invalidate();
}
誰能幫我?謝謝。
uj5u.com熱心網友回復:
如果我正確理解您的問題,這可能就是您要找的。基本上你需要從頂部開始,然后慢慢移動到底部。當你觸到底部時,回到容器的中間并再次下降。這段代碼沒有把圖片放在右邊,但如果你理解代碼的作用,你應該可以自己做;)
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://www.uj5u.com/caozuo/382072.html
上一篇:在函式失敗后我應該重新呼叫API函式并在c#中重繪我的訪問令牌嗎?
下一篇:從左到右和從右到左移動標簽
