2009. április 20., hétfő

Scroll text in title bar of a window


Problem/Question/Abstract:

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:

var
  ScrollPosition: integer = 0;
  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.

procedure TForm1.Timer1Timer(Sender: TObject);
const
  SCROLL_AREA = 10;
begin
  // Gets the part of the scroll that should be displayed
  Form1.Caption := Copy(ScrollText, ScrollPosition, SCROLL_AREA);

  // Increase scroll position to the next character
  Inc(ScrollPosition);

  // Reset position when the scroll has reached it's end
  if ScrollPosition >= Length(ScrollText) then
    ScrollPosition := 0;
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.

Nincsenek megjegyzések:

Megjegyzés küldése