const
  RowHeight=30;
  RowSep=15;
  RowLeft=4;
var    
  fIconBitmapName:string; 
  ts1,ts2:TTabSheet;
  lblMemoColor,lblTitle,lblTitleFont,lblLabelDays,lblFormWidth,lblFormHeight,lblMemoPlace,lblDaysFont,lblDaysText,lblBottom,lblBottomFont,lblDefaultPNG:TLabel;
  edtTitle,edtTitleFont,edtLabelDays,edtFormWidth,edtFormHeight,edtMemoPlace,edtDaysFont,edtDaysText,edtBottom,edtBottomFont:TEdit;
  cbbLabelType:TComboBox;
  
  pngImage:TPanel;
  pnlMemoColor: TShape;
  img,img1:TImage;
  dlgPic:TOpenPictureDialog;
  BtnTitleFontText:string;
  btnTitleFont,btnDaysFont,btnBottomFont:TSpeedButton;
  chkFormSize:TLabel;//TCheckBox;
  fontname,fontsize,fontcolor,fontbold:string;
  fontname1,fontsize1,fontcolor1,fontbold1:string;
  fontname2,fontsize2,fontcolor2,fontbold2:string;
  cbbDefaultPNG:TComboBox;
  SelMemoColor:TColor;
  oldW,oldH,newW,newH:string;
  lblMemoPlace1,lblqm:TLabel;edtMemoPlace2,edtqm:TEdit;
   
  
function GetFontText(AllFontText:string;Index:integer;var s1,s2,s3,s4:string):string;
  var
    i,j:Integer;
    tempstr,ResultStr:string; 
  begin
    tempstr:=AllFontText;
    ResultStr:='';
    for i:=1 to Index do
      begin
        j:=Pos('/',tempstr);
        if j>0 then
          begin
	  if i=1 then s1:=Copy(tempstr,1,j-1);
	  if i=2 then s2:=Copy(tempstr,1,j-1);
	  if i=3 then s3:=Copy(tempstr,1,j-1);
	  if i=4 then s4:=Copy(tempstr,1,j-1);
          ResultStr:=ResultStr+Copy(tempstr,1,j);
          Delete(tempstr,1,j);
          end
          else begin
	  if i=4 then s4:=tempstr;
          ResultStr:=ResultStr+tempstr;
	  end;
      end;
    if Copy(ResultStr,Length(ResultStr),1)='/' then
      Delete(ResultStr,Length(ResultStr),1); 
    Result:=ResultStr;
  end;
  
procedure InitSetting;
var
	s,s1,s2:string;
	inifn:TIniFile;
	lst:TStrings;
	i:integer;
