c# - C# 表单处理多个事物
问题描述
我在列表中有一些图片框。我希望他们更新他们的位置,并且仍然能够在更新其他对象的同时拖动对象。
我正在做和不定式 while 循环不断更新他们的位置:
while (true)
{
Random rnd = new Random();
pictures[0].Location = new Point(pictures[0].Location.X - rnd.Next(-2, 3), pictures[0].Location.Y + rnd.Next(0, 2));
pictures[1].Location = new Point(pictures[1].Location.X, pictures[1].Location.Y + 1);
this.Update();
}
问题是我使这些元素可拖动,但没有调用 MouseDown 事件。因为我猜是无限循环,但我该如何解决呢?
解决方案
您应该使用计时器。将其设置为触发,但您想经常更新位置。这让主线程可以处理其他事件,同时根据需要经常更新您的动画。
如何创建计时器:
myTimer = new System.Windows.Forms.Timer();
myTimer.Tick += TimerEventProcessor;
// Sets the timer interval to 0.5 seconds.
myTimer.Interval = 500;
myTimer.Start();
和
private void TimerEventProcessor(Object myObject,
EventArgs myEventArgs) {
// Your update code here
}
推荐阅读
- aws-lambda - 有没有办法在 Lambda@Edge 中加载外部数据
- unit-testing - 单元测试角6
- node.js - 如何在Node js excel-export中设置列宽
- python-3.x - 在浏览器选项卡之间切换 - Appium
- ionic3 - 页面不使用动画 ionic3 过渡
- jenkins - 在 dockerized Jenkins 的 docker 容器步骤中访问作业的工作区
- java - newFixedThreadPool 不时执行
- ios - 如何使用 Swift 在矩阵中添加对角线?
- java - Android - 如何动态移动到活动或以前的活动?
- kotlin - Kotlin JSR223 引擎传递绑定