如何使用Timer平稳地移动标签文本?

时间:2018-07-03 17:27:03

标签: c# label

在C#WinForm应用程序中,我有一个Label控件,当文本不适合大小时,我想将其文本从右向左移动。

我使用Timer使文本从右向左滑动,但是由于我认为每个字母的宽度不同(例如i的宽度比{ {1}}等。这看起来很糟糕,我在下面附加了 gif 供您查看。它说为什么文本移动不顺畅?如您所见,这很难读,因为它有时会移动很多,有时会移动一些。

Why won't this text move smoothly?

w控件的宽度和高度是固定的,不能更改。

这是我的代码:

Label

任何人都可以就如何解决此问题给我建议吗? 还是有比使用Timer更简单的方法?

1 个答案:

答案 0 :(得分:2)

我建议您不要编辑文本。将标签改为带有恒定文本,并将其放置在Panel对象内。然后,移动标签的位置,以使标签的某些部分在面板中可见。我认为您会获得更好的体验。您将更改“左”坐标而不是文本。标签将比面板宽。

Designer的源代码

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Controls.Add(Me.Label1)
        Me.Panel1.Location = New System.Drawing.Point(279, 90)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(200, 100)
        Me.Panel1.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(177, 35)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(291, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "rewrewtewerewrewtjoitjrewoirewjtewotjweotirjewotijwertjewirtj"
        '
        'Timer1
        '
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.Panel1.ResumeLayout(False)
        Me.Panel1.PerformLayout()
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents Panel1 As Panel
    Friend WithEvents Label1 As Label
    Friend WithEvents Timer1 As Timer
End Class

VB文件的源代码

Public Class Form1
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Label1.Location = New Point(Label1.Location.X - 1, Label1.Location.Y)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Timer1.Start()
    End Sub
End Class

标签图片开始右移并有意切除    Picture of the Label Starting To Right and Intentionally Cut Off