gpt4 book ai didi

c# - 使用 For 循环滑动对象 (C#)

转载 作者:行者123 更新时间:2023-11-30 21:20:47 25 4
gpt4 key购买 nike

我做了很多搜索,但没有找到任何有用的东西。

是否可以使用 C#“滑动”或“移动”对象,使用简单的 For 循环将对象从一个位置移动到另一个位置?

谢谢

最佳答案

我建议您使用 Timer .还有其他选项,但如果您想避免线程问题等,这将是最简单的。

使用直接的 for 循环将要求您使用 Application.DoEvents() 抽取消息队列以确保 Windows 有机会实际呈现更新的控件,否则 for 循环将运行完成在不更新 UI 的情况下,控件看起来会从源位置跳转到目标位置。

这是一个 QAD 示例,用于在单击时在 Y 方向上设置按钮动画。此代码假定您在名为 animationTimer 的窗体上放置了一个计时器控件。

private void button1_Click(object sender, EventArgs e)
{
if (!animationTimer.Enabled)
{
animationTimer.Interval = 10;
animationTimer.Start();
}
}

private int _animateDirection = 1;
private void animationTimer_Tick(object sender, EventArgs e)
{
button1.Location = new Point(button1.Location.X, button1.Location.Y + _animateDirection);

if (button1.Location.Y == 0 || button1.Location.Y == 100)
{
animationTimer.Stop();
_animateDirection *= -1; // reverse the direction
}
}

关于c# - 使用 For 循环滑动对象 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3078898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com