delphi - How to set individual Character Color from a string and display in TLabel? -


i having 1 delphi xe2 project. objective separate individual character string change font color after characters displayed in scrolling tlabel.
project display scrolling tex each characters having different color prevoius character. character color vary according color slider.
color slider have implemented following logic:

  1. using timer1 leftmostcharacter separated , color changed , added label1. label1 scroll right left.
  2. using timer2 rightmostcharacter separated , color changed , added label1. label1 scroll left right.

so have written following codes:
unit 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 '01', '05', '15'   decrementalfactor := 15;  //  may '01', '05', '15'    // leftmost character 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) , (g1 = 000) , (b1 < 255)     begin       b1 := b1 + incrementalfactor;     end   else if (r1 > 000) , (g1 = 000) , (b1 = 255)     begin       r1 := r1 - decrementalfactor;     end   else if (r1 = 000) , (g1 < 255) , (b1 = 255)     begin       g1 := g1 + incrementalfactor;     end   else if (r1 = 000) , (g1 = 255) , (b1 > 000)     begin       b1 := b1 - decrementalfactor;     end   else if (r1 < 255) , (g1 = 255) , (b1 = 000)     begin       r1 := r1 + incrementalfactor;     end   else if (r1 = 255) , (g1 > 000) , (b1 = 000)     begin       g1 := g1 - decrementalfactor;     end   else     begin       timer1.enabled := false;     end;   label1.font.color := rgb(r1, g1, b1);    // trim 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 '01', '05', '15'   decrementalfactor := 15;  //  may '01', '05', '15'    // rightmost character 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) , (g2 = 000) , (b2 < 255)     begin       b2 := b2 + incrementalfactor;     end   else if (r2 > 000) , (g2 = 000) , (b2 = 255)     begin       r2 := r2 - decrementalfactor;     end   else if (r2 = 000) , (g2 < 255) , (b2 = 255)     begin       g2 := g2 + incrementalfactor;     end   else if (r2 = 000) , (g2 = 255) , (b2 > 000)     begin       b2 := b2 - decrementalfactor;     end   else if (r2 < 255) , (g2 = 255) , (b2 = 000)     begin       r2 := r2 + incrementalfactor;     end   else if (r2 = 255) , (g2 > 000) , (b2 = 000)     begin       g2 := g2 - decrementalfactor;     end   else     begin       timer2.enabled := false;     end;   label2.font.color := rgb(r2, g2, b2);    //trim strings   label2.caption := copy(label2.caption, 1, stringlength02 -1);   rightscrollingtext := rightmostcharacter + label2.caption;   label2.caption := rightscrollingtext; end;  end.     

but problem font color varies according color slider whole string (i.e. label1 , label2) instead of individual character.

the tlabel control not give character character styling. cannot hope achieve goal using tlabel. options:

  • paint text yourself, perhaps tpaintbox control. paint text character character.
  • use windowless rich edit control , apply distinct styling each character.
  • use extant library offers such capabilities. example graphics32 gr32_text.

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -