mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:5k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls,IniFiles;
  6. type
  7.   TForm1 = class(TForm)
  8.     ComboBox1: TComboBox;
  9.     Label1: TLabel;
  10.     Memo1: TMemo;
  11.     Button1: TButton;
  12.     FontDialog1: TFontDialog;
  13.     ColorDialog1: TColorDialog;
  14.     Button2: TButton;
  15.     Button3: TButton;
  16.     Button4: TButton;
  17.     procedure Button1Click(Sender: TObject);
  18.     procedure FormCreate(Sender: TObject);
  19.     procedure ComboBox1Change(Sender: TObject);
  20.     procedure Button2Click(Sender: TObject);
  21.     procedure Button3Click(Sender: TObject);
  22.   private
  23.     { Private declarations }
  24.   public
  25.     procedure RestoreLastSettings(FileName:String);
  26.     procedure SaveFirstSettings(FileName:String);
  27.     procedure SaveCurrentSettings;
  28.     procedure LoadOtherSettings;
  29.     { Public declarations }
  30.   end;
  31. var
  32.   Form1: TForm1;
  33. implementation
  34. {$R *.dfm}
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. begin
  37.  with FontDialog1 do
  38.   begin
  39.    if execute then
  40.     begin
  41.      Memo1.Font:=FontDialog1.Font;
  42.     end;
  43.   end;
  44. end;
  45. procedure TForm1.SaveCurrentSettings;
  46. var
  47.  IniFile:TIniFile;
  48.  FileName:String;
  49.  SettingStr:String;
  50.  Number:Integer;
  51. begin
  52.  FileName:=ExtractFilePath(Application.ExeName)+'IniHandle.ini';
  53.  IniFile:=TInifile.Create(FileName);
  54.  if ComboBox1.ItemIndex=1 then
  55.   begin
  56.    SettingStr:='Settings1';
  57.    Number:=2;
  58.   end
  59.   else
  60.   begin
  61.    SettingStr:='Settings2';
  62.    Number:=1;
  63.   end;
  64.  try
  65.   With IniFile do
  66.    begin
  67.     WriteInteger('ThemeNumber','Number',Number);
  68.     WriteString(SettingStr,'FormColor',ColorToString(Form1.Color));
  69.     WriteString(SettingStr,'Memo.Font.Color',ColorToString(Memo1.Font.Color));
  70.     WriteInteger(SettingStr,'Memo.Font.Size',Memo1.Font.Size);
  71.     WriteString(SettingStr,'Memo.Color',ColorToString(Memo1.Color));
  72.     WriteString(SettingStr,'Memo.Font.Name',Memo1.Font.Name);
  73.    end;
  74.   finally
  75.    IniFile.Free;
  76.   end;  
  77. end;
  78. procedure TForm1.LoadOtherSettings;
  79. var
  80.  IniFile:TIniFile;
  81.  Number:Integer;
  82.  SettingStr:String;
  83.  FileName:String;
  84. begin
  85.  FileName:=ExtractFilePath(Application.ExeName)+'IniHandle.ini';
  86.  IniFile:=TInifile.Create(FileName);
  87.  try
  88.   With IniFile do
  89.    begin
  90.      Number:=ComboBox1.ItemIndex+1;
  91.      SettingStr:='Settings'+IntToStr(Number);
  92.      Form1.Color:=StringToColor(ReadString(SettingStr,'FormColor','ClBtnFace'));
  93.      Memo1.Color:=StringToColor(ReadString(SettingStr,'Memo.Color','ClBtnFace'));
  94.      Memo1.Font.Color:=StringToColor(ReadString(SettingStr,'Memo.Font.Color','ClBtnFace'));
  95.      Memo1.Font.Size:=ReadInteger(SettingStr,'Memo.Font.Size',0);
  96.      Memo1.Font.Name:=ReadString(SettingStr,'Memo.Font.Name','System');
  97.    end;
  98.  finally
  99.   IniFile.Free;
  100.  end; 
  101. end;
  102. procedure TForm1.RestoreLastSettings(FileName:String);
  103. var
  104.  IniFile:TIniFile;
  105.  Number:Integer;
  106.  SettingStr:String;
  107. begin
  108.  IniFile:=TInifile.Create(FileName);
  109.  try
  110.   With IniFile do
  111.    begin
  112.      Number:=ReadInteger('ThemeNumber','Number',1);
  113.      ComboBox1.ItemIndex:=Number-1;
  114.      SettingStr:='Settings'+IntToStr(Number);
  115.      Form1.Color:=StringToColor(ReadString(SettingStr,'FormColor','ClBtnFace'));
  116.      Memo1.Color:=StringToColor(ReadString(SettingStr,'Memo.Color','ClBtnFace'));
  117.      Memo1.Font.Color:=StringToColor(ReadString(SettingStr,'Memo.Font.Color','ClBtnFace'));
  118.      Memo1.Font.Size:=ReadInteger(SettingStr,'Memo.Font.Size',0);
  119.      Memo1.Font.Name:=ReadString(SettingStr,'Memo.Font.Name','System');
  120.    end;
  121.  finally
  122.   IniFile.Free;
  123.  end; 
  124. end;
  125. procedure TForm1.SaveFirstSettings(FileName:String);
  126. var
  127.  IniFile:TIniFile;
  128. begin
  129.  IniFile:=TInifile.Create(FileName);
  130.  try
  131.   With IniFile do
  132.    begin
  133.     WriteInteger('ThemeNumber','Number',1);
  134.     WriteString('Settings1','FormColor',ColorToString(Form1.Color));
  135.     WriteString('Settings1','Memo.Color',ColorToString(Memo1.Color));
  136.     WriteString('Settings1','Memo.Font.Color',ColorToString(Memo1.Font.Color));
  137.     WriteInteger('Settings1','Memo.Font.Size',Memo1.Font.Size);
  138.     WriteString('Settings1','Memo.Font.Name',Memo1.Font.Name);
  139.     WriteString('Settings2','FormColor',ColorToString(Form1.Color));
  140.     WriteString('Settings2','Memo.Color',ColorToString(Memo1.Color));
  141.     WriteString('Settings2','Memo.Font.Color',ColorToString(Memo1.Font.Color));
  142.     WriteInteger('Settings2','Memo.Font.Size',Memo1.Font.Size);
  143.     WriteString('Settings2','Memo.Font.Name',Memo1.Font.Name);
  144.    end;
  145.   finally
  146.    IniFile.Free;
  147.   end;  
  148. end;
  149. procedure TForm1.FormCreate(Sender: TObject);
  150. var
  151.  FileName:String;
  152. begin
  153.  FileName:=ExtractFilePath(Application.ExeName)+'IniHandle.ini';
  154.  if FileExists(FileName) then
  155.   begin
  156.    RestoreLastSettings(FileName);
  157.   end
  158.   else
  159.   begin
  160.    SaveFirstSettings(FileName);
  161.   end;
  162. end;
  163. procedure TForm1.ComboBox1Change(Sender: TObject);
  164. begin
  165.  SaveCurrentSettings;
  166.  LoadOtherSettings;
  167. end;
  168. procedure TForm1.Button2Click(Sender: TObject);
  169. begin
  170.   with ColorDialog1 do
  171.   begin
  172.    if execute then
  173.     begin
  174.      Memo1.Color:=ColorDialog1.Color;
  175.     end;
  176.   end;
  177. end;
  178. procedure TForm1.Button3Click(Sender: TObject);
  179. begin
  180. with ColorDialog1 do
  181.   begin
  182.    if execute then
  183.     begin
  184.      Form1.Color:=ColorDialog1.Color;
  185.     end;
  186.   end;
  187. end;
  188. end.