var
  tmr:TTimer;
  Index:integer;
  RecycleBinSize:Extended;
  RecycleBinCount:integer;
  isCustomFile:boolean;
  CustomFile1,CustomFile2:string;

  miEmpty,miOpen,miInterval,miStopRefresh:TMenuItem;

procedure OpenRecyclBin;
begin
  ShellExecute(0,'open','::{645FF040-5081-101B-9F08-00AA002F954E}','','',1);
  
//我的电脑{20D04FE0-3AEA-1069-A2D8-08002B30309D} 
//网上邻居{208D2C60-3AEA-1069-A2D7-08002B30309D} 
//回收站{645FF040-5081-101B-9F08-00AA002F984E} 
//我的文档{450D8FBA-AD25-11D0-98A8-0800361B1103} 
//Internet Explorer{871C5380-42A0-1069-A2EA-08002B30309D} 
//控制面板{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D} 
//拨号网络/网络连接{992CFFA0-F557-101A-88EC-00DD010CCC48} 
//网络（和拨号）连接{7007ACC7-3202-11D1-AAD2-00805FC1270E}（WIN2000/XP） 
//打印机/打印机和传真{2227A280-3AEA-1069-A2DE-08002B30309D} 
//计划任务{D6277990-4C6A-11CF-8D87-00AA0060F5BF} 
end;

procedure RefreshRecycleBin;
var
  f:Extended;
begin
  f:=0.1;
  if GetRecycleBinProperty(f,RecycleBinCount) then
  begin
    //if RecycleBinSize=f then exit;
	  xspfrm.beginupdate;
	  RecycleBinSize:=f;
    f:=f/1024;
    if f<1000 then
  		lblInfo.caption:=format('%d个对象(%fKB)',[RecycleBinCount,round(f*100)/100])
    else 
    begin
    	f:=f/1024;
	    if f<1000 then
	    	lblInfo.caption:=format('%d个对象(%fMB)',[RecycleBinCount,round(f*100)/100])
    	else
	    	lblInfo.caption:=format('%d个对象(%fGB)',[RecycleBinCount,round(f*100)/102400])
    end;
    if RecycleBinSize*1024=f then 
        lblInfo.caption:=format('%d个对象(小于0.1KB)',[RecycleBinCount]);
    if RecycleBinCount>0 then
    begin
	    imgbg.FileName:=CustomFile2;
	    if not miEmpty.Enabled then miEmpty.Enabled:=true;
	  end 
	  else
	  begin
      lblInfo.caption:=format('%d个对象(空)',[RecycleBinCount]);
      imgbg.FileName:=CustomFile1;
	    if  miEmpty.Enabled then miEmpty.Enabled:=false;
	  end;
    //showBalloonHint('Warning','Refresh RecycleBin');  
	  xspfrm.endupdate;
  end;
end;

procedure miEmptyOnClick(sender);
begin
  //MB_ICONWARNING+MB_YESNO+MB_DEFBUTTON2=IDYesr
  if MessageBox(0,format('您确认要清空回收站中的%d个项目吗？',[RecycleBinCount]),'警告',$00000134)=6 then
  begin
    EmptyRecycleBin;
    RefreshRecycleBin;
  end;
end;

procedure miOpenOnClick(sender);
begin
  OpenRecyclBin;
end;

procedure miStopRefreshOnClick(sender);
begin
  if tmr.Enabled then
  begin
    miStopRefresh.Caption:='开始刷新';
  end
  else
  begin
    miStopRefresh.Caption:='停止刷新';
  end;
  tmr.Enabled:=not tmr.Enabled;
end;

procedure InitMenu;
begin
  miInterval:=TMenuItem.Create(Widget);
  miInterval.Caption:='-';
  pm.Items.Insert(0,miInterval);

  miStopRefresh:=TMenuItem.Create(Widget);
  miStopRefresh.Caption:='停止刷新';
  miStopRefresh.onclick:='miStopRefreshOnClick';
  pm.Items.Insert(0,miStopRefresh);
 
  miEmpty:=TMenuItem.Create(Widget);
  miEmpty.Caption:='清空回收站';
  miEmpty.onclick:='miEmptyOnClick';
  pm.Items.Insert(0,miEmpty);
 
  miOpen:=TMenuItem.Create(Widget);
  miOpen.Caption:='打开';
  miOpen.onclick:='miOpenOnClick';
  miOpen.default:=true;
  pm.Items.Insert(0,miOpen);
end;

//读取当前Widget的INI设置
procedure InitIni;
begin
  Index:=GetSetting('Index',0);
  isCustomFile:=GetSettingInt('CustomFile',0)=1;
  if isCustomFile then
  begin
    CustomFile1:=GetSetting('CustomFile1','');
    CustomFile2:=GetSetting('CustomFile2','');
  end
  else
  begin
    CustomFile1:=WidgetPath+'images\'+inttostr(Index)+'.png';
    CustomFile2:=WidgetPath+'images\'+inttostr(Index)+'f.png';
  end;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  Beginupdate;
  InitIni;
  InitMenu;
  RefreshRecycleBin;
  EndUpdate;
  
  tmr:=TTimer.Create(Widget);
  tmr.OnTimer:='OntmrTimer';
  tmr.Interval:=10000;
  tmr.Enabled:=true;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
  miInterval.Free;
  miStopRefresh.free;
  miEmpty.Free;
  miOpen.free;
end;

procedure OntmrTimer(sender)
begin
  RefreshRecycleBin;
end;

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

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

//--- 文件拖放到事件，当文件拖放到Widget中时会触发此事件 ---
procedure OnWidgetDropFiles(Files:string);
var
  lst:TStrings;
  i:integer;
begin
  if Files<>'' then
  begin
    lst:=TStringList.create;
    try
      lst.text:=Files;
      if lst.count>0 then
        for i:=0 to lst.count-1 do
        begin
          DeleteFileToRecyclebin(lst.strings[i]);
        end;
        RefreshRecycleBin;
    finally
      lst.free;
    end;
  end;
//  showtrayhint('file',files);
end;