先说个伤心的事情,深圳某9年码农2周前失业了。在家待业,面试也懒得参加,坐吃山空。

倒计时应用定制化开发-LMLPHP

 

 

 

有鉴于此,突然想起读书那会参加了一些竞赛活动,每个参赛选手需要在给定的时间内做一篇全英文演讲。

倒计时30秒的时候开始有声音提醒,时间到了之后,会弹出一个演讲结束图片。

总结一下需求:

1,倒计时开始按钮,和退出应用按钮,以及对应鼠标特效

2,倒计时用两种方式显示:#MM:SS, #倒计时秒数

3,倒计时结束后,弹出结束图片和对应提示声音

4,倒计时参数,图片可以在配置文件自定义

5,全屏显示,窗体可拖动

 

大家懂的,依然是面向百度编程,Pascal和Delphi还是在9年前读书那会玩过,现在已经没啥印象了。

最开始用Lazarus撸了一个版本,发现不规则图片显示效果不太行,遂用Delphi重新撸了一个版本。

图片和按钮都是自己用photoshop绘制,画的不太好,大伙将就看看,也可以在配置文件修改对应图片。

项目代码结构

setting为配置文件

倒计时应用定制化开发-LMLPHP

配置文件

倒计时应用定制化开发-LMLPHP

动画效果如下

倒计时应用定制化开发-LMLPHP

老规矩,祭出界面代码,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.

 

12-28 15:31