【问题标题】:Move the picture top to bottom将图片从上到下移动
【发布时间】:2022-01-18 17:27:36
【问题描述】:

如何将图片从上到下移动到其他坐标?

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

我写了代码,但它只是失败了,我不知道如何继续。

 private int x = 5;
 private void tmrMoving_Tick(object sender, EventArgs e)
 {
          
            pictureBox1.Top += x;
            pictureBox1.Location = new Point(350,0);
                x += 5;
            Invalidate();
}

谁能帮帮我?谢谢。

【问题讨论】:

  • 为什么你一触底就将 x 增加 10?
  • 顶部 + x 是错误的。 X 是宽度,Y 是高度。它应该是 Top +y 或 Left + x。 with 从左到右为正。高度下降(而不是上升)。
  • @RaulF 哦,很抱歉拼错了,但无论如何我没有达到预期的结果:)

标签: c# winforms


【解决方案1】:

如果我正确理解您的问题,这可能就是您正在寻找的内容。基本上你需要从顶部开始,然后慢慢移动到底部。当你触到底部时,回到容器的中间并再次下降。此代码没有将图片放在右侧,但如果您了解代码的作用,您应该可以自己做;)

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);
                }
            }
        });
    }
}

【讨论】:

  • 只有1个错误,超时错误(错误CS0103当前上下文中不存在名称'Timeout'),我想我可以解决它,谢谢。
  • 这个类在System.Threading命名空间中,你需要导入它。抱歉,我没有包含 using 部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多