我创建了一个计时器,它使文本框的文本看起来从左向右移动,我怎样才能让它看起来更流畅? [英] I've created a timer which makes a textbox's text appear to be moving from left to right, how can I make it look smoother?

查看:132
本文介绍了我创建了一个计时器,它使文本框的文本看起来从左向右移动,我怎样才能让它看起来更流畅?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个计时器,它使文本框的文本看起来从左向右移动,我怎样才能让它看起来更流畅?





到目前为止它完成了我想要的,但运动看起来并不正确。

计时器内部是100



代码:



I have created a timer which makes a textbox''s text appear to be moving from left to right, how can I make it look smoother?


So far it does what I want, but the movement doesn''t look right.
Timer internal is 100

Code:

Public Class Form1
    Dim CurrentName As String = String.Empty

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        CurrentName = "I'm looking for somehelp with this :-) Hope everyone's days going well"
        CurrentName = CurrentName + "          "
    End Sub

    Private Sub RotateTextbox_Tick(sender As Object, e As EventArgs) Handles RotateTextbox.Tick
        If Not CurrentName = String.Empty Then
                Dim FirstLetterCPN As String = CurrentName.Substring(0, 1)
                CurrentName = CurrentName.Substring(1, CurrentName.Length - 1) & FirstLetterCPN
        End If
        TextBox1.Text = CurrentName
    End Sub
End Class

推荐答案

首先,我不知道你的计时器是什么使用。如果你正在使用你正在使用 System.Windows.Forms.Timer ,不要这样做:这个计时器绝对不适合动画,因为它的准确性是不可接受的,它可能会非常糟糕。这个计时器类的唯一好处是它的使用简单:它不需要UI线程调用。



现在,不要依赖于确切的时间即使有更好的计时器,也可以使用计时器事件。相反,在渲染坐标之前立即测量实际时间。使用 System.Diagnostics.Stopwatch

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx [ ^ ]。



根据测量的时间计算坐标。在这种情况下,即使帧之间的时间间隔不均匀,您的动作也会很平滑。



更好的是,根本不使用计时器。使用带循环的附加线程和 System.Threading.Thread.Sleep 在一个循环内调用,以设置一些帧频率。因为你现在将测量每帧中的时间,准确的时间并不重要,但使用线程更简单。



现在,你将面对同样的问题计时器和线程的问题:跨线程调用UI,所以你必须解决它。您不能从非UI线程调用与UI相关的任何内容。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



另一个重要的事情是避免闪烁。为此,您应该使用双缓冲。您没有标记UI库,但您可以轻松找到如何在文档中执行此操作。



-SA
First of all, I don''t know what timer do you use. If you are using you are using System.Windows.Forms.Timer, don''t do it: this timer is absolutely unsuitable for animation, because its accuracy is unacceptable, it can be amazingly bad. The only "benefit" of this timer class is the simplicity of its use: it does not require UI thread invocation.

Now, don'' rely at exact time of the timer event at all, even with a better timer. Instead, measure "real" time immediately before rendering coordinates. Use System.Diagnostics.Stopwatch:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^].

Calculate your coordinate based on measured time. In this case, even if the time intervals between "frames" are uneven, your motion will be smooth.

Better yet, don''t use timers at all. Use additional thread with a cycle and System.Threading.Thread.Sleep calls inside a cycle, to setup some frame frequency. As you now will be measuring time in each frame anyway, exact timing does not matter much, but using a thread is way simpler.

Now, you will face the same problem with timers and thread: cross-thread calls to UI, so you have to solve it anyway. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.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[^].

Another important thing is to avoid flicker. For this purpose, you should use double buffering. You did not tag you UI library, but you can easily find how to do it in documentation.

—SA


使用选取框并放置文字控件。不需要计时器
Use marquee and put literal control. No need timer


这篇关于我创建了一个计时器,它使文本框的文本看起来从左向右移动,我怎样才能让它看起来更流畅?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