1. 程式人生 > >倒計時應用定製化開發

倒計時應用定製化開發

先說個傷心的事情,深圳某9年碼農2周前失業了。在家待業,面試也懶得參加,坐吃山空。

 

 

 

有鑑於此,突然想起讀書那會參加了一些競賽活動,每個參賽選手需要在給定的時間內做一篇全英文演講。

倒計時30秒的時候開始有聲音提醒,時間到了之後,會彈出一個演講結束圖片。

總結一下需求:

1,倒計時開始按鈕,和退出應用按鈕,以及對應滑鼠特效

2,倒計時用兩種方式顯示:#MM:SS, #倒計時秒數

3,倒計時結束後,彈出結束圖片和對應提示聲音

4,倒計時引數,圖片可以在配置檔案自定義

5,全屏顯示,窗體可拖動

 

大家懂的,依然是面向百度程式設計,Pascal和Delphi還是在9年前讀書那會玩過,現在已經沒啥印象了。

最開始用Lazarus擼了一個版本,發現不規則圖片顯示效果不太行,遂用Delphi重新擼了一個版本。

圖片和按鈕都是自己用photoshop繪製,畫的不太好,大夥將就看看,也可以在配置檔案修改對應圖片。

專案程式碼結構

setting為配置檔案

配置檔案

 

 

 

 

 

動畫效果如下

老規矩,祭出介面程式碼,Delphi不太熟,程式碼寫的比較亂,勿噴。

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls,
  jpeg,pngimage, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    imgstop: TImage;
    imgstart: TImage;
    imgclose: TImage;
    lbl: TLabel;
    lbl2: TLabel;
    timercountdown: TTimer;
    timertimeout: TTimer;
    lbltime: TLabel;
    timertime: TTimer;
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure imgcloseClick(Sender: TObject);
    procedure imgstartClick(Sender: TObject);
    procedure timercountdownTimer(Sender: TObject);
    procedure timertimeoutTimer(Sender: TObject);
    procedure imgcloseMouseEnter(Sender: TObject);
    procedure imgcloseMouseLeave(Sender: TObject);
    procedure imgstartMouseLeave(Sender: TObject);
    procedure imgstartMouseEnter(Sender: TObject);
    procedure FormDblClick(Sender: TObject);
    procedure timertimeTimer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure InitImages;
    procedure MakeFullScreen;
    function ConvertIntToTimeParts(const value: Integer):string;
    procedure ResizeControls;
  end;

var
  Form1: TForm1;
  picstop, picstart, picstartover, picclose,piccloseover: TPngImage;
  countdown, timeout: integer;
  config: tstrings;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  //
  config:=tstringlist.Create;
  config.LoadFromFile('setting');
  //
  countdown := StrToInt(config.Values['countdown']) ;
  timeout:= StrToInt(config.Values['due_timeout'])  ;
  //
  MakeFullScreen;
  ResizeControls;
  initimages;
end;

procedure TForm1.FormDblClick(Sender: TObject);
begin
  self.MakeFullScreen;
  self.ResizeControls;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  releasecapture;
  postmessage(handle, WM_SYSCOMMAND, SC_MOVE+1,0);
end;

procedure TForm1.imgcloseClick(Sender: TObject);
begin
  self.Close;
end;

procedure TForm1.imgcloseMouseEnter(Sender: TObject);
begin
  imgclose.Picture.Assign(piccloseover);
end;

procedure TForm1.imgcloseMouseLeave(Sender: TObject);
begin
  imgclose.Picture.Assign(picclose);
end;

procedure TForm1.imgstartClick(Sender: TObject);
begin
  countdown := StrToInt(config.Values['countdown']) ;
  timeout:= StrToInt(config.Values['due_timeout'])  ;
  //
  self.timercountdown.Enabled:= not self.timercountdown.Enabled;
  self.timertimeout.Enabled:=false;
  lbl.Caption:=inttostr(countdown);
  lbl2.Caption:= ConvertIntToTimeParts(countdown);
  lbl.Font.Color:=clBlack;
end;

procedure TForm1.imgstartMouseEnter(Sender: TObject);
begin
  imgstart.Picture.Assign(picstartover);
end;

