const
  bgCount=34;
  
var
  tmr:TTimer;
  NetDevice:string;
  TotalSpeed:integer;
  bgIndex:integer;
  TotalMem:integer;

procedure Changebg;
begin
  bgIndex:=(bgIndex mod bgCount)+1;
  BeginUpdate;
  imgbg.FileName:=WidgetPath+format('images\bg%d.png',[bgIndex]);
  EndUpdate;
  SetSetting('bgIndex',bgIndex);
end;

function GetPercentName(iPercent:integer):string;
begin
  if iPercent>90 then
    result:='red'
  else if iPercent>80 then
    result:='roux'
  else
    result:='blue';
  result:=WidgetPath+'images\'+result+'.png';  
end;

function GetSize(value:Int64):string;
var
  i:integer;
  f:double;
  s:string;
begin
  //计算剩余容量
  i:=0;
  f:=value/1024;
  while f>1024 do
  begin
    inc(i);
    f:=f/1024;
  end;
  case i of
    0: s:='KB';
    1: s:='MB';
    2: s:='GB';
    3: s:='TB';
  else
    s:='MB';
  end;
  result:=format('%.1f %s',[f,s]);
end;

procedure OntmrTimer(sender)
var
  iup,idown,i:integer;
  FreeMem:Int64;
begin
  BeginUpdate;
  //cpu
  i:=system.Machine.cpus.usagePercentage;

  lblcpu.caption:='CPU:'+inttostr(i)+'%';
  imgpb0.width:=round(i/2);
  imgpb0.FileName:=GetPercentName(i);
  
  //mem info
 // GetMemoryInfo(aFree,aTotal,i);
  if TotalMem> 0  then 
	begin
	  FreeMem:=system.Machine.availableMemory;
	  i:=round((TotalMem-FreeMem)*100/TotalMem); 
	  lblmem.caption:='MEM:'+inttostr(i)+'%';
  	imgpb1.width:= round(i/2);
    imgpb1.hint:=format('可用:%d MB(%d%s)'#13#10'总内存:%d MB'#13#10#13#10'(双击打开任务管理器)',[FreeMem,100-i,'%',TotalMem]);
	end;
  
  //net
  iup:=0; idown:=0;
  idown:=System.Machine.Network.InPerSec;
  iup:=System.Machine.Network.OutPerSec;
  if (TotalSpeed<idown) and (idown<2048000) then
    TotalSpeed:=idown;
  //up
  i:=round(iup/TotalSpeed*50);
  if i>50 then i:=50
    else if i<0 then i:=0;
  imgpb2.width:=i;
  imgpb2.FileName:=GetPercentName(imgpb3.width*2);
  //down
  i:=round(idown/TotalSpeed*50);
  if i>50 then i:=50
    else if i<0 then i:=0;
  imgpb3.width:=i;
  imgpb3.FileName:=GetPercentName(imgpb3.width*2);

  if (iup>=1024) or (iup=0) then
    lblup.caption:=format('Up:%d k/s',[round(iup/1024)])
  else
    lblup.caption:=format('Up:%1.f k/s',[iup/1024]);
    
  if (idown>=1024) or (idown=0) then
    lbldown.caption:='Down:'+inttostr(idown/1024)+' k/s'
  else
    lbldown.caption:=format('Down:%1.f k/s',[idown/1024]);

  //下载总量
  with System.Machine.Network do
  begin
    lbldown.hint:=format('--- 下载 ---'#13#10'累计下载 : %s'#13#10'平均下载速度: %s/每秒'#13#10'最高下载速度: %s/每秒',
        [GetSize(ActiveAdapter.InTotal),GetSize(ActiveAdapter.AverageInPerSec), GetSize(ActiveAdapter.PeakInPerSec)]);
    lblup.hint:=format('--- 上传 ---'#13#10'累计上传 :%s'#13#10'平均上传速度: %s/每秒'#13#10'最高上传速度: %s/每秒',
        [GetSize(ActiveAdapter.OutTotal),  GetSize(ActiveAdapter.AverageOutPerSec),GetSize(ActiveAdapter.PeakOutPerSec)]);
    //chartNetDown.hint:=format('IP    地址 : %s'#13#10'网卡地址 : %s'#13#10'%s'#13#10'%s',
    //    [IP,MacAddress,lbldown.hint,lblup.hint]);
  end;
  EndUpdate;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  lst:TStrings;
begin
  NetDevice:=GetSetting('NetDevice','');
  bgIndex:=GetSetting('bgIndex',1);

  with System.Machine.Network do
  if NetDevice='' then
  begin
    if AvailableAdapterList.count>1 then
    begin
      NetDevice:=ActiveAdapterName;
      showtrayhint('警告','您的电脑有多个网络设备，如果默认的没有网速，请到小工具设置窗口选择你使用的网络设备！');
      SetSetting('NetDevice',NetDevice);
    end;
  end
  else
    System.Machine.Network.ActiveAdapterName:=NetDevice;
  BeginUpdate;
  TotalSpeed:=GetSetting('TotalSpeed',102400);
  imgbg.FileName:=WidgetPath+'images\bg'+inttostr(bgIndex)+'.png';
  EndUpdate;
  System.Machine.Network.Active:=true;
	
	TotalMem:=system.Machine.totalMemory; 
  system.Machine.Cpus.Active:=true;
	
  tmr:=TTimer.Create(Widget);
  tmr.Interval:=1000;
  tmr.OnTimer:='OntmrTimer';
  AutoAdjustMem:=0;
 
end;

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

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

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
begin
	system.Machine.Cpus.Active:=false;
	System.Machine.Network.Active:=false;
end;
