VB.NET 文字动画
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23743057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET text animation
提问by apolloneo
I am doing a scrolling text animation with VS2010 and Windows 7. I have a textbox, label and a button, of course a timer to make this work. following is the code I put in timer.
我正在使用 VS2010 和 Windows 7 制作滚动文本动画。我有一个文本框、标签和一个按钮,当然还有一个计时器来完成这项工作。以下是我放入计时器的代码。
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox1.Text
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
'Dim txtBuffer As String = TextBox1.Text
Label1.Text = Label1.Text
If Len(Label1.Text) <> 0 Then
Label1.Text = Microsoft.VisualBasic.Right(Label1.Text, (Len(Label1.Text) - 1))
Else
Timer1.Enabled = False
End If
End Sub
My problem is when you input a certain amount of space at the start of string, the scrolling animation is kind of strange. It will not showing the string moving from right side into the label, but only show the whole string at once then start scrolling out of the left boundary of the label.
我的问题是当你在字符串的开头输入一定量的空间时,滚动动画有点奇怪。它不会显示从右侧移动到标签的字符串,而只会显示整个字符串,然后开始滚动到标签的左边界之外。
I am using fixed length of label, my purpose is to simulate a message board, so the text can be scrolled into the msg box from right side and moving out from left.
我使用的是固定长度的标签,我的目的是模拟一个留言板,因此文本可以从右侧滚动到msg框并从左侧移出。
I debuged each time the timer was triggered and the head of string which is a certain amount of spaces does go away one by one. But I didn't see the expected animation on the label.
每次触发计时器时,我都会进行调试,并且一定数量的空格的字符串头部确实会一一消失。但是我没有在标签上看到预期的动画。
The size of label is 7 upper case letters' width and AutoSize property is false. If in the textbox you input is "[7 space] ABCDEFG", then you would expect the scrolling effect would be "[7 space]"->"[6 space]A"->"[5 space]AB"-> "[4 space]ABC"->...->"ABCDEFG"->"BCDEFG"->"CDEFG"->...->"". But you won't get it. All you will get is "[7 space]" for a while, then all of a sudden, "ABCDEFG" appears, then the letters go away one by one. The string is not looks like it slides in from the right side of label area.
标签的大小为 7 个大写字母的宽度,AutoSize 属性为 false。如果您在文本框中输入的是“[7 空格] ABCDEFG”,那么您会期望滚动效果是“[7 空格]”->“[6 空格]A”->“[5 空格]AB”-> "[4 个空格]ABC"->...->"ABCDEFG"->"BCDEFG"->"CDEFG"->...->""。但你不会得到它。你会得到的只是“[7个空格]”一会儿,然后突然出现“ABCDEFG”,然后字母一个一个地消失。字符串看起来不像是从标签区域的右侧滑入的。
I don't know if anyone can help on that or maybe have a better idea to realize that animation? Thanks!
我不知道是否有人可以对此提供帮助,或者是否有更好的主意来实现该动画?谢谢!
回答by LarsTech
It's probably easier to just move a label inside a panel control than trying to figure out character measurements, etc, in order to get the text to starton the right side and then move left:
为了让文本从右侧开始然后向左移动,在面板控件内移动标签可能比尝试找出字符测量等更容易:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.AutoSize = True
Label1.Text = TextBox1.Text
Label1.Parent = Panel1
Label1.Location = New Point(Panel1.ClientSize.Width, _
Panel1.ClientSize.Height / 2 - (Label1.Height / 2))
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Label1.Right < 0 Then
Label1.Left = Panel1.ClientSize.Width
Else
Label1.Left -= 10
End If
End Sub
回答by Maeda
I had a similar issue. I solved using a timer and calculating the textbox size to move the text (right to left) until the left border. The idea is move the text from right to left and when the text ends before fill the textbox, complete the text with same blank spaces and restart showing the text.
我有一个类似的问题。我解决了使用计时器并计算文本框大小以将文本(从右到左)移动到左边框的问题。这个想法是将文本从右向左移动,当文本在填充文本框之前结束时,用相同的空格完成文本并重新开始显示文本。
Private _textToDisplay As String = "12345"
Private _Showing As String = ""
Private _avrchar As Integer = 0
Private Sub Showing_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TickerTimer.Tick
Try
If _Showing.Count < _textToDisplay.Count And _Showing.Count > 0 Then
_Showing = _textToDisplay.Substring(0, _Showing.Length + 1)
ElseIf _Showing.Count < _textToDisplay.Count And _Showing.Count = 0 Then
_Showing = _textToDisplay.Substring(0, 1)
ElseIf _Showing.Count < _avrchar Then
_Showing = " " + _Showing
Else
_Showing = ""
End If
textTicker.Text = _Showing
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Function NumberfitCharsInTextBox(tb As TextBox) As Integer
Dim avgW As Integer = 0
Dim avgH As Integer = 0
Dim avg As Size
For i As Integer = 65 To 90
avg = TextRenderer.MeasureText(CChar(ChrW(i)).ToString(), textTicker.Font)
avgH += avg.Height
avgW += avg.Width
Next
Return CInt((((45 * textTicker.Width) / avgW) * ((45 * textTicker.Height) / avgH)))
End Function
回答by antony thomas
If the label size is fixed and if it located at fixed place and just the text scroll in or out from left to right or right to left seems to be a better animation for applications.
如果标签大小是固定的并且它位于固定的位置并且只有文本从左到右或从右到左滚动进出,似乎是应用程序更好的动画。
回答by Sharath Nandakumar
Try this:
尝试这个:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Timer1.Interval = 1
Label1.Left = Label1.Left - 1
If Label1.Right = 0 Then
Label1.Left = Me.Width + 1
End If
End Sub
Private Sub form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Label1.AutoSize = True
End Sub
End Class
回答by Jeff
I found this very small and interesting implementation.Take a look.
我发现这个非常小而有趣的实现。看一看。
Public Class Start
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Left -= 10
If Label1.Left <= -Width Then
Label1.Left = Width
End If
End Sub
Private Sub Start_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.AutoSize = True
Timer1.Start()
End Sub
End Class