begin
  inifn:=TIniFile.create(SkinFileName);
 
  with inifn do
  begin
    try
    //背景图片 
      fIconBitmapName:=ReadString('system','BackgroundBitmapName','blue.png');
      begin
      if pos('\',fIconBitmapName)>0 then
        s:=fIconBitmapName
        else
        s:=SkinFilePath+'\'+fIconBitmapName;
      img.Picture.LoadFromFile(s);
      end;   
    
    //LoadPngImage(img,SkinPath+fIconBitmapName,i,i);

    SelMemoColor:=StrToColor(ReadString('Item0','SMS0EditColor','clMoneyGreen'));
    pnlMemoColor.brush.Color:=SelMemoColor;
    //短信编辑框
    edtMemoPlace.text:=ReadString('Item0','SMS0Rect','30/55/128/125');
    s1:=ReadString('Item0','SMS0Font','宋体/12/#000000/0');
    s1:=GetFontText(s1,4,fontname,fontsize,fontcolor,fontbold);
    edtTitleFont.text:=s1;
    //电话编辑框
    edtMemoPlace2.text:=ReadString('Item0','SMS1Rect','60/202/96/18');
    //s1:=ReadString('Item0','SMS1Font','宋体/12/#000000/0');
    //s1:=GetFontText(s1,4,fontname,fontsize,fontcolor,fontbold);
    //edtTitleFont.text:=s1;
    
    //签名
    edtqm.text:=ReadString('Item7','Item0','');

    s:=ReadString('system','Width','342');
    s1:=ReadString('system','Height','340');

      edtFormWidth.text:=IntToStr(StrToInt(s)-160);
      edtFormHeight.text:=s1; 
      edtFormWidth.ReadOnly:=False;
      edtFormHeight.ReadOnly:=False;
      oldW:=s;
      oldH:=s1;
 
    finally
      inifn.free;
    end; 
  end;
end;

//save Skin form setting
function SaveSetting:boolean;
var
	s:string;
	inifn:TIniFile;
	i:integer;
	Alls,s1,s2,s3,s4:string;
begin
  result:=false;
  result:=true;
  inifn:=TIniFile.create(SkinFileName);
  with inifn do
  try
    //背景图片 
    if pos('\',fIconBitmapName)<=0 then
        s:=SkinFilePath+'\'+fIconBitmapName
        else
        s:=fIconBitmapName;  
    img1.Picture.LoadFromFile(s);
    
    WriteString('system','BackgroundBitmapName',s); 
    WriteString('Item0','SMS0EditColor',ColorToStr(pnlMemoColor.brush.Color) );
    WriteString('Item0','SMS1EditColor',ColorToStr(pnlMemoColor.brush.Color) );
 
 
    newW:=edtFormWidth.text;
  	newH:=edtFormHeight.text; 
 
	  
    //Tab2
    WriteString('Item0','SMS0Rect', edtMemoPlace.text);
    WriteString('Item0','SMS1Rect', edtMemoPlace2.text);
    WriteString('Item0','SMS0Font', edtTitleFont.text+'/0/0/0/0/255/1/0/1/clBlack/0/0');
    WriteString('Item0','SMS1Font', edtTitleFont.text+'/0/0/0/0/255/1/0/1/clBlack/0/0'); 
    //签名    
	  WriteString('Item7','Item0', edtqm.text);
	  
	  WriteString('system','Width',IntToStr(StrToInt(newW)+160));
  	WriteString('system','Height',newH); 
  finally
    inifn.free;
  end;
end;

procedure CreateTab1;
begin
  //--- Tab 1 ---
  ts1:=TTabSheet.Create(pgc);
  ts1.PageControl:=pgc; 
  ts1.Caption:='背景图片设置';
  //image preview
  pngImage:=TPanel.create(ts1);
  with pngImage do
  begin
    parent:=ts1;
    left:=10; top:=10;
    width:=350; height:=200;
    caption:='';
    BevelOuter:=bvLowered;
  end;
  img:=TImage.create(pngImage);
  with img do
  begin
    cursor:=crHandPoint;
    parent:=pngImage;
    Align:=alClient; 
    AutoSize:=True;
    left:=1; top:=1;
    //width:=348; height:=198;
    img.hint:='单击选择背景图片';
    ShowHint:=True; 
    OnClick:=@imgClick;
  end;
  
  img1:=TImage.create(pngImage);
  with img1 do
  begin
    cursor:=crHandPoint;
    //parent:=pngImage;
    //Align:=alClient; 
    AutoSize:=True;
    left:=1; top:=1;
    Visible:=False;
    //width:=348; height:=198;
    //img.hint:='单击选择背景图片';
    //ShowHint:=True; 
    //OnClick:=@imgClick;
  end;

  //默认背景图片
  lblDefaultPNG:=TLabel.create(ts1);
  with lblDefaultPNG do
  begin
    parent:=ts1;
    left:=RowLeft; top:=pngImage.top+pngImage.Height+RowHeight-20;
    //Font.Color:=clBlue;
    //Font.style:=fsUnderLine;
    caption:='默认背景图片';
    cursor:=crHandPoint;
  end;
  
  cbbDefaultPNG:=TComboBox.Create(ts1);
  with cbbDefaultPNG do
  begin
    //visible:=False;
    Left:=RowLeft+lblDefaultPNG.width+2; Top:=pngImage.top+pngImage.Height+RowHeight-25; width:=100;
    DropDownCount:=8;
    Parent:=ts1;
    Style:=csDropDownList;
    with Items do
    begin 
      add('blue');
      add('front');
      add('green'); 
      add('red');
      add('white');
      add('orange');  
      add('');
    end;
    OnChange:=@DefaultPNGClick;
    ItemIndex:=0;
  end;

  
  //窗口宽度
  lblFormWidth:=TLabel.create(ts1);
  with lblFormWidth do
  begin
    parent:=ts1;
    left:=RowLeft; top:=pngImage.top+pngImage.Height+RowHeight;
    caption:='背景图片长：';
  end;
  edtFormWidth:=TEdit.create(self);
  with edtFormWidth do
  begin
    parent:=ts1;
    left:=lblFormWidth.left+70; top:=lblFormWidth.top-4; width:=50;
  end;
  lblFormHeight:=TLabel.create(ts1);
  with lblFormHeight do
  begin
    parent:=ts1;
    left:=edtFormWidth.left+60; top:=lblFormWidth.top;
    caption:='背景图片高：';
  end;
  edtFormHeight:=TEdit.create(ts1);
  with edtFormHeight do
  begin
    parent:=ts1;
    left:=lblFormHeight.left+70; top:=lblFormWidth.top-4; width:=50;
  end;

  chkFormSize:=TLabel.create(ts1);
  with chkFormSize do
    begin
    parent:=ts1;
    left:=edtFormHeight.left+edtFormHeight.width+10;
    top:=edtFormHeight.top+5;
    Font.Color:=clBlue;
    Font.Style:=fsUnderline;
    Cursor:=crHandPoint;
    //height:=17;
    width:=130;
    caption:='实际背景图片大小';
    OnClick:=@ChkFormSizeClick;
    end;
end;
  
procedure CreateTab2;
begin
  //--- Tab 2---
  ts2:=TTabSheet.Create(pgc);
  ts2.PageControl:=pgc;
  ts2.Caption:='手机设置';
  //发短信-字体  
  lblTitleFont:=TLabel.create(self);
  with lblTitleFont do
  begin
    parent:=ts2;
    left:=RowLeft; top:=10;
    caption:='短信字体(大小/颜色/粗体)';
  end;
  edtTitleFont:=TEdit.create(ts2);
  with edtTitleFont do
  begin
    parent:=ts2;
    //edtTitleFont.ReadOnly:=true;
    left:=lblTitleFont.left; top:= lblTitleFont.top+RowSep; width:=130;
  end;
  //选择字体-按钮
  btnTitleFont:=TSpeedButton.create(ts2);
  with btnTitleFont do
    begin
    parent:=ts2;
    left:=edtTitleFont.left+edtTitleFont.width+2; 
    top:=edtTitleFont.top;
    caption:='...';
    width:=23; height:=22;
    OnClick:=@BtnTitleFontClick;
    end;
  //短信显示位置
  lblMemoPlace:=TLabel.create(ts2);
  with lblMemoPlace do
  begin
    parent:=ts2;
    left:=RowLeft; top:= edtTitleFont.top+RowHeight;
    caption:='短信显示位置(距左/距顶/长/高)';
  end;
  edtMemoPlace:=TEdit.create(ts2);
  with edtMemoPlace do
  begin
    parent:=ts2;
    left:=RowLeft; top:= lblMemoPlace.top+RowSep; width:=130;
  end;
  //电话显示位置
  lblMemoPlace1:=TLabel.create(ts2);
  with lblMemoPlace1 do
  begin
    parent:=ts2;
    left:=14*RowLeft+edtMemoPlace.width; top:= edtTitleFont.top+RowHeight;          
    caption:='号码显示位置(距左/距顶/长/高)';
  end;
  edtMemoPlace2:=TEdit.create(ts2);
  with edtMemoPlace2 do
  begin
    parent:=ts2;
    left:=14*RowLeft+edtMemoPlace.width; top:= lblMemoPlace.top+RowSep; width:=130;
  end;
  //编辑框的颜色
  lblMemoColor:=TLabel.create(ts2);
  with lblMemoColor do
    begin
    parent:=ts2;
    left:=RowLeft; top:=edtMemoPlace.top+RowHeight;
    caption:='短信、号码编辑框的背景颜色';
    end;
  pnlMemoColor:= TShape.create(ts2);
  with pnlMemoColor do
    begin
    parent:=ts2;
    left:=RowLeft; top:=lblMemoColor.top+RowSep; width:=130; height:=33;
    OnMouseDown:=@PnlMemoColorClick;
    end;
  //签名
  lblqm:=TLabel.create(ts2);
  with lblqm do
  begin
    parent:=ts2;
    left:=RowLeft; top:= pnlMemoColor.top+pnlMemoColor.height+RowHeight-20;          
    caption:='短信签名(方便收到短信的人知道我是谁)';
  end;
  edtqm:=TEdit.create(ts2);
  with edtqm do
  begin
    parent:=ts2;
    left:=RowLeft; top:=lblqm.top+RowSep; width:=130;
  end;

end;

procedure imgClick(Sender: TObject);
var
  i:integer;
begin
  dlgPic:=TOpenPictureDialog.create(self);
  try
    dlgPic.Filter:='Portable Network Graphics (*.png)|*.png';
    dlgPic.FileName:= SkinFileName;
    //dlgPic.InitialDir:= ExtractFilePath(SkinFileName);
    if dlgPic.Execute then
    begin
      fIconBitmapName:=dlgPic.FileName;
      LoadPngImage(img,fIconBitmapName,i,i);
      cbbDefaultPNG.ItemIndex:=6;
      if Pos(SkinFilePath+'\blue.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=0;
      if Pos(SkinFilePath+'\front.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=1;
      if Pos(SkinFilePath+'\green.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=2;
      if Pos(SkinFilePath+'\red.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=3;
      if Pos(SkinFilePath+'\white.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=4;
      if Pos(SkinFilePath+'\orange.png',fIconBitmapName)>0 then 
        cbbDefaultPNG.ItemIndex:=5;
    end;
  finally
    dlgPic.free;
  end;
  
end;

procedure BtnTitleFontClick(Sender: TObject);
var
  i:integer;
  dlgFont1:TFontDialog;
begin
try
  dlgFont1:=TFontDialog.create(self);
  dlgFont1.Font.Name:=fontname;
  dlgFont1.Font.Size:=StrToInt(fontsize);
  dlgFont1.Font.Color:=StrToColor(fontcolor);
  if fontbold='1' then
    dlgFont1.Font.Style:=fsBold
    else
    dlgFont1.Font.Style:=nil;
  if dlgFont1.Execute then
    begin
    fontname:=dlgFont1.Font.Name;
    fontsize:=IntToStr( dlgFont1.Font.Size);
    fontcolor:=ColorToHtmlStr(dlgFont1.Font.Color);
    if dlgFont1.Font.Style=fsBold Then
      fontbold:='1'
      else
      fontbold:='0';
    end ;
  BtnTitleFontText:=fontname+'/'+fontsize+'/'+fontcolor+'/'+fontbold;
  edtTitleFont.text:= BtnTitleFontText;
finally
  dlgFont1.free;
end;
  
end;

procedure ChkFormSizeClick(Sender: TObject);
var s:string;
begin
    //if not pos('\',fIconBitmapName)<=0 then 
        s:=fIconBitmapName;  
    img1.Picture.LoadFromFile(s);
    edtFormWidth.text:=IntToStr(Img1.Picture.Width);
	  edtFormHeight.text:=IntToStr(Img1.Picture.Height); 
  {if ChkFormSize.checked then
      begin
      edtFormWidth.text:='';
      edtFormHeight.text:=''; 
      edtFormWidth.ReadOnly:=True;
      edtFormHeight.ReadOnly:=True;
      end
      else begin
      edtFormWidth.text:='220';
      edtFormHeight.text:='216'; 
      edtFormWidth.ReadOnly:=False;
      edtFormHeight.ReadOnly:=False; 
      end;}
end;

procedure PnlMemoColorClick(Sender: TObject; Button: TMouseButton; 
	Shift: TShiftState; X, Y: Integer);
var dlgColor1: TColorDialog;
begin
try
  dlgColor1:=TColorDialog.Create(ts2);
  if dlgColor1.Execute then
    TShape(Sender).Brush.color:=dlgColor1.color;
finally
  dlgColor1.free;  
end;
end;

procedure DefaultPNGClick(Sender: TObject);
var  inifn:TIniFile;
i:integer; s:string;
begin
try
  inifn:=TIniFile.create(SkinFileName);
  with inifn do
    begin
    fIconBitmapName:='blue.png';
    case cbbDefaultPNG.ItemIndex of
      0:begin
      fIconBitmapName:='blue.png'; 
      end;
      1:begin
      fIconBitmapName:='front.png';  
      end;
      2:begin
      fIconBitmapName:='green.png'; 
      end;
      3:begin
      fIconBitmapName:='red.png';  
      end;
      4:begin
      fIconBitmapName:='white.png'; 
      end;
      5:begin
      fIconBitmapName:='orange.png';  
      end;
    end; 
    fIconBitmapName:=SkinFilePath+'\'+fIconBitmapName;
    img.Picture.LoadFromFile(fIconBitmapName);
    end;
finally
  inifn.free;
end;
end;


begin
  CreateTab1;
  CreateTab2;
  InitSetting;
end.