顺利移动标签 [英] Move labels Smoothly
本文介绍了顺利移动标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我想顺利移动标签。在这里,是我的代码(
Hello, I want to move the labels smoothly. Here, is my codes (
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Start()
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label4.Visible = True
Label4.Top = Label4.Top - 75
Label3.Visible = False
Timer1.Stop()
Timer1.Enabled = False
Timer2.Start()
Timer2.Enabled = True
End Sub
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
Label4.Top = Label4.Top + 75
Label3.Visible = True
Label4.Visible = False
Timer1.Start()
Timer1.Enabled = True
Timer2.Stop()
Timer2.Enabled = False
End Sub
)。它非常粗略。我想让它变得非常顺利。在Windows 8地铁屏幕上,可以看到上升的情况。它非常顺利地帮助我。在此先感谢。
). It goes very roughly. I want to make it go very smoothly. In windows 8 metro screen, see the things going up either down. It goes very smoothly help me with this. Thanks in advance.
推荐答案
首先,永远不要使用System.Windows.Forms.Timer
来制作动画,使用其他定时器之一。更好更简单,不要使用计时器,使用单独的线程。如果您需要知道移动的实时时间,请使用System.Diagnostics.Stopwatch
(您只需要相对时间)。缩小步骤。
现在,使用所有计时器(除了上面提到的那个不能给你任何可接受的准确度的计时器)和一个单独的线程,你需要委托您的方法将UI上的任何内容更改为UI线程。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用Invoke
或
System.Windows.Threading的方法
。 Dispatcher
(对于Forms或WPF)或System.Windows.Forms.Control
(仅限表单)。
您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
使用Treeview扫描仪和MD5的问题 [ ^ ]。
另请参阅有关线程的更多参考资料:
如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],
在启用禁用+多线程后控制事件未触发 [ ^ ]。
准确编写代码;永远不要使用自动生成的名称,如Label4
或Timer1_Tick
;他们违反(良好)Microsoft命名约定而不是缩进永久使用;将它们全部重命名以给出语义上合理的名称,否则你的代码将难以维护。
基本上,这就是你需要的全部。
-SA
First of all, never useSystem.Windows.Forms.Timer
for animation, use one of the other timers. Even better and simpler, don't use a timer, use a separate thread. If you need to know real time of the move, useSystem.Diagnostics.Stopwatch
(you only need relative time). Make smaller steps.
Now, with all the timers (except the one I mentioned above which cannot give you any acceptable accuracy) and with a separate thread, you will need to delegate your method changing anything on your UI to the UI thread. You cannot call anything related to UI from non-UI thread. Instead, you need to use the methodInvoke
orBeginInvoke
ofSystem.Windows.Threading.Dispatcher
(for both Forms or WPF) orSystem.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].
Write code accurately; never ever use auto-generated names likeLabel4
orTimer1_Tick
; they violate (good) Microsoft naming conventions and not indented for permanent use; rename them all to give semantically sensible names, otherwise you code will be hard to maintain.
Basically, that's all you need.
—SA
这篇关于顺利移动标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文