Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何从字符串中设置单个字符的颜色并在TLabel中显示?_Delphi - Fatal编程技术网

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

Delphi 如何从字符串中设置单个字符的颜色并在TLabel中显示?,delphi,Delphi,我有一个德尔福XE2项目。我的目标是将单个字符从字符串中分离出来,然后更改字体颜色,之后所有字符都将在滚动TLabel中显示 基本上,我的项目是显示滚动Tex,每个字符的颜色与prevoius字符不同。字符颜色将根据颜色滑块的不同而变化 因此,我实现了以下逻辑: 使用Timer1将分离LeftMostCharacter,并更改颜色,然后将其添加到Label1标签1将从右向左滚动 使用Timer2将分离最右侧的字符,并更改颜色,然后将其添加到标签1标签1将从左向右滚动 因此,我编写了以下代码: 第

我有一个德尔福XE2项目。我的目标是将单个字符从字符串中分离出来,然后更改
字体颜色
,之后所有字符都将在滚动
TLabel
中显示
基本上,我的项目是显示
滚动Tex
,每个字符的颜色与prevoius字符不同。字符颜色将根据
颜色滑块的不同而变化

因此,我实现了以下逻辑:

  • 使用
    Timer1
    将分离
    LeftMostCharacter
    ,并更改颜色,然后将其添加到
    Label1
    <代码>标签1
  • 将从右向左滚动
  • 使用
    Timer2
    将分离
    最右侧的字符
    ,并更改颜色,然后将其添加到
    标签1
    <代码>标签1
  • 将从左向右滚动 因此,我编写了以下代码:
    第一单元

    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.    
    
    接口
    使用
    Winapi.Windows、Winapi.Messages、System.SysUtils、System.Variants、System.Classes、Vcl.Graphics、,
    Vcl.控件、Vcl.窗体、Vcl.对话框、Vcl.StdCtrls、Vcl.ExtCtrls;
    类型
    TMainForm=类(TForm)
    标签1:TLabel;
    标签2:TLabel;
    定时器1:TTimer;
    Timer2:TTimer;
    按钮1:t按钮;
    按钮2:t按钮;
    程序定时器1定时器(发送方:TObject);
    程序定时器2定时器(发送方:ToObject);
    过程表单创建(发送方:ToObject);
    程序按钮1点击(发送方:ToObject);
    程序按钮2点击(发送者:对象);
    私有的
    {私有声明}
    公众的
    {公开声明}
    结束;
    变量
    主要形式:TMA通知;
    实施
    {$R*.dfm}
    程序TMainForm.按钮1单击(发件人:ToObject);
    开始
    Timer1.Enabled:=真;
    Timer2.Enabled:=真;
    结束;
    程序TMainForm.按钮2点击(发送方:TObject);
    开始
    Timer1.Enabled:=false;
    Timer2.Enabled:=false;
    结束;
    程序TMAInformCreate(发送方:ToObject);
    开始
    颜色:=RGB(41,41,41);
    计时器1.间隔:=100;
    Label1.Font.Color:=RGB(000255000);
    标签1.标题:=“Koushik Halder的左滚动文本效果示例001 Koushik Halder的左滚动文本效果示例001”;
    Timer2.间隔:=100;
    Label2.Font.Color:=RGB(000000255);
    标签2.标题:=“Koushik Halder的右滚动文本效果示例001 Koushik Halder的右滚动文本效果示例001”;
    结束;
    程序TMainForm.Timer1Timer(发送方:TObject);
    变量
    StringLength01:整数;
    LeftScrollingText:字符串;
    LeftMostCharacter:字符串;
    R1,G1,B1:整数;
    递增因子、递减因子:整数;
    开始
    增量系数:=15;//可能是'01','05','15'
    递减系数:=15;//可能是'01','05','15'
    //从Label1.Caption中获取最左边的字符
    StringLength01:=长度(标签1.标题);
    LeftMostCharacter:=Label1.标题[1];
    R1:=GetRValue(ColorToRGB(Label1.Font.Color));
    G1:=GetGValue(ColorToRGB(Label1.Font.Color));
    B1:=GetBValue(ColorToRGB(Label1.Font.Color));
    如果(R1=255)和(G1=000)以及(B1<255),则
    开始
    B1:=B1+增量系数;
    结束
    否则,如果(R1>000)和(G1=000)和(B1=255),则
    开始
    R1:=R1——递减因子;
    结束
    否则,如果(R1=000)和(G1<255)和(B1=255),则
    开始
    G1:=G1+递增系数;
    结束
    否则,如果(R1=000)和(G1=255)以及(B1>000),则
    开始
    B1:=B1——递减系数;
    结束
    否则,如果(R1<255)和(G1=255)以及(B1=000),则
    开始
    R1:=R1+递增系数;
    结束
    否则,如果(R1=255)和(G1>000)和(B1=000),则
    开始
    G1:=G1——递减因子;
    结束
    其他的
    开始
    Timer1.Enabled:=false;
    结束;
    Label1.Font.Color:=RGB(R1、G1、B1);
    //修剪琴弦
    Label1.Caption:=副本(Label1.Caption,2,StringLength01-1);
    LeftScrollingText:=Label1.Caption+LeftMostCharacter;
    标签1.标题:=LeftScrollingText;
    结束;
    程序TMainForm.Timer2Timer(发送方:TObject);
    变量
    StringLength02:整数;
    右滚动文本:字符串;
    最右边的字符:字符串;
    R2,G2,B2:整数;
    递增因子、递减因子:整数;
    开始
    增量系数:=15;//可能是'01','05','15'
    递减系数:=15;//可能是'01','05','15'
    //从Label2中获取最右边的字符。标题
    StringLength02:=长度(标签2.标题);
    最右边的字符:=Label2.标题[StringLength02];
    R2:=GetRValue(ColorToRGB(Label2.Font.Color));
    G2:=GetGValue(ColorToRGB(Label2.Font.Color));
    B2:=GetBValue(ColorToRGB(Label2.Font.Color));
    如果(R2=255)和(G2=000)以及(B2<255),则
    开始
    B2:=B2+增量系数;
    结束
    否则,如果(R2>000)和(G2=000)和(B2=255),则
    开始
    R2:=R2-递减因子;
    结束
    否则,如果(R2=000)和(G2<255)和(B2=255),则
    开始
    G2:=G2+递增系数;
    结束
    否则,如果(R2=000)和(G2=255)和(B2>000),则
    开始
    B2:=B2——递减因子;
    结束
    否则,如果(R2<255)和(G2=255)和(B2=000),则
    开始
    R2:=R2+递增系数;
    结束
    否则,如果(R2=255)和(G2>000)和(B2=000),则
    开始
    G2:=G2-递减因子;
    结束
    其他的
    开始
    Timer2.Enabled:=false;
    结束;
    Label2.Font.Color:=RGB(R2、G2、B2);
    //修剪琴弦
    Label2.Caption:=副本(Label2.Caption,1,StringLength02-1);
    RightScrollingText:=RightMostCharacter+Label2.标题;
    标签2.标题:=右滚动文本;
    结束;
    结束。
    

    但我的问题是,整个
    字符串(即
    Label1
    Label2
    )的
    Font-Color
    根据
    Color Slider
    变化,而不是单个字符。

    TLabel
    控件不提供字符样式。你不可能指望取得成功