var
  tmr:TTimer;
  dt:TDateTime;
  d2,h2,m2,s2,ms2:word;
  ShowTime,ShowSec,ShutDown,EveryDay:boolean;

procedure ShutDown(action:string);
begin
  case action of
    '':       ShellExecute(0,'open','Shutdown.exe','-s -t 15','',1);
    'cancel': ShellExecute(0,'open','Shutdown.exe','-a','',1);
  end;
end;

procedure RefreshCountDown;
var
  d,h,m,s,ms:word;
  dt1:TDateTime;
  str:string;
begin
  BeginUpdate;
  dt1:=now-dt;
  if dt1<0 then
    str:='还有'
  else 
    str:=''; //'已经';
  if dt1>0 then
    dt1:=dt1
  else
    dt1:=-dt1;
  d:=trunc(dt1);
  if not ShowTime then
    str:=format('%s%d天',[str,d])
  else
  begin
    DecodeTime(dt1,h,m,s,ms);
    if d>0 then str:=format('%s%d天',[str,d]);
    if h>0 then str:=format('%s%d小时',[str,h]);
    if m>0 then str:=format('%s%d分',[str,m]);
    if ShowSec then
      begin
        if s>0 then str:=format('%s%d秒',[str,s]);
      end;
    if ShutDown then
    begin
      if EveryDay then
      begin//每天定时关机
        if (h=0) and(m=0) and (s=0) then
        begin
          ShutDown('');
          imgcancel.visible:=1;
        end
        else
        begin
          dt1:=frac(dt)-frac(now);
          if dt1>0 then
          begin
            lbl.caption:='离关机还有';
            DecodeTime(dt1,h,m,s,ms);
            str:='';
            if d>0 then str:=format('%s%d天',[str,d]);
            if h>0 then str:=format('%s%d小时',[str,h]);
            if m>0 then str:=format('%s%d分',[str,m]);
            if ShowSec or ((str='') and (s>0)) then str:=format('%s%d秒',[str,s]);
          end
          else
          begin
            DecodeTime(dt,h2,m2,s2,ms2);
            lbl.caption:='关机时间';
            str:=format('%d点',[h2]);
            if m2>0 then
              str:=format('%s%d分',[str,m2]);
            else
              str:=format('%s%s分',[str,'00']); 
            str:=str+'(已过)';  
          end; 
        end;
      end
      else if (d=0) and (h=0) and(m=0) and (s=0) then
      begin//仅关机一次
        ShutDown('');
        //ShellExecute(0,'open','Shutdown.exe','-s -t 15','',1);
        imgcancel.visible:=true;
      end;
    end;
  end;

  lblTime.caption:=str;
  //下面的代码在有些情况下会显示不正确
{  if not ShowTime then
    begin
      if d=0 then
       lblTime.caption:='就是今天！';
    end;}
  EndUpdate;
end;

//读取当前Widget的INI设置
procedure InitIni;
var
  y,m,d:word;
begin
  BeginUpdate;
  lbl.caption:=GetSetting('title','离2012伦敦奥运：');
  y:=GetSettingInt('Year',2012);
  m:=GetSettingInt('Month',7);
  d:=GetSettingInt('Day',28);
  ShowTime:=GetSettingInt('ShowTime',1)=1;
  ShowSec:=GetSettingInt('ShowSec',0)=1;
  ShutDown:=GetSettingInt('ShutDown',0)=1;
  EveryDay:=GetSettingInt('EveryDay',0)=1;
  if GetSetting('LunarDate',0)<>1 then
    dt:=EncodeDate(y,m,d)
  else
    dt:=DateCnToDate(y,m,d);
  
  if ShowTime then
  try
    dt:=dt+StrToTime(GetSetting('Time','03:12:00'));
  except
  
  end
  else
    h2:=0; 
  EndUpdate;
end;

//--- Widget移入边栏事件，当Widget被移到边栏上时会触发此事件 ---
procedure OnWidgetDock;
begin
  BeginUpdate;
  Widget.Width:=136;
  EndUpdate;
end;

//--- Widget移出边栏事件，当Widget被移出边栏时会触发此事件 ---
procedure OnWidgetUnDock;
begin
  BeginUpdate;
  Widget.Width:=140;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  InitIni;
  RefreshCountDown;
  if ShowTime then
  begin
    tmr:=TTimer.create(Widget);
    tmr.OnTimer:='tmrTimer';
    if ShowSec or ShutDown then
      tmr.Interval:=1000
    else
      tmr.Interval:=60000;
  end;
  //tmr.Enabled:=false;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  if ShowTime then
    tmr.free;
end;

procedure tmrTimer(sender);
begin
  RefreshCountDown;
end;

//--- 日期改变事件，当日期改变时触发此事件 ---
procedure OnSystemDateChanged(sender);
begin
  RefreshCountDown;
end;