簡體   English   中英

如何從字符串中設置單個字符顏色並在TLabel中顯示?

[英]How to set individual Character Color from a string and display in TLabel?

我有一個Delphi XE2項目。 我的目標是將單個字符與字符串分開然后更改Font Color之后所有字符將顯示在滾動TLabel
基本上我的項目是顯示Scrolling Tex ,每個字符都有與prevoius字符不同的顏色。 字符顏色將根據Color Slider而有所不同。
彩色滑塊 所以我實現了以下邏輯:

  1. 使用Timer1LeftMostCharacter將被分開,顏色將被更改,然后它將被添加到Label1 Label1將從右向左滾動。
  2. 使用Timer2RightMostCharacter將被分開,顏色將被更改,然后它將被添加到Label1 Label1將從左向右滾動。

所以我寫了以下代碼:
單位Unit1;

interface

uses
  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', '05', '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, G1, 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, DecrementalFactor: Integer;
begin
  IncrementalFactor := 15;  //  May Be '01', '05', '15'
  DecrementalFactor := 15;  //  May Be '01', '05', '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, G2, B2);

  //Trim The Strings
  Label2.Caption := Copy(Label2.Caption, 1, StringLength02 -1);
  RightScrollingText := RightMostCharacter + Label2.Caption;
  Label2.Caption := RightScrollingText;
end;

end.    

但我的問題是Font Color根據整個String (即Label1Label2 )的Color Slider而不是單個字符而變化。

TLabel控件不會為您提供字符樣式。 你不能希望用TLabel實現你的目標。 一些選擇:

  • 自己繪制文本,可能使用TPaintBox控件。 逐個字符地繪制文本。
  • 使用無窗口的豐富編輯控件,並為每個角色應用不同的樣式。
  • 使用提供此類功能的現有庫。 例如帶有GR32_Text的graphics32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM