var
	g_iActionIndex:Integer;  //用户指定的动作Index
	g_iBtnimgMenuIndex:Integer; //按钮背景图片菜单Index
	g_FolderPathStr,g_FilePathStr,g_ParaMeterStr:string;  //获取的目录地址，文件地址，运行参数
  g_sControlPanelGuid:String;
	g_lstMenuItems:TString;
	g_lstBtnImgMenuItems:TString;
	g_lstBtnImgFiles:TStrings;
	
Procedure ChangeBtnImgClicked(sender);
begin
	BeginUpdate;
		TMenuItem(g_lstBtnImgMenuItems.Objects[g_iBtnimgMenuIndex]).checked:=False;
		btnMain.filename:=g_lstBtnImgFiles.strings[sender.tag];
		btnMain.needRefresh:=True;
		TMenuItem(g_lstBtnImgMenuItems.Objects[sender.tag]).checked:=True;
		SetSetting('btnMainImgFilename',g_lstBtnImgFiles.strings[sender.tag]);
		g_iBtnimgMenuIndex:=Sender.tag;
		TMenuItem(g_lstBtnImgMenuItems.Objects[g_iBtnimgMenuIndex]).checked:=True;
	EndUpdate;
end;

procedure ChangeIconClicked(isChangeIcon);  //该过程替换Icon图标/Button背景
var OpenDialog:TOpenDialog;
begin
	try
		OpenDialog:=TOpenDialog.create(widget);
		OpenDialog.InitialDir:=WidgetPath+'MainPicture';
		OpenDialog.filter:='All (*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf;*.png)|*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf;*.png|JPEG Image File (*.jpg)|*.jpg|JPEG Image File (*.jpeg)|*.jpeg|Bitmaps (*.bmp)|*.bmp|Icons (*.ico)|*.ico|Enhanced Metafiles (*.emf)|*.emf|Metafiles (*.wmf)|*.wmf|Png图片|*.png';
		if OpenDialog.execute then
		begin
			//do something
			if isChangeIcon then
			begin
				if CopyFile(OpenDialog.filename,AppPath+'Data\'+WidgetName+'.png',False) then
					begin
           	BeginUpdate;
						imgMain.filename:='';
						SetSetting('IconFileName',AppPath+'Data\'+WidgetName+'.png');
						imgMain.filename:=AppPath+'Data\'+WidgetName+'.png';
           	EndUpdate;
					end;
			end;
		end;
	finally
		OpenDialog.free;
	end;
end;

procedure ChangeIconSize(iIconSize:Integer); //该过程改变图标大小
begin
	BeginUpdate;
	if 	imgMain.width <> iIconSize then
	begin
		imgSelectedBg.width:=iIconSize;
		imgSelectedBg.height:=iIconSize;
		imgMain.width:=iIconSize;
		imgMain.height:=iIconSize;
		widget.width:=iIconSize+12;
		widget.height:=iIconSize+32;	
		if btnMain.width<= imgMain.width then 
		begin
		  btnMain.left:= Round((imgMain.width-btnMain.width)/2) ;
			if iIconSize>=85 then 
			  btnMain.width:=85; 
			else
	    btnMain.width:=imgMain.width	 
		end  else
		begin
		 btnMain.left:=imgMain.left;
		 btnMain.width:=imgMain.width;
		end;
		if imgMain.width<64 then	 
		 begin
		   // btnMain.fontstyle.font.size:=8;	
		 end  else if imgMain.width<=128 then
	   begin
	   // btnMain.fontstyle.font.size:=9;	
	   end  else  if imgMain.width<=256 then
	   begin
	   // btnMain.fontstyle.font.size:=10;	
	   end;		
 end;
 	btnMain.Top:= iIconSize + 3;		
  {
	if (length(btnMain.Caption)>4) and (imgMain.width<=50) then
	begin
	 imgMain.width:=length(btnMain.Caption)*10;
	 widget.width:=btnMain.width+12;
	 btnMain.left:= Round((widget.width-btnMain.width)/2) ;
	end else
	begin
	
	end;
}

 
	EndUpdate;
end;

procedure ChangeBtnCap(index:integer);
var 
	lstTmpStr:TStrings;
	Caption:string;
begin
try
	lstTmpStr:=TStringList.create;
	lstTmpStr.loadFromFile(WidgetPath+'ActionList.dat');
	if index<2 then
	begin
		btnMain.hint:=lstTmpStr.strings[index];
		case index of 
			0:btnMain.Caption:='打开指定目录';
			1:btnMain.Caption:='打开文件/程序';
		end;
     Caption:=btnMain.Caption;
		 SetSetting('IconCaption',Caption);
		 	ChangeIconSize(imgMain.width);
		 exit;
	end; 
 Caption:=Copy(lstTmpStr.strings[index],Pos('“',lstTmpStr.strings[index])+2,Pos('”',lstTmpStr.strings[index])-Pos('“',lstTmpStr.strings[index])-2);
 SetSetting('IconCaption',Caption);
 btnMain.Caption:=Caption;
	btnMain.hint:=lstTmpStr.strings[index];
	finally
		lstTmpStr.free;
	end;
	ChangeIconSize(imgMain.width);
end;

procedure OpenWindow(spath:String); //通用打开窗体
const
  SW_SHOW=1;
  ID_YES=6;
  MB_YESNO=4;
begin
	 if  DirectoryExists(spath) or (copy(spath,1,7)='http://') then
			ShellExecute(0,'open','Explorer', '"'+spath+'"', '',SW_SHOW)
   else if fileExists(spath) then
	   ShellExecute(0,'open',spath,'','"'+ExtractFilePath(spath)+'"',1)
   else
   begin
     //showtrayhint('其它',spath);
     r := CreateOleObject('wscript.shell');
     r.run(spath);
     r := Unassigned;
   end;
end;

procedure RunAction;  //执行用户指定的操作
const
  SW_SHOW=1;
  ID_YES=6;
  MB_YESNO=4;
begin
	case g_iActionIndex of
		0:	//打开文件夹/盘符
			OpenWindow(g_FolderPathStr);
		1:  //打开文件或运行程序
			begin
				if g_FilePathStr <> '' then
				begin
					if fileExists(g_FilePathStr) then
					 ShellExecute(0,'open', g_FilePathStr,g_ParaMeterStr,'"'+ExtractFilePath(g_FilePathStr)+'"',1)
				end else
					ShowTrayHint('提示','尚未设置文件/程序路径！');
			end;
		2:  //打开我的电脑
			OpenWindow('::{20D04FE0-3AEA-1069-A2D8-08002B30309D}');
		3:  //打开我的文档
			OpenWindow('::{450D8FBA-AD25-11D0-98A8-0800361B1103}');
		4:  //打开回收站
			OpenWindow('::{645FF040-5081-101B-9F08-00AA002F954E}');
		5:  //打开网上邻居
			OpenWindow('::{208D2C60-3AEA-1069-A2D7-08002B30309D}');
		6:  //打开控制面板
		//	OpenWindow(g_sControlPanelGUID);
				ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL','',SW_SHOW);
 // 	7:  //打开收藏夹
	//		OpenWindow($17);
		8:  //打开CMD命令行模式
		 OpenWindow('CMD');
		9:  //打印机和扫描仪{may be error}
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL Sticpl.cpl','',SW_SHOW);
		10: //IE浏览器
			OpenWindow('explorer ::{871C5380-42A0-1069-A2EA-08002B30309D}');
		11: //打开WinPPT
			OpenWindow('powerpnt');
		12: //打开Word     
			OpenWindow('WinWord');
		13: //打开MSExecl
			OpenWindow('excel');
		14: //打开ACCESS
			OpenWindow('msaccess');
		15: //打开添加/删除程序
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL appwiz.cpl','',SW_SHOW);
		16: //打开显示属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL desk.cpl','',SW_SHOW);
		17: //鼠标属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL main.cpl   @0','',SW_SHOW);
		18: //打开Internet属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL inetcpl.cpl','',SW_SHOW);				
		19: //打开用户帐户属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL Nusrmgr.cpl','',SW_SHOW);				
		20: //打开系统属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL Sysdm.cpl','',SW_SHOW);
		21: //打开日期/时间属性  
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL timedate.cpl','',SW_SHOW);
		22: //打开电源管理 属性
			ShellExecute(0,'Open','rundll32.exe','shell32.dll,Control_RunDLL Powercfg.cpl','',SW_SHOW);
		23: //打开拨号属性
			OpenWindow('Rasphone.exe');
		24: //锁定系统                 
			ShellExecute(0,'open','rundll32.exe','user32.dll,LockWorkStation','',SW_SHOW); 	
		25: //打开关机对话框
		 if MessageBox(0,'确认要关机？','警告',MB_YESNO)=ID_YES then
			ShellExecute(0,'open','shutdown','-p','',SW_SHOW);
		26: //重新启动对话框
      if MessageBox(0,'确认要重启？','警告',MB_YESNO)=ID_YES then
        ShellExecute(0,'open','shutdown','-r -f -t 03','',SW_SHOW);
		27: //打开任务管理器
			OpenWindow('TaskMgr.exe');
		28: //查找文件
			begin
        r := CreateOleObject('Shell.Application');
        r.FindFiles;
        r := Unassigned;
      end;
		29: //最小化所有窗体
			begin
        r := CreateOleObject('Shell.Application');
        r.MiniMizeAll;
        r := Unassigned;
      end;
    30: //关闭显示器屏幕
      SendMessage(Widget.Handle, $0112, 61808, 2);
		end;
end;

procedure ActionMenuClicked(sender);//切换动作的菜单按下
begin
	TMenuItem(g_lstMenuItems.Objects[g_iActionIndex]).checked:=False;
	g_iActionIndex:=sender.tag;
	SetSettingInt('CbbSelectItemIndex',g_iActionIndex);
	sender.checked:=True;
	ChangeBtnCap(sender.tag);
end;

Function AddMenuItems(aCaption:string; aTag:Integer;aProcedure:String; aMenu,aMenuLst:TObject):TMenuItem;
begin
	result:=nil;
	result:=TMenuItem.Create(Widget);
	result.tag:=aTag;
	result.caption:=aCaption;
	result.Onclick:=aProcedure; 
	TMenuItem(aMenu).add(result);
	TStringList(aMenuLst).AddObject(inttostr(aTag),result);
end;

procedure InitActionMenuList;  //初始化Action菜单
var
	lstAction:TString;
	i:Integer;
begin
	Try
		lstAction:=TStringList.Create;
		lstAction.LoadFromFile(WidgetPath+'ActionList.dat');
		for i:=0 to lstAction.count-1 do
		begin
			AddMenuItems(lstAction.strings[i],i,'ActionMenuClicked',miChangeAction,g_lstMenuItems);  //创建Menu子菜单
		end;
		TMenuItem(g_lstMenuItems.Objects[g_iActionIndex]).Checked:=True;
	finally
		lstAction.Free;
	end;
end;

procedure InitImgMenuList ; //初始化Button
var 
	i:Integer;
	sImgFileName:string;
begin
	g_lstBtnImgMenuItems.clear;
	g_lstBtnImgFiles.text:=GetFileList(WidgetPath+'ButtonImages','.png',false,false,True);
	for i:=0 to g_lstBtnImgFiles.count-1 do
	begin
		sImgFileName:=g_lstBtnImgFiles.Strings[i];
		sImgFileName:=Copy(sImgFileName,Pos('ButtonImages\',sImgFileName)+13,Pos('.',sImgFileName)-Pos('ButtonImages\',sImgFileName)-13);
		AddMenuItems(sImgFileName,i,'ChangeBtnImgClicked',miChangeBtnImg,g_lstBtnImgMenuItems);  //创建Menu子菜单
	end;
end;

//--- Widget初始化事件，当打开Widget时会触发此事件 ---
procedure OnWidgetInit;
begin
  BeginUpdate;
  //初始化代码，如读取配置信息，初始化一些变量
	imgMain.FileName:=GetSetting('IconFileName',WidgetPath+'drag.png');
  
  
	btnMain.OnDblClick:='RunAction';
	btnMain.Caption:=GetSetting('IconCaption','我的电脑');
  if GetSettingInt('Lblvisible',1) = 0 then btnMain.visible:=false;

	ChangeIconSize(GetSettingInt('IconSize',128));
	{读取用户设置}
	g_iActionIndex:=GetSettingInt('CbbSelectItemIndex',2);
	g_FolderPathStr:=GetSetting('FolderPath','C:\');
	g_FilePathStr:=GetSetting('FileName','');
	g_ParaMeterStr:=GetSetting('ParaMeter',''); 
	if isWinVista then  //不同系统控制面板参数不同
    g_sControlPanelGUID:='::{21EC2020-3AEA-1069-A2DD-08002B30309D}' 
  else
	g_sControlPanelGUID:='::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{21EC2020-3AEA-1069-A2DD-08002B30309D}';

	btnMain.filename:=GetSetting('btnMainImgFilename',WidgetPath+'ButtonImages\AI3.png');
	g_lstMenuItems:=TStringList.Create;  //维护创建的Button的列表
	g_lstBtnImgMenuItems:=TStringList.create;
	g_lstBtnImgFiles:=TStringList.create;
	InitActionMenuList;
	InitImgMenuList;
	//取得选择的按钮图片序列，设置菜单
	g_iBtnimgMenuIndex:=g_lstBtnImgFiles.IndexOf(btnMain.filename);
	if g_iBtnimgMenuIndex=-1 then  //万一用户调用的文件不在默认目录内则取值0
		g_iBtnimgMenuIndex:=0;
	TMenuItem(g_lstBtnImgMenuItems.Objects[g_iBtnimgMenuIndex]).checked:=True;
	EndUpdate;
end;

//--- Widget关闭事件，当关闭Widget时会触发此事件 ---
procedure OnWidgetClose;
var 
	i:integer;
begin
	for i:=g_lstMenuItems.count-1 downto 0 do  //先释放所有菜单选项
		TMenuItem(g_lstMenuItems.Objects[i]).free;
  for i:=g_lstBtnImgMenuItems.count-1 downto 0 do //释放菜单
		TMenuItem(g_lstBtnImgMenuItems.Objects[i]).free;
	g_lstMenuItems.free;
	g_lstBtnImgFiles.free;
	g_lstBtnImgMenuItems.free;
end;

//--- Widget事件，当小工具成为当前活跃窗体时，触发此事件
procedure OnWidgetActivate;
begin
  BeginUpdate;
		imgSelectedBg.visible:=True;
  EndUpdate;
end;

procedure OnWidgetDeactivate;
begin
  BeginUpdate;
		imgSelectedBg.visible:=False;
  EndUpdate;
end;

procedure OnWidgetMouseWheel(WheelDelta);
begin
	BeginUpdate;
  if WheelDelta>0 then
  begin
		if imgMain.width >=256 then 
		begin	   
			 EndUpdateAll;
			 exit;
		end;	
		if imgMain.width + 3 >= 256 then 
		  ChangeIconSize(256)
		else
    ChangeIconSize(imgMain.width + 3);
  end
  else
  begin
		if imgMain.width <= 32 then 
		begin	   
			 EndUpdateAll;
			 exit;
		end;	 
		if imgMain.width - 3 <= 32 then 
		  ChangeIconSize(32)
		else
    ChangeIconSize(imgMain.width - 3);
  end;
	SetSettingInt('IconSize',imgMain.width );					//图标大小
  EndUpdateAll;
end;
procedure imgMainDblClick(Sender:TObject);
begin
  RunAction
end;
procedure miConcealPassedClick(Sender:TObject);
begin
  RunAction
end;
procedure miChangeIconClick(Sender:TObject);
begin
  ChangeIconClicked(True)
end;

//--- 文件拖放到事件，当文件拖放到Widget中时会触发此事件 ---
procedure OnWidgetDropFiles(Files:string);
var
  lst:TStrings;
begin
  if trim(files)='' then exit;
  lst:=TStringList.create;
  try
    lst.text:=files;
    if lst.count>0 then
    begin
      if FileExists(lst.strings[0]) and (LowerCase(ExtractFileExt(lst.strings[0]))='.png') then
      begin
				if CopyFile(lst.strings[0],AppPath+'Data\'+WidgetName+'.png',False) then
				begin
         	BeginUpdate;
					imgMain.filename:='';
					SetSetting('IconFileName',AppPath+'Data\'+WidgetName+'.png');
					imgMain.filename:=AppPath+'Data\'+WidgetName+'.png';
         	EndUpdate;
				end;
      end;
    end;
  finally
    lst.free;
  end;
end;