uses
  uDisk;

const
  MAX_COUNT=15;

var
  tmr:TTimer;
  DriveNames:string;
  lblInfos,imgpbbgs,imgpbs:TStrings;
  
//取指定盘符的信息，并赋值到Label和Image进度条上
procedure GetDiskInfo(aDriveName:string; lblInfo:TxspLabel; imgpb:TxspImage);
var
  isReady:boolean;
  aTotal,aFree:Integer;
  aPer:integer;
  aVolume:string;
  iType,
  i:integer;
  sUse,sTotal,sDrive,s,sType,sdriveFormat:string;
  fUse,fTotal:Real;
  sfree:string;
  ffree:Real;
begin
  if not Assigned(lblInfo) or not Assigned(imgpb) then exit;
  aFree:=0; aTotal:=0; aPer:=0;
  aVolume:=''; iType:=0;
  sUse:=''; sFree:=''; sTotal:='';
  System.Shell.GetDriveInfo(aDriveName,isReady,aVolume,aFree,aTotal,aPer,iType,sdriveFormat);

	Beginupdate;
  //计算剩余容量
  sFree:=GetMSize(aFree);
  //计算已用数量
  sUse:=GetMSize(aTotal-aFree);
  //计算总容量
  sTotal:=GetMSize(aTotal);

 	if aTotal=0 then
 	  imgpb.width:=0
 	else
    imgpb.width:=round(aPer*110/100);
  i:=aPer;
  
  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 i>80 then imgpb.FileName:=WidgetPath+'images\bar3.png'
  else if i>50 then imgpb.FileName:=WidgetPath+'images\bar2.png'
  else imgpb.FileName:=WidgetPath+'images\bar1.png';
  if (aVolume='') and (iType=3) then
    sDrive:=format('本地磁盘 (%s:)',[aDriveName])
  else
    sDrive:=format('%s (%s:)',[aVolume,aDriveName]);

  lblInfo.caption:=format('%s: %s / %s',[aDriveName,sUse,sTotal]);

  //类型 0:无法识别 1:Floppy 2:Removable 3:Fixed 4:Remote (network) 5:CD-ROM 6:RAM
  sType:=DiskTypeToStr(iType);
  if sType='本地磁盘' then sType:='';
  
  imgpb.hint:=sType+sDrive+#13#10+format('已用：%s (%d%s)'#13#10,[sUse,aPer,'%'])+
    format('可用：%s (%s%s)'#13#10'总容量：%s'#13#10'文件系统：%s',[sfree,IntToStr(100-aPer),'%',sTotal,sdriveFormat]);
  lblInfo.hint:=imgpb.hint;
  imgpb.visible:=true;
  lblInfo.visible:=true;
  Endupdate;
end;

//刷新硬盘信息
procedure RefreshDiskInfo;
var
  i,ilen:integer;
  s:string;
begin
  if DriveNames='' then exit;
  BeginUpdate;
  ilen:=length(DriveNames);
  for i:=0 to ilen-1 do
  try
    s:=DriveNames[i+1];
    GetDiskInfo(s,lblInfos.Objects[i],imgpbs.Objects[i]);
  except
    
  end;
  if ilen>1 then
    Widget.Height:=67+21*(ilen-2)
  else
  begin
    Widget.Height:=57;
  end;
  imgbg.Height:=Widget.Height;
  EndUpdateAll;
end;

//文字点击事件
procedure lblInfoClick(Sender);
begin
  try
    OpenDrive(DriveNames[sender.tag+1]);
  except
  
  end;
end;

//新建一个Label和Image
procedure NewLabel(index:integer);
var
  lbl:TxspLabel;
  img:TxspImage;
begin
  if lblInfos.Count<=index then
  begin
    lbl:=TxspLabel.Create(Widget);
    lbl.parent:=Widget;
    //文字的的位置
    lbl.left:=8;
    lbl.top:=14+index*21;
    //文字的高/宽
    lbl.height:=18;
    lbl.width:=108;
    lbl.tag:=index;
    //字体名称
    lbl.FontStyle.Font.Name:='Arial';
    //字体大小
    lbl.FontStyle.Font.Size:=7;
    //字体颜色
    lbl.FontStyle.Font.Color:=$C19D82; //#829dc1;
    //是否粗体
   	//lbl.FontStyle.Font.Style:=lbl.FontStyle.Font.Style or 1;
    lbl.OnClick:='lblInfoClick'
    lbl.Cursor:=-21; //crHandPoint
    lblInfos.AddObject('',lbl);
  end;
  if imgpbbgs.Count<=index then
  begin
    img:=TxspImage.Create(Widget);
    img.parent:=Widget;
    //进度条背景的位置
    img.left:=8;
    img.top:=24+index*21;
    img.width:=108;
    img.height:=7;
    img.FileName:=WidgetPath+'images\bar0.png';
    imgpbbgs.AddObject('',img);
    SetAllowMoveWidget(img,true);
  end;
  if imgpbs.Count<=index then
  begin
    img:=TxspImage.Create(Widget);
    img.parent:=Widget;
    //进度条的位置
    img.left:=8;
    img.top:=24+index*21;
    img.width:=1;
    img.height:=7;
    img.FileName:=WidgetPath+'images\bar1.png';
    imgpbs.AddObject('',img);
    SetAllowMoveWidget(img,true);
  end;
end;

procedure OntmrTimer(sender)
begin
  RefreshDiskInfo;
end;

//--- Widget初始化事件 ---
procedure OnWidgetInit;
var
  i:integer;
begin
  lblInfos:=TStringList.Create;
  imgpbbgs:=TStringList.Create;
  imgpbs:=TStringList.Create;
  
  DriveNames:=UpperCase(GetSetting('DriveNames',GetValidDrives));
  if (length(DriveNames)>0) and (DriveNames[0]='A') then
    DriveNames:=copy(DriveNames,2,length(DriveNames)-1);
  if length(DriveNames)>MAX_COUNT then DriveNames:=copy(DriveNames,1,MAX_COUNT);
  BeginUpdate;
  if length(DriveNames)>0 then
    for i:=0 to length(DriveNames)-1 do
      NewLabel(i);
  
  EndUpdate;
  RefreshSidebarOrder;
  tmr:=TTimer.Create(Widget);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=30000;
  tmr.Enabled:=true;
  RefreshDiskInfo;
end;

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

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

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