C#使用迭代器實現(xiàn)動態(tài)文字效果
本文主介紹如何使用C#實現(xiàn)動態(tài)文字效果。效果圖如下

實現(xiàn)方式
使用Drawing空間下的類,將文字重繪制,并輸出到Panel控件的畫布中
1、設定相關參數(shù),字體大小、字體樣式、字體顏色、控件的寬度和高度、字體的寬度和高度、以及單個文字的寬度
2、在Panel控件中創(chuàng)建畫布,并獲取控件的高度、寬度、背景顏色,以及文字的高度、寬度,并設置繪制字體的樣式,包括高度、寬度以及字體規(guī)格等等。
知識點
Graphics
封裝一個 GDI+ 繪圖圖面。 此類不能被繼承,屬于System.Drawing命名空間。通俗來說,就是一塊畫布
本項目用到此類的方法屬性主要有:
FillRectangle:
public void FillRectangle(System.Drawing.Brush brush, float x, float y, float width, float height);
FillRectangle(Brush, Single, Single, Single, Single)
| 參數(shù) | 類型 | 說明 |
|---|---|---|
| brush | Brush | 定義用于填充圖形形狀(如矩形、橢圓、餅形、多邊形和封閉路徑)的內部的對象 |
| X | Single | 要填充的矩形左上角的 x 坐標 |
| Y | Single | 要填充的矩形左上角的 y 坐標 |
| width | Single | 要填充的矩形的寬度 |
| height | Single | 要填充的矩形的高度 |
填充由一對坐標、寬度和高度指定的矩形的內部。
System.Drawing.Graphics g;//定義Graphics對象 g= C_Panel.CreateGraphics(); g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//用控件背景填充控件
MeasureString
用指定的 Font繪制時度量指定的字符串。
MeasureString(String, Font)
String:要度量的字符串
Font:定義字符串文本格式的 Font。
此方法返回一個 SizeF 結構,該結構代表用 font 參數(shù)繪制的由 text 參數(shù)指定的字符串的大小
SizeF TitSize = g.MeasureString(C_str, Str_Font);//將繪制的字符串進行格式化 Str_Width = TitSize.Width;//獲取字符串的寬度 Str_Height = TitSize.Height;//獲取字符串的高度 Str_Odd_Width=Str_Width /(float)C_str.Length;//獲取單個文字的高度 Str_Width = (float)((Str_Odd_Width + Str_block) * C_str.Length);//獲取文字的寬度 Str_Width = (Panel_W - Str_Width) / 2F;//使文字居中 Str_Height = Panel_H - Str_Height;//使文字顯示在控件底端
DrawString
使用指定的 Brush 和 Font 對象在指定位置繪制指定的文本字符串
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point);
參數(shù) s String 要繪制的字符串。
font Font 定義字符串文本格式的 Font。
brush Brush 確定所繪制文本的顏色和紋理的 Brush。
point PointF PointF 指定繪制文本左上角的結構。
public void DrawStringPointF(PaintEventArgs e)
{
// Create string to draw.
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(150.0F, 150.0F);
// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
Font
定義特定的文本格式,包括字體、字號和樣式特性。 此類不能被繼承。
屬于System.Drawing命名空間。通俗來說,就是一個畫布上的字體
通過構造函數(shù),傳入字體參數(shù),然后將Font類作為參數(shù),再傳入到SolidBrush類。
Font(FontFamily, Single, FontStyle)
FontFamily:字體種類,黑體,宋體等等
Single:字體大小
FontStyle:字體類型,斜體、黑體等等
Font Tem_Font = new Font("黑體", FSize[0], FontStyle.Bold);//定義字體樣式
Graphics .DrawString(C_Odd_Str, S_Font, new SolidBrush(Str_Color), new PointF(left,top));//繪制字符串中單個文字
SizeF
存儲有序浮點數(shù)對,通常為矩形的寬度和高度
private void AddShadow(PaintEventArgs e)
{
// Create two SizeF objects.
SizeF shadowSize = listBox1.Size;
SizeF addSize = new SizeF(10.5F, 20.8F);
// Add them together and save the result in shadowSize.
shadowSize = shadowSize + addSize;
// Get the location of the ListBox and convert it to a PointF.
PointF shadowLocation = listBox1.Location;
// Add two points to get a new location.
shadowLocation = shadowLocation + new Size(5, 5);
// Create a rectangleF.
RectangleF rectFToFill =
new RectangleF(shadowLocation, shadowSize);
// Create a custom brush using a semi-transparent color, and
// then fill in the rectangle.
Color customColor = Color.FromArgb(50, Color.Gray);
SolidBrush shadowBrush = new SolidBrush(customColor);
e.Graphics.FillRectangles(shadowBrush, new RectangleF[]{rectFToFill});
// Dispose of the brush.
shadowBrush.Dispose();
}
PointF
表示在二維平面中定義點的浮點 x 和 y 坐標的有序對。
構造函數(shù):PointF(Single, Single)
public PointF (float x, float y);
x Single 該點的水平位置。
y Single 該點的垂直位置。
public void DrawStringPointF(PaintEventArgs e)
{
// Create string to draw.
String drawString = "Sample Text";
// Create font and brush.
Font drawFont = new Font("Arial", 16);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Create point for upper-left corner of drawing.
PointF drawPoint = new PointF(150.0F, 150.0F);
// Draw string to screen.
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
SolidBrush
定義單色畫筆。 畫筆用于填充圖形形狀,如矩形、橢圓、扇形、多邊形和封閉路徑。 此類不能被繼承。
構造函數(shù):SolidBrush(Color)
public void CartoonEffect(Panel C_Panel,string C_Str)
{
g= C_Panel.CreateGraphics();//在panel控件中創(chuàng)建畫布,即Graphics
Panel_H = C_Panel.Height;
Panel_W = C_Panel.Width;
Panel_C=C_Panel.BackColor;
GetTextInfo(C_Str);
g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//用控件背景填充控件
ProtractText(C_Str, 0);//繪制文字
th=new Thread(new ParameterizedThreadStart(DynamicText));
th.Start(C_Str);
}
Panel控件
方法:CreateGraphics
創(chuàng)建畫布
System.Drawing.Graphics g;//定義Graphics對象 g= C_Panel.CreateGraphics();//在panel控件中創(chuàng)建畫布,即Graphics
線程
Thread
ParameterizedThreadStart 委托
表示在 Thread 上執(zhí)行的方法。
public delegate void ParameterizedThreadStart(object? obj);
obj:包含線程過程的數(shù)據的對象。
using System;
using System.Threading;
public class Work
{
public static void Main()
{
// Start a thread that calls a parameterized static method.
Thread newThread = new Thread(Work.DoWork);
newThread.Start(42);
// Start a thread that calls a parameterized instance method.
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
newThread.Start("The answer.");
}
public static void DoWork(object data)
{
Console.WriteLine("Static thread procedure. Data='{0}'",
data);
}
public void DoMoreWork(object data)
{
Console.WriteLine("Instance thread procedure. Data='{0}'",
data);
}
}
// This example displays output like the following:
// Static thread procedure. Data='42'
// Instance thread procedure. Data='The answer.'
迭代
public static IEnumerable<object> Transpose(string n)
{
if(n.Length>0) //如果泛型不為空
{
foreach(object i in n) //對字符串進行遍歷
{
yield return i;
}
}
}
foreach(object s in Transpose(C_Str.ToString()))
{
ProtractOddText(s.ToString(), Tem_Font, tem_left, tem_top);//繪制單個文字
}
畫面組態(tài)

控件:Panel控件
代碼解析
private void Form1_Load(object sender, EventArgs e)
{
Graphics Car_Paint = panel1.CreateGraphics();//實例化繪圖對象
string Car_Str = "你好,你在哪呢";
Character character = new Character();//實例化自定義類對象
character.CartoonEffect(panel1, Car_Str);//在窗體上顯示動態(tài)文字
}
class Character
{
System.Drawing.Graphics g;//定義Graphics對象
static int[] FSize = new int[3] { 20, 25, 30 };//設置字體的大小
int Str_block = 5;//字體間隔時間
Font Str_Font = new Font("宋體", FSize[0], FontStyle.Bold);//定義字體樣式
Color Str_Color = Color.Orange;//定義字體顏色
float Str_Width = 0;//獲取字符串的位置
float Str_Height = 0;
float Panel_W = 0;//獲取控件的寬度
float Panel_H = 0;//獲取控件的高度
Color Panel_C;//記錄控件的背景顏色
float Str_Odd_Width = 0;//獲取單個文字的寬度
Thread th;//定義線程
public void CartoonEffect(Panel C_Panel,string C_Str)
{
g= C_Panel.CreateGraphics();//在panel控件中創(chuàng)建畫布,即Graphics
Panel_H = C_Panel.Height;
Panel_W = C_Panel.Width;
Panel_C=C_Panel.BackColor;
GetTextInfo(C_Str);
g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//用控件背景填充控件
ProtractText(C_Str, 0);//繪制文字
th=new Thread(new ParameterizedThreadStart(DynamicText));
th.Start(C_Str);
}
/// <summary>
/// 獲取文字的大小及繪制位置
/// </summary>
/// <param name="C_str">文字字符串</param>
public void GetTextInfo(string C_str)
{
SizeF TitSize = g.MeasureString(C_str, Str_Font);//將繪制的字符串進行格式化
Str_Width = TitSize.Width;//獲取字符串的寬度
Str_Height = TitSize.Height;//獲取字符串的高度
Str_Odd_Width=Str_Width /(float)C_str.Length;//獲取單個文字的高度
Str_Width = (float)((Str_Odd_Width + Str_block) * C_str.Length);//獲取文字的寬度
Str_Width = (Panel_W - Str_Width) / 2F;//使文字居中
Str_Height = Panel_H - Str_Height;//使文字顯示在控件底端
}
public void ProtractText(string C_Str,int n)
{
float Str_Place = Str_Width;//單個字符的位置
for(int i=0;i<C_Str.Length;i++)
{
if(i!=n)
{
ProtractOddText(C_Str[i].ToString(), Str_Font, Str_Place, Str_Height);//繪制單個文字
Str_Place += Str_Odd_Width + Str_block;//獲取下一個文字的位置
}
}
}
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));//繪制字符串中單個文字
}
public static IEnumerable<object> Transpose(string n)
{
if(n.Length>0) //如果泛型不為空
{
foreach(object i in n) //對字符串進行遍歷
{
yield return i;
}
}
}
public void DynamicText(object C_Str)
{
float tem_left = 0;//獲取當前文字的左端位置
float tem_top = 0;//獲取當前文字的頂端位置
float tem_W = 0;
float tem_H = 0;
float tem_place = Str_Width;//獲取起始文字的位置
Font Tem_Font = new Font("黑體", FSize[0], FontStyle.Bold);//定義字體樣式
int p = 0;//記錄字符串中文字的索引號
int Str_Index = 0;
try
{
foreach(object s in Transpose(C_Str.ToString()))
{
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("黑體", FSize[p], FontStyle.Bold);//定義字體樣式
SizeF TitSize = g.MeasureString(s.ToString(), Str_Font);//將繪制的單個文字進行格式化
tem_W = TitSize.Width;//獲取文字的寬度
tem_H = TitSize.Height;//獲取文字的高度
tem_left = tem_place - (tem_W - Str_Odd_Width) / 2F;//獲取文字改變大小后的左端位置
tem_top = Str_Height - (Str_Height - tem_H) / 2F;
ProtractOddText(s.ToString(), Tem_Font, tem_left, tem_top);//繪制單個文字
Thread.Sleep(200);//等待0.2秒
g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);
}
tem_place += Str_Odd_Width + Str_block;//計算下一個文字的左端位置
Str_Index += 1;
}
ProtractText(C_Str.ToString(), -1);//恢復文字的原始繪制樣式
th = new Thread(new ParameterizedThreadStart(DynamicText));
th.Start(C_Str);
}
catch
{
th.Abort();
}
}
}
}
文字繪制
private void Form1_Load(object sender, EventArgs e)
{
Graphics Car_Paint = panel1.CreateGraphics();//實例化繪圖對象
string Car_Str = "你好,你在哪呢";
Character character = new Character();//實例化自定義類對象
character.CartoonEffect(panel1, Car_Str);//在窗體上顯示動態(tài)文字
}
class Character
{
System.Drawing.Graphics g;//定義Graphics對象
static int[] FSize = new int[3] { 20, 25, 30 };//設置字體的大小
int Str_block = 5;//字體間隔時間
Font Str_Font = new Font("宋體", FSize[0], FontStyle.Bold);//定義字體樣式
Color Str_Color = Color.Orange;//定義字體顏色
float Str_Width = 0;//獲取字符串的位置
float Str_Height = 0;
float Panel_W = 0;//獲取控件的寬度
float Panel_H = 0;//獲取控件的高度
Color Panel_C;//記錄控件的背景顏色
float Str_Odd_Width = 0;//獲取單個文字的寬度
public void CartoonEffect(Panel C_Panel,string C_Str)
{
g= C_Panel.CreateGraphics();//在panel控件中創(chuàng)建畫布,即Graphics
Panel_H = C_Panel.Height;
Panel_W = C_Panel.Width;
Panel_C=C_Panel.BackColor;
GetTextInfo(C_Str);
g.FillRectangle(new SolidBrush(Panel_C), 0, 0, Panel_W, Panel_H);//用控件背景填充控件
ProtractText(C_Str, 0);//繪制文字
}
/// <summary>
/// 獲取文字的大小及繪制位置
/// </summary>
/// <param name="C_str">文字字符串</param>
public void GetTextInfo(string C_str)
{
SizeF TitSize = g.MeasureString(C_str, Str_Font);//將繪制的字符串進行格式化
Str_Width = TitSize.Width;//獲取字符串的寬度
Str_Height = TitSize.Height;//獲取字符串的高度
Str_Odd_Width=Str_Width /(float)C_str.Length;//獲取單個文字的高度
Str_Width = (float)((Str_Odd_Width + Str_block) * C_str.Length);//獲取文字的寬度
Str_Width = (Panel_W - Str_Width) / 2F;//使文字居中
Str_Height = Panel_H - Str_Height;//使文字顯示在控件底端
}
public void ProtractText(string C_Str,int n)
{
float Str_Place = Str_Width;//單個字符的位置
for(int i=0;i<C_Str.Length;i++)
{
if(i!=n)
{
ProtractOddText(C_Str[i].ToString(), Str_Font, Str_Place, Str_Height);//繪制單個文字
Str_Place += Str_Odd_Width + Str_block;//獲取下一個文字的位置
}
}
}
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));//繪制字符串中單個文字
}
以上就是C#使用迭代器實現(xiàn)動態(tài)文字效果的詳細內容,更多關于C#迭代器動態(tài)文字的資料請關注腳本之家其它相關文章!
相關文章
詳解如何實現(xiàn)C#和Python間實時視頻數(shù)據交互
我們在做RTSP|RTMP播放的時候,遇到好多開發(fā)者,他們的視覺算法大多運行在python下,需要高效率的實現(xiàn)C#和Python的視頻數(shù)據交互,本文給大家總結了一些常用的方法,感興趣的小伙伴跟著小編一起來看看吧2024-10-10
C#結合Spire.XLS for .NET實現(xiàn)自動創(chuàng)建Excel數(shù)據透視圖
在當今環(huán)境中,數(shù)據分析和可視化扮演著至關重要的角色,Excel數(shù)據透視圖以其強大的交互性和靈活性,成為了許多業(yè)務分析師和決策者不可或缺的工具,下面我們就來看看如何使用C#實現(xiàn)自動創(chuàng)建Excel數(shù)據透視圖吧2025-12-12
C#使用RestSharp實現(xiàn)封裝常用的http請求方法
這篇文章主要為大家詳細介紹了C#如何使用RestSharp實現(xiàn)封裝常用的http請求方法,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2024-02-02