procedure TForm1.imgstartMouseLeave(Sender: TObject);
begin
  imgstart.Picture.Assign(picstart);
end;

procedure TForm1.InitImages;
var
  bmp: TBitmap;
  jpg: TJpegImage;
  datadir:string;
begin
  datadir:=config.Values['data_dir'];
  // init stop
  picstop:=TPngImage.Create;
  picstop.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_stop']]));
  imgstop.Stretch:=true;
  imgstop.Visible:=false;
  imgstop.Transparent:=true;
  imgstop.Picture.Assign(picstop);
  imgstop.Left:=(self.Width-imgstop.width) div 2;
  imgstop.Top:=(self.Height-imgstop.height) div 2;
  imgstop.BringToFront;
  // init start
  picstart:=TPngImage.Create;
  picstart.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_start']]));
  picstartover:=TPngImage.Create;
  picstartover.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_start_over']]));
  imgstart.Stretch:=true;
  imgstart.Picture.Assign(picstart);
  imgstart.Left:=self.Width-imgstart.Width-10;
  imgstart.Top:=imgclose.Top+imgclose.Height+60;
  // init close
  picclose:=TPngImage.Create;
  picclose.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_close']]));
  piccloseover:=TPngImage.Create;
  piccloseover.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_close_over']]));
  imgclose.Stretch:=true;
  imgclose.Picture.Assign(picclose);
  imgclose.Left:=imgstart.Left;
  imgclose.top:=50;
  // init bg
  jpg:=TJpegImage.Create;
  jpg.LoadFromFile(Format('%s/%s',[datadir,config.Values['bg_main']]));
  bmp:=TBitmap.Create;
  bmp.Assign(jpg);
  self.Brush.Bitmap := bmp;
  jpg.Free;
  // init labels
  lbl.Caption:=inttostr(countdown);
  lbl2.Caption:= ConvertIntToTimeParts(countdown);


end;

function TForm1.ConvertIntToTimeParts(const value: Integer):string;
var
  min, sec: string;
begin
  min := inttostr(value div 60);
  sec := inttostr(value mod 60);
  Result := stringofchar('0',2-length(min)) + min + ':'
         + stringofchar('0',2-length(sec)) + sec;
end;

procedure TForm1.MakeFullScreen;
begin
  //覆蓋工作列
  Self.BorderStyle := bsNone;
  Self.Left := 0;
  Self.Top := 0;
  Self.Width := Screen.Width;
  Self.Height := Screen.Height;
end;

procedure TForm1.ResizeControls;
begin
  lbl.Caption:=inttostr(countdown);
  lbl2.Caption:= ConvertIntToTimeParts(countdown);
  //
  lbl.Left:=(Self.Width-lbl.Width) div 2;
  lbl.Top:=(Self.Height-lbl.Height) div 2;
  {}
  lbl2.Left:=(Self.Width-lbl2.Width) div 2;
  lbl2.Top:=(Self.Height-2*lbl.Height-lbl2.Height) div 2;
  //
  lbltime.Caption:=FormatDateTime('yyyy-mm-dd HH:MM:SS', now);
  lbltime.Left:=self.Width-lbltime.Width-10;
  lbltime.Top:=1;
end;

procedure TForm1.timercountdownTimer(Sender: TObject);
begin
  countdown := countdown-1;
  lbl.Caption:= inttostr(countdown);
  lbl2.Caption:= ConvertIntToTimeParts(countdown);
  //
  if countdown<=0 then
    begin
      self.timercountdown.Enabled:=False;
      self.timertimeout.Enabled:=true;
      imgStop.visible:=true;
    end
  else if (countdown=30) or (countdown<=10) then
    begin
      lbl.Font.Color:=clRed;
      MessageBeep(MB_OK);
    end
end;

procedure TForm1.timertimeoutTimer(Sender: TObject);
begin
  timeout := timeout-1;
  if timeout<=0 then
    begin
      self.timertimeout.Enabled:=false;
      imgstop.visible:=false;
    end;
end;

procedure TForm1.timertimeTimer(Sender: TObject);
begin
  self.lbltime.Caption:=FormatDateTime('yyyy-mm-dd HH:MM:SS', now);
end;

end.

&n