본문 바로가기

Delphi/꿀팁

타이머로 간단하게 흐르는 텍스트효과 주기

폼에 TImage와 TTimer 컴포넌트를 하나씩 올려줍니다.

type 
  TFlowText = record
    Text : string;
    TextWidth : Integer;
    TextX : Integer;
    TextY : Integer;
    Width : Integer;
  end;

  TForm1 = class(TForm)
    Image1: TImage;
    Timer1: TTimer;
    btnStart: TButton;
    btnPause: TButton;
    procedure btnStartClick(Sender: TObject);
    procedure btnPauseClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);   
  private
    FFlowText: TFlowText;
  public
    property FlowText: TFlowText read FFlowText;
  end;
procedure TForm1.BtnStartClick(Sender: TObject);
begin
  Timer1.Enabled := False;

  Image1.Canvas.FillRect(Image1.Canvas.ClipRect);
  Image1.Canvas.Font.Size := 20; //글씨 크기
  
  Timer1.Interval = 10; //흐르는 속도

  FFlowText := Default(TFlowText);
  FFlowText.Width := Image1.Width;
  FFlowText.Text := 'Developist''s Coding Story'; //텍스트 내용
  FFlowText.TextWidth := Image1.Canvas.TextWidth(FFlowText.Text);
  FFlowText.TextX := Image1.Width; //오른쪽 끝에서 시작
  FFlowText.TextY := (Image1.Height - Image1.Canvas.TextHeight(FFlowText.Text)) div 2; //중앙정렬

  Timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;

  Dec(FlowText.TextX, 1); //흐르는 간격, 오른쪽 -> 왼쪽

  if (FlowText.TextX + FlowText.Width) = 0 then
    FlowText.TextX := FlowText.Width; //텍스트 끝이 전광판을 벗어나면 다시 처음으로
    
  Image1.Canvas.TextOut(FlowText.TextX, FlowText.TextY, FlowText.Text); //텍스트 출력

  Timer1.Enabled := True;
end;
procedure TForm1.BtnPauseClick(Sender: TObject);
begin
  Timer1.Enabled := not Timer1.Enabled; //일시정지
end;​

2021.07.22 - [Delphi/끄적이기] - 화면에 스크롤링 텍스트 기능 구현하기

반응형