首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图片上下移动

问将图片上下移动
EN

Stack Overflow用户
提问于 2021-12-15 14:13:02
回答 1查看 113关注 0票数 1

如何将图片从上到下移动,然后在其他坐标下得到?

我将更清楚地解释我的问题:我希望图像从表单的右上角开始,然后向下,然后出现在顶部的中间,然后再次下降。

我写的代码,但它只是下降,我不知道如何继续。

代码语言:javascript
复制
 private int x = 5;
 private void tmrMoving_Tick(object sender, EventArgs e)
 {
          
            pictureBox1.Top += x;
            pictureBox1.Location = new Point(350,0);
                x += 5;
            Invalidate();
}

有谁可以帮我?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-15 15:45:32

如果我正确理解你的问题,这可能就是你要找的。基本上,你需要从顶部开始,然后慢慢移动到底部。当你触摸底部时,回到容器的中间,再向下一次。这段代码没有将图片放置在右边,但是如果您了解代码所做的事情,您应该能够自己完成;)

代码语言:javascript
复制
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);
                }
            }
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70365186

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档