winforms Windows窗体中的摇摆文本动画(沸腾文本/行)

hsvhsicv  于 2023-05-18  发布在  Windows
关注(0)|答案(1)|浏览(157)

我想在前面说:我不确定帖子标签是否正确,也不知道我到底想达到什么目的的正确术语。
因此,我试图实现老式的动画文本,在我的应用程序中的所有文本上摆动/沸腾/抖动/抖动(启用WPF,但不知道它是如何工作的)。不知道如何正确地传达我试图描述的东西,所以我会把这个例子,我可以找到:https://www.youtube.com/watch?v=HUmzcU8llPQ
如果你是一个艺术家,知道如何称呼这种效果(据我所知,通常是它的关键帧),那么请分享它。似乎找不到合适的术语。
我的任务是为一个假设的小学生创建一个数学测验应用程序,当我完成了功能时,剩下的就是在我还可以的时候添加一些舒适的风格。
下面是应用程序现在的样子:

我也许可以通过使用关键帧将每个字母单独动画,然后将它们转换成gif或其他东西,然后显示一堆gif,然而,这似乎是一个迂回的方式来做到这一点,visual studio已经落后了,因为没有任何动画,所以我想避免超载。
我真的没有任何想法从哪里开始,似乎没有人尝试过这在所有之前,所以没有任何信息,我可以找到。
最后,我只剩下一周的时间了,所以如果你认为这是不可能在这么短的时间内实现(特别是考虑到我有点新手),那么请告诉我。更糟糕的情况是,如果它不工作,我可以丢弃它,并恢复到备份,但我真的希望有这样的区别:)

cqoc49vn

cqoc49vn1#

如果你不想使用视频中的软件,而只是使用Winforms。这个可以做一些简单的文字效果,可以参考下面的代码:

using System;
using System.Collections.Generic;
using System. ComponentModel;
using System.Data;
using System. Drawing;
using System. Linq;
using System. Text;
using System. Threading;
using System. Threading. Tasks;
using System. Windows. Forms;

namespace drawing1919
{
     public partial class Form3 : Form
     {
         public Form3()
         {
             InitializeComponent();
         }

         private void Form3_Load(object sender, EventArgs e)
         {
             Graphics Car_Paint = panel1.CreateGraphics();//Instantiate the drawing object
             string Car_Str = "Hello World";//Define the dynamic text to be drawn
             Character character = new Character();//Instantiate a custom class object
             character.CartoonEffect(panel1, Car_Str);//Display dynamic text on the form
         }

         class Character
         {
             System.Drawing.Graphics g;//Define the Graphics object
             static int[] FSize = new int[3] { 20, 25, 30 };//Set font size
             int Str_block = 5;//Interval between fonts
             Font Str_Font = new Font("Microsoft Sans Serif", FSize[0], FontStyle.Bold);//Define the font style
             Color Str_Color = Color.Orange;//Define font color
             float Str_Width = 0;//Get the position of the string
             float Str_Height = 0;
             float Panel_W = 0;//Get the width of the control
             float Panel_H = 0;//Get the height of the control
             Color Panel_C;//Record the background color of the control
             float Str_Odd_Width = 0;//Get the width of a single text
             Thread th;//Definition thread

             /// <summary>
             /// Draw animated text in the Panel control
             /// </summary>
             /// <param Panel="C_Panel">Container control for displaying text</param>
             /// <param string="C_Str">Text string</param>
             public void CartoonEffect(Panel C_Panel, string C_Str)
            {
                 g = C_Panel.CreateGraphics();//Create a Graphics object for the control
                 Panel_H = C_Panel.Height;//Get the height of the control
                 Panel_W = C_Panel.Width;//Get the width of the control
                 Panel_C = C_Panel.BackColor;//Get the background color of the control
                 GetTextInfo(C_Str);//Get the size and position of the text
                 g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//Fill the control with the control background
                 ProtractText(C_Str, 0);//draw text
                                        //Instantiate the ParameterizedThreadStart delegate thread
                 th = new Thread(new ParameterizedThreadStart(DynamicText));
                 th.Start(C_Str);//Pass a string parameter
             }

