delphi – 如何从字符串中设置单个字符颜色并在TLabel中显示?

delphi – 如何从字符串中设置单个字符颜色并在TLabel中显示?,第1张

概述我有一个Delphi XE2项目.我的目标是将单个字符字符串分开然后更改字体颜色,之后所有字符将显示在滚动TLabel中. 基本上我的项目是显示Scrolling Tex,每个字符都有与prevoius字符不同的颜色.字符颜色将根据颜色滑块而有所不同. 所以我实现了以下逻辑: >使用Timer1,LeftMostCharacter将被分离,颜色将被更改,然后它将被添加到Label1. Label 我有一个Delphi XE2项目.我的目标是将单个字符与字符串分开然后@R_794_6502@颜色,之后所有字符将显示在滚动TLabel中.
基本上我的项目是显示Scrolling Tex,每个字符都有与prevoius字符不同的颜色.字符颜色将根据颜色滑块而有所不同.

所以我实现了以下逻辑:

>使用Timer1,leftMostCharacter将被分离,颜色将被更改,然后它将被添加到Label1. Label1将从右向左滚动.
>使用Timer2,RightmostCharacter将被分开,然后它将被添加到Label1. Label1将从左向右滚动.

所以我写了以下代码:
    单位Unit1;

interfaceuses  WinAPI.windows,WinAPI.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,Vcl.ExtCtrls;type  TMainForm = class(TForm)    Label1: TLabel;    Label2: TLabel;    Timer1: TTimer;    Timer2: TTimer;    button1: Tbutton;    button2: Tbutton;    procedure Timer1Timer(Sender: TObject);    procedure Timer2Timer(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure button1Click(Sender: TObject);    procedure button2Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  MainForm: TMainForm;implementation{$R *.dfm}procedure TMainForm.button1Click(Sender: TObject);begin  Timer1.Enabled := true;  Timer2.Enabled := true;end;procedure TMainForm.button2Click(Sender: TObject);begin  Timer1.Enabled := false;  Timer2.Enabled := false;end;procedure TMainForm.FormCreate(Sender: TObject);begin  MainForm.color := RGB(41,41,41);  Timer1.Interval := 100;  Label1.Font.color := RGB(000,255,000);  Label1.Caption := ' Koushik Halder''s left Scrolling Text Effect Example 001 Koushik Halder''s left Scrolling Text Effect Example 001 ';  Timer2.Interval := 100;  Label2.Font.color := RGB(000,000,255);  Label2.Caption := ' Koushik Halder''s Right Scrolling Text Effect Example 001 Koushik Halder''s Right Scrolling Text Effect Example 001 ';end;procedure TMainForm.Timer1Timer(Sender: TObject);var  StringLength01: integer;  leftScrollingText: string;  leftMostCharacter: string;  R1,G1,B1: Integer;  IncrementalFactor,DecrementalFactor: Integer;begin  IncrementalFactor := 15;  //  May Be '01','05','15'  DecrementalFactor := 15;  //  May Be '01','15'  // Get The leftmost Character From Label1.Caption  StringLength01 := Length(Label1.Caption);  leftMostCharacter  := Label1.Caption[1];  R1 := GetRValue(colorToRGB(Label1.Font.color));  G1 := GetGValue(colorToRGB(Label1.Font.color));  B1 := GetBValue(colorToRGB(Label1.Font.color));  if (R1 = 255) and (G1 = 000) and (B1 < 255) then    begin      B1 := B1 + IncrementalFactor;    end  else if (R1 > 000) and (G1 = 000) and (B1 = 255) then    begin      R1 := R1 - DecrementalFactor;    end  else if (R1 = 000) and (G1 < 255) and (B1 = 255) then    begin      G1 := G1 + IncrementalFactor;    end  else if (R1 = 000) and (G1 = 255) and (B1 > 000) then    begin      B1 := B1 - DecrementalFactor;    end  else if (R1 < 255) and (G1 = 255) and (B1 = 000) then    begin      R1 := R1 + IncrementalFactor;    end  else if (R1 = 255) and (G1 > 000) and (B1 = 000) then    begin      G1 := G1 - DecrementalFactor;    end  else    begin      Timer1.Enabled := false;    end;  Label1.Font.color := RGB(R1,B1);  // Trim The Strings  Label1.Caption := copy(Label1.Caption,2,StringLength01 -1);  leftScrollingText := Label1.Caption + leftMostCharacter;  Label1.Caption := leftScrollingText;end;procedure TMainForm.Timer2Timer(Sender: TObject);var  StringLength02: integer;  RightScrollingText: string;  RightmostCharacter: string;  R2,G2,B2: Integer;  IncrementalFactor,'15'  // Get The Rightmost Character From Label2.Caption  StringLength02 := Length(Label2.Caption);  RightmostCharacter  := Label2.Caption[StringLength02];  R2 := GetRValue(colorToRGB(Label2.Font.color));  G2 := GetGValue(colorToRGB(Label2.Font.color));  B2 := GetBValue(colorToRGB(Label2.Font.color));  if (R2 = 255) and (G2 = 000) and (B2 < 255) then    begin      B2 := B2 + IncrementalFactor;    end  else if (R2 > 000) and (G2 = 000) and (B2 = 255) then    begin      R2 := R2 - DecrementalFactor;    end  else if (R2 = 000) and (G2 < 255) and (B2 = 255) then    begin      G2 := G2 + IncrementalFactor;    end  else if (R2 = 000) and (G2 = 255) and (B2 > 000) then    begin      B2 := B2 - DecrementalFactor;    end  else if (R2 < 255) and (G2 = 255) and (B2 = 000) then    begin      R2 := R2 + IncrementalFactor;    end  else if (R2 = 255) and (G2 > 000) and (B2 = 000) then    begin      G2 := G2 - DecrementalFactor;    end  else    begin      Timer2.Enabled := false;    end;  Label2.Font.color := RGB(R2,B2);  //Trim The Strings  Label2.Caption := copy(Label2.Caption,1,StringLength02 -1);  RightScrollingText := RightmostCharacter + Label2.Caption;  Label2.Caption := RightScrollingText;end;end.

但我的问题是字体颜色根据整个字符串(即Label1和Label2)的颜色滑块而不是单个字符而变化.

解决方法 TLabel控件不会为您提供字符样式.你不能希望用TLabel实现你的目标.一些选择:

>自己绘制文本,可能使用TPaintBox控件.逐个字符地绘制文本.
>使用无窗口的丰富编辑控件,并为每个角色应用不同的样式.
>使用提供此类功能的现有库.例如,带有GR32_Text的graphics32.

总结

以上是内存溢出为你收集整理的delphi – 如何从字符串中设置单个字符颜色并在TLabel中显示?全部内容,希望文章能够帮你解决delphi – 如何从字符串中设置单个字符颜色并在TLabel中显示?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1246012.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存