procedure btnSelectFolderClicked;  //选择目录点击事件
var
	sPathName:string;
begin
	sPathName:='';
	if SelectDirectory('浏览文件夹',sPathName) then
	edtFolder.text:=sPathName;
end;

procedure SetInitWindow(i:integer);  //根据实际内容调整窗口大小
begin{还需要刷新整个窗口使其重绘}
	BeginUpdate;	
	case i of 
	0:begin
	  lblChangeIcon.top:=202;
		btnChangeIcon.Top:=196;
		lblSetImageSize.Top:=202;
		edtIconSize.Top:=196;

		chkLblVisible.top:= 220;
		self.height:=312;
		end;
	1:begin
	  lblChangeIcon.top:=230;
		btnChangeIcon.Top:=226;
		lblSetImageSize.Top:=230;
		edtIconSize.Top:=226;
		chkLblVisible.top:= 250;

		self.height:=340;
		end;
	else begin
	  lblChangeIcon.Top:=140;
		btnChangeIcon.Top:=137;
		lblSetImageSize.Top:=140;
		edtIconSize.Top:=137;
		chkLblVisible.top:= 162;

		self.height:=251;	
		end;
	end;

	EndUpdateAll;
end;

procedure btnSelectFileClicked;
var 
	OpenDialog:TOpenDialog;
begin
	try
		OpenDialog:=TOpenDialog.Create(nil);
		if OpenDialog.Execute then
			edtFile.text:=OpenDialog.filename;
	finally
		OpenDialog.free;
	end;
end;
procedure HideAll;			//隐藏选择文件或选择目录的组件
begin
	BeginUpdate;
	lblFolderSel.visible:=False;
	btnSelectFolder.visible:=False;
	edtFolder.visible:=False;
	lblSelectFile.visible:=False;
	btnSelectFile.visible:=False;
	edtFile.visible:=False;
	lblParaMeter.visible:=False;
	edtParaMeter.visible:=False;
	EndUpdate;
end;
procedure ShowSelectFolder; //显示选择目录的组件
begin
  BeginUpdate;
	HideAll;
	lblFolderSel.visible:=True;
	btnSelectFolder.visible:=True;
	edtFolder.visible:=True;
	EndUpdate;
end;

procedure btnChangeIconClicked;//切换Icon的事件
var OpenDialog:TOpenDialog;
begin
	BeginUpdate;
	try
		OpenDialog:=TOpenDialog.create(nil);
		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 CopyFile(OpenDialog.filename,AppPath+'Data\'+WidgetName+'.png',False) then
				SetSetting('IconFileName',AppPath+'Data\'+WidgetName+'.png');
		end;
	finally
		OpenDialog.free;
	end;
	EndUpdate;
end;

Procedure ShowSelectFile;//显示选择文件的组件
begin
	BeginUpDate;
	HideAll;
	lblSelectFile.visible:=True;
	btnSelectFile.visible:=True;
	edtFile.visible:=True;
	lblParaMeter.visible:=True;
	edtParaMeter.visible:=True;
	EndUpDate;
end;

procedure cbbSelectActionSelected(sender);  //打开动作事件Combox变化事件
begin
	case cbbSelectAction.ItemIndex of
		0:	
		begin
			SetInitWindow(cbbSelectAction.ItemIndex);
			ShowSelectFolder;
		end;
		1:
		begin
			SetInitWindow(cbbSelectAction.ItemIndex);
			ShowSelectFile;
		end;
		else
		begin
			HideAll;
			SetInitWindow(cbbSelectAction.ItemIndex);
		end;
	end;
end;

procedure InitActionList; //初始化Combox选项
var
	lstAction:TString;
	i:Integer;
begin
	Try
		lstAction:=TStringList.Create;
		lstAction.LoadFromFile(AppPath+'widgets\桌面图标\ActionList.dat');
		cbbSelectAction.Items.clear;
		for i:=0 to lstAction.count-1 do
		begin
			cbbSelectAction.Items.add(lstAction.strings[i]);
		end;
	finally
		lstAction.Free;
	end;
end;





Function CheckIconSize:Boolean;
var
  iSize:integer;
begin
	result:=True;
	try
		iSize:=StrToInt(edtIconSize.text);
		if iSize<0 then raise('');
		if (iSize<32) Or (iSize>256) then
		begin
		  ShowMsg('图标大小数值不合法，可用范围为32-256');
			edtIconSize.text:='128';
			result:=False;
		end;
	except
		ShowMsg('图标大小数值不合法，可用范围为32-256');
		edtIconSize.text:='128';
		result:=False;	
	end;
end;

procedure InitIni;
begin
  if GetSettingInt('Lblvisible',1) = 0 then 
	 chkLblVisible.checked:=true
	else
	 chkLblVisible.checked:=false;	 
	cbbSelectAction.ItemIndex:=GetSettingInt('CbbSelectItemIndex',2);//用户选择动作项
	edtFolder.text:=GetSetting('FolderPath','C:\');       //指定的目录
	edtFile.text:=GetSetting('FileName','');          //指定的文件
	edtParaMeter.text:=GetSetting('ParaMeter',''); 
	edtIconSize.text:=IntToStr(GetSetting('IconSize',128));
	edtCaption.text:=GetSetting('IconCaption','我的电脑'); 
	cbbSelectAction.OnSelect:='CmbSelected';
end;

procedure SaveIni;
begin
	
	if  chkLblVisible.checked then 	
	begin
		SetSettingInt('Lblvisible',0);
	end else
	begin
	  SetSettingInt('Lblvisible',1);
	end;
	SetSettingInt('IconSize',StrToInt(edtIconSize.text));					//图标大小
	SetSettingInt('CbbSelectItemIndex',cbbSelectAction.ItemIndex);//用户选择动作项
	SetSetting('FolderPath',edtFolder.text);       //指定的目录
	SetSetting('FileName',edtFile.text);          //指定的文件
	SetSetting('ParaMeter',edtParaMeter.text);    //附加参数	
  if trim(edtCaption.text)<>'' then 	SetSetting('IconCaption',edtCaption.text);    //图标标题


end;

Procedure OnSettingApply;
begin 
	if not CheckIconSize then exit;	
	SaveIni;
end;

procedure OnSettingInit;
begin
	InitActionList;
	cbbSelectAction.onSelect:='cbbSelectActionSelected';
	{读取用户设置的信息}
	InitIni;
	cbbSelectActionSelected(nil);
end;

//--- 按下设置窗口的确定按钮时，触发此事件 ---
//通过设置返回值来改变是否允许确认,当result为False时不关闭设置窗口
function OnSettingClose(iResult);
begin
	{保存用户设置的信息}
	if  CheckIconSize then 
	begin
		SaveIni;
		Result:=True;
	end else
	begin
		Result:=False;
	end;
end;


procedure CmbSelected(Sender: TObject);
var
  index:integer;
	CmbItemString:string;
	begindex,Endindex;
begin 
  index:= cbbSelectAction.ItemIndex;
  if index<0 then exit;
  case index of
	   	0:	//打开文件夹/盘符
			edtCaption.text:='打开指定目录';
		  1:  //打开文件或运行程序
				edtCaption.text:='打开文件/程序';
	  else begin 
		 CmbItemString:=cbbSelectAction.items.strings[index];
		
		begindex:=Pos('“',CmbItemString);
		Endindex:=Pos('”',CmbItemString);
		if (begindex<0) or (Endindex<0) then exit; 
		CmbItemString:=Copy(CmbItemString,begindex +2,Endindex -begindex -2);
		edtCaption.text:=CmbItemString;
		end;

  end;  
end;


