const
  bgCount=100;

var
  tmr:TTimer;
  aDriveName:string;
  aTotal,aFree:Real;
  bgIndex:integer;
  UseFSO:boolean;

procedure OpenDrive;
begin
  ShellExecute(0,'open',aDriveName+':\','','',1);
end;

//刷新硬盘信息
procedure RefreshDriveInfo;
var
  fTotal,fFree,fUse,f:double;
  i:integer;
  sFree,sTotal,sUse,s:string;

  fso:variant;
  d:variant;
begin
  if UseFSO then
  try
   	fso:=createoleobject('scripting.filesystemobject');
   	d:=fso.getdrive(aDriveName);
  except
    UseFSO:=false;
  end;
  if not UseFSO then
  begin
    BeginUpdate;
    lblDrive.caption:=format('(%s:)',[aDriveName]);
    fTotal:=0.0; fFree:=0.0;
    GetDiskSizeAvail(aDriveName+':\',fTotal,fFree);
    fUse:=fTotal-fFree;
    EndUpdate;
  end;
  
  if UseFSO then
  begin
    //imgNotReady.visible:=not d.isReady;
    if not d.isReady then
    begin
      BeginUpdate;
      imgbg.width:=0;
      lblDrive.caption:=format('(%s:)',[aDriveName]);
      lblInfo.caption:='未准备好';//'Not Ready';
      EndUpdate;
      exit;
    end;
    i:=d.DriveType;
    if (aTotal=d.TOtalSize) and (aFree=d.FreeSpace) then exit;
    try
      fFree:=d.FreeSpace;
      fTotal:=d.totalSize;
      fUse:=d.totalSize-d.FreeSpace;
    finally
    end;
  end;
  aTotal:=fTotal;
  aFree:=fFree;
	Beginupdate;
  //计算剩余容量
  i:=0;
  fFree:=fFree/1024;
  while fFree>1024 do
  begin
    inc(i);
    fFree:=fFree/1024;
  end;
  case i of
    0: sFree:='KB';
    1: sFree:='MB';
    2: sFree:='GB';
    3: sFree:='TB';
  else
    sFree:='MB';
  end;
  //计算使用容量
  i:=0;
  fUse:=fUse/1024;
  while fUse>1024 do
  begin
    inc(i);
    fUse:=fUse/1024;
  end;
  case i of
    0: sUse:='KB';
    1: sUse:='MB';
    2: sUse:='GB';
    3: sUse:='TB';
  else
    sUse:='MB';
  end;
  //计算总容量
  i:=0;
  fTotal:=fTotal/1024;
  while fTotal>1024 do
  begin
    inc(i);
    fTotal:=fTotal/1024;
  end;
  case i of
    0: sTotal:='KB';
    1: sTotal:='MB';
    2: sTotal:='GB';
    3: sTotal:='TB';
  else
    sTotal:='MB';
  end;
 	
 	if aTotal>0 then
    i:=100-round(aFree/aTotal*100)
  else i:=0;
  imgpb.width:=round(i*110/100);
  if i>90 then imgpb.FileName:=WidgetPath+'images\red.png'
  else if i>80 then imgpb.FileName:=WidgetPath+'images\roux.png'
  else if i>50 then imgpb.FileName:=WidgetPath+'images\green.png'
  else imgpb.FileName:=WidgetPath+'images\blue.png';
  if UseFSO then
  begin  
    if (d.DriveType=2) and (d.VolumeName='') then
      lblDrive.caption:=format('本地磁盘(%s:)',[aDriveName])
    else
      lblDrive.caption:=format('%s (%s:)',[d.VolumeName,aDriveName]);
  end;
  if (sUse='KB') or (sUse='MB') then
 	  lblInfo.caption:=format('Use : %.1f %s / %.1f %s',[fUse,sUse,fTotal,sTotal])
 	else
 	  lblInfo.caption:=format('Use : %f %s / %f %s',[fUse,sUse,fTotal,sTotal]);
 	if fFree=0 then  
 	  lblPercent.caption:=format('Free: 0 %s / %d',[sFree,100-i])+'%'
 	else
 	  lblPercent.caption:=format('Free: %f %s / %d',[fFree,sFree,100-i])+'%';
  imgbg.hint:=lblDrive.caption+#13#10+'可用：'+format('%f%s (%d',[fFree,sFree,100-i])+'%)'+#13#10+format('总容量：%f%s',[fTotal,sTotal]);
  lblDrive.hint:=imgbg.hint;
  lblInfo.hint:=imgbg.hint;
  lblPercent.hint:=imgbg.hint;
  imgpb.hint:=imgbg.hint;
  Endupdate;
end;

//读取当前Widget的INI设置
procedure InitIni;
var
  s:string;
begin
  aDriveName:=GetSetting('DriveName','C');
  bgIndex:=GetSetting('bgIndex',1);
  if (bgIndex<=0) or (bgIndex>bgCount) then bgIndex:=1;
	imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
	UseFSO:=GetSettingInt('UseFSO',1);
end;

procedure OnWidgetInit;
begin
  BeginUpdate;
  InitIni;
  RefreshDriveInfo;
  if Docking then
    Widget.Width:=156;
  EndUpdate;
  tmr:=TTimer.Create(Widget);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=30000;
  tmr.Enabled:=true;
end;

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

procedure OntmrTimer(sender)
begin
  RefreshDriveInfo;
end;

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

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