var
  tmr:TTimer;
  
  
procedure RefreshStatus;
var
  b:boolean;
  ACLineStatus:Byte;
  BatteryFlag:Byte;
  BatteryLifePercent:Byte; 
  Reserved1:Byte; 
  BatteryLifeTime:DWORD; 
  BatteryFullLifeTime:DWORD;
  s:string;
begin   
  if GetSystemPowerState(ACLineStatus,BatteryFlag,BatteryLifePercent,Reserved1,BatteryLifeTime,BatteryFullLifeTime) then
  begin 
//BatteryFlag:=1; 
//BatteryLifePercent:=89; 
//ACLineStatus:=0;
  
    //是否在充电
    case ACLineStatus of   
      0:  imgeOnOff.FileName:=WidgetPath+'images\charging_off.png'; // Offline;   
      1:  imgeOnOff.FileName:=WidgetPath+'images\charging_on.png';  //  Online;   
      else//255
        imgeOnOff.FileName:=WidgetPath+'images\charging_off.png'; // Unknown;   
    end;
    //是否有电池
    imgNoBattery.visible:=(BatteryFlag=128) or (BatteryFlag=255);
    //电量及提示
    if imgNoBattery.visible then
    begin
      imgbg.hint:='没有安装电池！';
      lblPercent.caption:='';
      shpbg.Color:=$999999;
    end
    else
    begin
      //电量百分比
      if BatteryLifePercent>50 then shpbg.Color:=$00CA56
        else if BatteryLifePercent>20 then shpbg.Color:=$00CCFF
          else shpbg.Color:=$003CFF
      lblPercent.caption:=IntToStr(BatteryLifePercent)+'%';
      case ACLineStatus of   
        0:  s:='放电中...'; // Offline;   
        1:  
            if BatteryLifePercent=100 then
              s:='使用交流电源'
            else
              s:='正在充电...';  //  Online;   
        255:s:='未知状态';
        else
          s:=''
      end;
      if BatteryLifeTime>0 then
        imgbg.hint:=format('电池剩余时间:%d(%s) %s',[BatteryLifeTime div 60,lblPercent.caption,s])
      else
        imgbg.hint:=format('电池剩余:%s(%s)',[lblPercent.caption,s]);
      imgfull.width:=round(59*BatteryLifePercent/100);
    end;
    shpbg.Colorto:=shpbg.Color;
    shpbg.BorderColor:=shpbg.Color;
    imgfull.visible:=not imgNoBattery.visible;
    imgEmpty.visible:=imgfull.visible;
  end
  else b:=false;
end;

//读取当前Widget的INI设置
procedure InitIni;
begin

end;

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

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

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  BeginUpdate;
  InitIni;
  RefreshStatus;
  if Docking then 
  	OnWidgetDock;
  EndUpdate;
  tmr:=TTimer.Create(Widget);
  tmr.Interval:=30000;//30秒刷新一次
  tmr.OnTimer:='OntmrTimer';
end;

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

procedure OntmrTimer(sender)
begin
  RefreshStatus;
end;
