【问题标题】:Text animation in Windows FormsWindows 窗体中的文本动画
【发布时间】:2012-09-12 14:17:02
【问题描述】:

我想知道是否有一种方法可以为表单上显示的文本添加某种动画。

当我想到这一点时,我的想法有点类似于您可以在 PowerPoint 中对文本执行的操作(即一次键入一个文本的类似打字机的动画,让整个文本框显示为某些效果等),我只是想了解您可以使用 Windows 窗体做什么。

目前我正在使用文本框在我的表单应用程序中显示信息,但事后我意识到标签也可以。

编辑:毕竟我使用的是标签,我只是给它起了一个名称,里面有“文本框”,因为没有更好的描述。

【问题讨论】:

  • 这类动画很难平滑并很快真的消失。使用 AutoEllipsis = True 的标签,您可以免费获得工具提示。
  • 虽然在 Winforms 中可以做到,但我建议你使用 WPF。 WPF 的工具库中有更多可用的工具来实现这些特殊效果。还要考虑,如果您坚持使用 Winforms,请考虑使用 DirectX

标签: c# winforms text


【解决方案1】:
public partial class Form1 : Form
{
    int _charIndex = 0;
    string _text = "Hello World!!";
    public Form1()
    {
        InitializeComponent();
    }

    private void button_TypewriteText_Click(object sender, EventArgs e)
    {
        _charIndex = 0;
        label1.Text = string.Empty;
        Thread t = new Thread(new ThreadStart(this.TypewriteText));
        t.Start();
    }

    private void TypewriteText()
    {
        while (_charIndex < _text.Length)
        {
            Thread.Sleep(500);
            label1.Invoke(new Action(() =>
            {
                label1.Text += _text[_charIndex];
            }));
            _charIndex++;
        }
    }
}

【讨论】:

【解决方案2】:

现在,我个人不会这样做,因为免费的动画往往会惹恼用户。我只会谨慎地使用动画——当它真正有意义的时候。

也就是说,你当然可以这样做:

 string stuff = "This is some text that looks like it is being typed.";
 int pos = 0;
 Timer t;

 public Form1()
 {
     InitializeComponent();
     t = new Timer();
     t.Interval = 500;
     t.Tick += new EventHandler(t_Tick);
 }

 void t_Tick(object sender, EventArgs e)
 {
     if (pos < stuff.Length)
     {
         textBox1.AppendText(stuff.Substring(pos, 1));
         ++pos;
     }
     else
     {
         t.Stop();
     }
 }

 private void button1_Click(object sender, EventArgs e)
 {
     pos = 0;
     textBox1.Clear();
     t.Start();
 }

或类似的东西。它会每半秒打勾并在多行文本框中添加另一个字符。只是某人可以做什么的一个例子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2012-01-23
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 2018-03-01
    相关资源
    最近更新 更多