Author: Stefan Pettersson
How to scroll a string of text in the caption of a window, in an editbox or in the
application''s title so it''s displayed in the Start-menu toolbar (even when the
form is minimized).
Answer:
It's very simple to scroll a text so it gets displayed in the Start-menu toolbar
the same way as Winamp scrolls a songs title.
First two global variables are needed:
1 var2 ScrollPosition: integer = 0;
3 ScrollText: string = ' This is the scrolltext...';
The first integer ScrollPosition keep tracks of the current position of the scroll
text, and the string ScrollText is the actual text that will scroll. A tip is to
start the scroll text with the same number of spaces as the number of characters
that will be displayed at the same time, then the scroll will seem to start from
the right and scroll to the left instead of the first character popping up directly
at the first position.
The procedure that does the actual scrolling needs to be called every time the
scroll needs to be updated. An OnTimer event (TTimer) suits good for that matter.
4 procedure TForm1.Timer1Timer(Sender: TObject);
5 const6 SCROLL_AREA = 10;
7 begin8 // Gets the part of the scroll that should be displayed9 Form1.Caption := Copy(ScrollText, ScrollPosition, SCROLL_AREA);
10 11 // Increase scroll position to the next character12 Inc(ScrollPosition);
13 14 // Reset position when the scroll has reached it's end15 if ScrollPosition >= Length(ScrollText) then16 ScrollPosition := 0;
17 end;
The code is pretty self explaining together with it's comments. The constant
SCROLL_AREA decides how many characters of the scroll should be displayed at once.
Exactly the same code may be used to scroll text in an TEdit control instead, simply replace Form1.Caption with Edit1.Text (or what the name of the control is) instead. To make the title of the window that is viewed in the Start-menu toolbar to scroll, use Application.Title.