             /// <summary>
             /// Get the size and drawing position of the text
             /// </summary>
             /// <param string="C_Str">Text string</param>
             public void GetTextInfo(string C_Str)
             {
                 SizeF TitSize = g.MeasureString(C_Str, Str_Font);//Format the drawn string
                 Str_Width = TitSize.Width;//Get the width of the string
                 Str_Height = TitSize.Height;//Get the height of the string
                 Str_Odd_Width = Str_Width / (float)C_Str.Length;//Get the width of a single text
                 Str_Width = (float)((Str_Odd_Width + Str_block) * C_Str.Length);//Get the width of the text
                 Str_Width = (Panel_W - Str_Width) / 2F;//Center the text
                 Str_Height = Panel_H - Str_Height;//Make the text display at the bottom of the control
             }

             /// <summary>
             /// Draw all text
             /// </summary>
             /// <param string="C_Str">The drawn text string</param>
             public void ProtractText(string C_Str, int n)
             {
                 float Str_Place = Str_Width;//The position of a single character
                 for (int i = 0; i < C_Str.Length; i++)//traverse the text in the string
                 {
                     if (i != n)
                         ProtractOddText(C_Str[i].ToString(), Str_Font, Str_Place, Str_Height);//Draw a single text
                     Str_Place += Str_Odd_Width + Str_block;//Get the position of the next text
                 }
             }

             /// <summary>
             /// Draw a single text
             /// </summary>
             /// <param name="C_Odd_Str">Single text string</param>
             /// <param name="S_Font">text style</param>
             /// <param name="left"></param>
             /// <param name="top"></param>
             public void ProtractOddText(string C_Odd_Str, Font S_Font, float left, float top)
             {
                 g.DrawString(C_Odd_Str, S_Font, new SolidBrush(Str_Color), new PointF(left, top));//Draw a single text in the string
             }

             /// <summary>
             /// String traversal through iterators
             /// </summary>
             /// <param string="n">Text string</param>
            /// <returns>Return a single text</returns>
             public static IEnumerable<object> Transpose(string n)
             {
                 if (n.Length > 0)//if the generic is not empty
                 {
                     foreach (object i in n)//traversing the string
                         yield return i;
                 }
             }

             /// <summary>
             /// Draw dynamic text
             /// </summary>
             /// <param string="C_Str">The drawn text string</param>
             public void DynamicText(Object C_Str)
             {
                 float tem_left = 0;//Get the left end position of the current text
                 float tem_top = 0;//Get the top position of the current text
                 float tem_w = 0;//Get the width of the text
                 float tem_h = 0;//get the height of the text
                 float tem_place = Str_Width;//Get the position of the starting text
                 Font Tem_Font = new Font("Microsoft Sans Serif", FSize[0], FontStyle.Bold);//Define the font style
                 int p = 0;
                 //Record the index number of the text in the string
                 int Str_Index = 0;
                 try
                 {
                     foreach (object s in Transpose(C_Str.ToString()))//traversal string
                     {
                         for (int i = 1; i < 5; i++)//
                         {
                             if (i >= 3)
                                 p = Convert.ToInt16(Math.Floor(i/2F));
                             else
                                 p = i;
                             ProtractText(C_Str.ToString(), Str_Index);
                             Tem_Font = new Font("Microsoft Sans Serif", FSize[p], FontStyle.Bold);//Define the font style
                             SizeF TitSize = g.MeasureString(s.ToString(), Str_Font);//Format the drawn single text
                             tem_w = TitSize.Width;//Get the width of the text
                             tem_h = TitSize.Height;//Get the height of the text string
                             tem_left = tem_place - (tem_w - Str_Odd_Width) / 2F;//Get the left end position of the text after changing the size
                             tem_top = Str_Height - (Str_Height - tem_h) / 2F;//Get the top position of the text after changing the size
                             ProtractOddText(s.ToString(), Tem_Font, tem_left, tem_top);//Draw a single text
                             Thread.Sleep(200);//wait for 0.2 seconds
                             g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//clear the drawn text
                         }
                         tem_place += Str_Odd_Width + Str_block;//Calculate the left end position of the next text
                         Str_Index += 1;//position the index number to the next text
                     }
                     ProtractText(C_Str.ToString(), -1);//Restore the original drawing style of the text
                                                        //Instantiate the ParameterizedThreadStart delegate thread
                     th = new Thread(new ParameterizedThreadStart(DynamicText));
                     th.Start(C_Str);//Pass a string parameter
                 }
                 catch//The reason why the exception statement is used here is to close the thread when closing the form
                 {
                     th.Abort();//Close the thread
                 }
             }
         }
     }
}

相关问题