var
  aSkinName:string;

procedure Menu_AddChild(aParentMenu:TMenuItem; aCaption:stirng; aFunctionName:string; aTag:integer);
var
  mi:TMenuItem;
begin
  if aParentMenu=nil then exit;
  mi:=TMenuItem.Create(Widget);
  mi.caption:=aCaption;
  mi.OnClick:=aFunctionName;
  mi.RadioItem:=true;
  mi.Tag:=aTag;
  if aTag=0 then
    mi.Checked:=true;
  aParentMenu.add(mi);
  if aCaption=aSkinName then
    mi.Checked:=true;
end;

//按比例缩放图片
procedure SetImageWidth(value:integer);
var
  r:Real;
begin
  r:=value/imgbg.ActualWidth;
  BeginUpdate;
  imgbg.width:=value;
  imgbg.height:=imgbg.ActualHeight*r;
  widget.width:=imgbg.width;
  widget.height:=imgbg.height;
  EndUpdate;
end;

procedure OnSkinClick(sender:TObject);
begin
  TMenuItem(sender).Checked:=true;
  BeginUpdate;
  SetSetting('Skin',TMenuItem(sender).caption);
  imgbg.FileName:=WidgetPath+'images\'+TMenuItem(sender).caption+'.png';
  imgbg.width:=imgbg.ActualWidth;
  imgbg.height:=imgbg.ActualHeight;
  widget.width:=imgbg.width;
  widget.height:=imgbg.height;
	SetSettingInt('ImageWidth',imgbg.width );
  EndUpdate;
end;

procedure InitMenu;
var
  lst:TStrings;
  i:integer;
begin
//procedure Menu_AddChild(aParentMenu:TMenuItem; aCaption:stirng; aFunctionName:string; aTag:integer);
  lst:=TStringList.create;
  lst.text:=GetFileList(WidgetPath+'images','*.png',false,false,false);
  if lst.count>0 then
    for i:=0 to lst.count-1 do
      Menu_AddChild(nSkins,copy(lst.strings[i],1,length(lst.strings[i])-4),'OnSkinClick',i);
  //showmsg(lst.text);
  lst.free;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
var
  s:string;
  i:integer;
begin
  aSkinName:=GetSetting('Skin','');
  if aSkinName<>'' then
  begin
    imgbg.FileName:=WidgetPath+'images\'+aSkinName+'.png';
  	i:=GetSetting('ImageWidth',imgbg.ActualWidth);
   	SetImageWidth(i);
  end;
  InitMenu;
end;

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

procedure OnWidgetMouseWheel(WheelDelta);
begin
  if WheelDelta>0 then
  begin
		if imgbg.width >=300 then exit;
		SetImageWidth(imgbg.width+3);
  end
  else
  begin
		if imgbg.width <= 64 then exit;
		SetImageWidth(imgbg.width-3);
  end;
	SetSettingInt('ImageWidth',imgbg.width );
end;