用计时器移动图片框
我试图通过每 20 毫秒更改其顶部和左侧属性来移动包含控件中的图片框。在黑色背景上,它会更平滑一些,但是当为控件分配背景图像时,图片框会在其后面留下一条痕迹,直到几秒钟后重新绘制控件,突然减慢然后向前启动,或者以锯齿状轨迹移动。
我认为当重绘 PictureBox 控件时,更改其左侧和顶部属性的线程将停止,直到重绘完成为止,这会导致它在重绘完成时对被保留的计时器循环的任何迭代进行排队。
有没有办法让图片框的运动变得平滑?
谢谢 F
I'm trying to move a picturebox in a containing control by changing its Top and Left properties every 20ms. On a black background it's a bit smoother but when assigning a BackgroundImage to the control, the picturebox leaves a trail behind it until the control is redrawn a few seconds later, slows down abruptly then launches forward, or moves in a jagged trajectory.
I think when the PictureBox control is redrawn, the thread that changes its left and top properties is halted until redrawn is complete and that causes it to queue up any iterations of the timer loop that were held up, when the redraw is complete.
Is there anyway to smoothen up the picturebox's movement?
Thanks
F
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议在一个控件中绘制图像,而不是在控件中移动。
创建一个自定义控件,覆盖 OnPaint 并在其中绘制“图片框”。如果这样做,您还可以进行后台缓冲。通过调用 Graphics.FromImage() 从图像创建图形对象来完成此操作。绘制完毕后,最后在 REAL 图形对象(在 eventArgs 中)上调用 g.DrawImage 。
I'd suggest drawing an image in one control as opposed to moving around controls.
Create a custom control, override OnPaint and draw your "picture box" inside there. If you do this you can also back buffer. Do this by calling Graphics.FromImage() to create a graphics object from an image. Paint onto that and finally call g.DrawImage on the REAL graphics object (in the eventArgs) once you're done drawing.
我不知道如何解决该问题(除了切换到 WPF),但我可以说您遇到的延迟是由于移动 PictureBox 时正在更新的区域失效所致。您想要做的是以某种方式对新位置进行后台缓冲并切换到缓冲区,而不是依赖 GDI 实时进行重绘。 HTH。
I don't know exactly how to resolve the problem (other than switching to WPF) but I can say the lag you're getting is coming from the invalidation of the areas that are being updated as you move the PictureBox. What you want to do is somehow back-buffer the new position and switch to the buffer, rather than relying on GDI to do the redraw in realtime. HTH.