如何颜色保存在ini文件中(100分)

  • 主题发起人 主题发起人 sdonline
  • 开始时间 开始时间
S

sdonline

Unregistered / Unconfirmed
GUEST, unregistred user!

我要把选择的颜色保存在ini文件中,下次运行程序时,再把颜色读出来,在程序中显示。
我想了两种方法不知如何实现
1、把Tcolor的颜色转化成rgb保存,下次运行程序时再转化为Tcolor.
2、把Tcolor的颜色转化成数值,再还原
 
1、用ColorToString()把所选择的颜色转为String;
2、用Writestring把以上的字串写入INI文件
3、用Readstring从INI文件中读入字串
4、用StringToColor()把字串转为颜色值
 
procedure TForm1.Button1Click(Sender: TObject);
var
s:TColor;
inifile: TiniFile;
s1:string;
begin
inifile := Tinifile.Create('d:/temp/a.ini');
if ColorDialog1.Execute then
begin
s := ColorDialog1.Color;
inifile.Writestring('Version', 'Color', colortostring(s));
end;
inifile.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
s:string;
inifile: TiniFile;
begin
inifile := Tinifile.Create('d:/temp/a.ini');
s := inifile.Readstring('Version', 'Color', '');
memo1.Color := StringToColor(s);
inifile.Free;
end;
 
lhc4000方法就可以了.
 
上面的做法读出来都是字符串,

inifile.WriteInt('Color', 'R', 128);
inifile.WriteInt('Color', 'G', 128);
inifile.WriteInt('Color', 'B', 128);

r:=iniFile.ReadInt('Color', 'R');
g:=iniFile.ReadInt('Color', 'G');
b:=iniFile.ReadInt('Color', 'B');
再把r、g、b三个变量变成颜色




 
读出的是String还是Int并不是主要的,
用ColorToString()与StringToColor()实现起来要比通过R、G、B简单一些。
 
更简单的办法是不要用INI,直接用文本文件(如果你的INI文件不需要保存别的东西的话)
st:=TstringList.create;
st.SaveToFile('c:/color.txt');
 
type
PColor = ^TColor;
TColor = -$7FFFFFFF-1..$7FFFFFFF;
所以颜色是Integer类型数据
 
谢谢各位了,其实我已经自己做出来了,但还是谢谢大家
 
后退
顶部