font.style的问题(50分)

老莫

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把font.style写到ini文件里去。我想style应该是枚举类型的
因此我这样写
case memo1.font.Style of
fsbold: wordproIni.WriteString('setting', 'fontstylebold','fsbold');
fsItalic:wordproIni.WriteString('setting', 'fontstyleItalic','fsItalic');
fsUnderline:wordproIni.WriteString('setting', 'fontstyleUnderline','fsUnderline');
fsStrikeOut: wordproIni.WriteString('setting', 'fontstyleStrikeOut','fsStrikeOut');
end;
但编译通不过,提示case memo1.font.Style of这一句
Ordinal type required

way?
 
因为font.style是集合类型的数据,所以不能用case
如,它可以是[fsBold,fsUnderline]等等
我想可以这样
for i:=fsbold to fsStrikeOut do //看一下fsxxx的定义
begin
if i in memo1.font.style then WriteString(...);
end;



 
2 kangxy:

照你的方法,WriteString里改写什么就不好办了,

修改如下:

<font color=red>const
FontStyleName:array[fsBold..fsStrikeOut]of String=
('xxx','xx','xxx','xx','xx');//在此处定义对应的名称</font>

for I:=fsbold to fsStrikeOut do
begin
if I in memo1.font.style then
WriteString(<B>FontStyleName</B>);
end;

细节上可能有问题,自己搞定吧!
 
>>我想把font.style写到ini文件里去

把问题又看了一遍,有点想法:

1、如果纯粹只为保存Font.Style
可以将Style转换为Integer,然后直接保存这个Integer就够了

2、但是这样INI文件会很不好看
所以还是应该用你原来的方法,
不过建议你改用<font color=red>WriteBool</font>更好

程序如下:
const FontStyleName:array[fsBold..fsStrikeOut]of String=
('xxx','xx','xxx','xx','xx');//在此处定义对应的名称
var
Bool:Boolean;
begin
for I:=fsbold to fsStrikeOut do
begin
<B>Bool:=I in memo1.font.style;
WriteBool('xxx',FontStyleName,Bool);</B>
end;
end;
 
给你一个例子,保你全部搞定,呵呵。

//把字体转化为字符串
//输出的格式:
// 宋体,134,9,[Bold|Italic],[clAqua]
function FontToString(Font : TFont ) : string;
var
sStyle : string;
begin
with Font do
begin
// convert font style to string
sStyle := '';

if( fsBold in Style )then
sStyle := sStyle + csfsBold;

if( fsItalic in Style )then
sStyle := sStyle + csfsItalic;

if( fsUnderline in Style )then
sStyle := sStyle + csfsUnderline;

if( fsStrikeout in Style )then
sStyle := sStyle + csfsStrikeout;

if( ( Length( sStyle ) > 0 ) and ( '|' = sStyle[ 1 ] ) )then //去除第一个 ‘|’
begin
sStyle := Copy( sStyle, 2,Length( sStyle ) - 1 );
end;

Result := Format('%s,%d,%d,[%s],[%s]',
[ Name, //字体的名称
CharSet, //字符集
Size, //字体的大小
sStyle, //字体的属性
ColorToString( Color ) ] //字体的颜色
);
end;
end;

//把字符串转换为字体的相关属性!
//sFont参数的格式: 宋体,134,9,[Bold],[clRed]
procedure StringToFont(sFont : string
Font : TFont );
var
p : integer;
sStyle : string;
begin
with Font do
begin
// get font name
p := Pos( ',', sFont );
Name := Copy( sFont, 1, p-1 );
Delete( sFont, 1, p );

// get font charset
p := Pos( ',', sFont );
Charset :=StrToInt( Copy( sFont, 1, p-1 ) );
Delete( sFont, 1, p );

// get font size
p := Pos( ',', sFont );
Size :=StrToInt( Copy( sFont, 1, p-1 ) );
Delete( sFont, 1, p );

// get font style
p := Pos( ',', sFont );
sStyle := '|' + Copy( sFont, 2, p-3 );
Delete( sFont, 1, p );

// get font color
Color :=StringToColor(Copy( sFont, 2,Length( sFont ) - 2 ) );

// convert str font style to font style
Style := [];

if( Pos( csfsBold,sStyle ) > 0 )then
Style := Style + [ fsBold ];

if( Pos( csfsItalic,sStyle ) > 0 )then
Style := Style + [ fsItalic ];

if( Pos( csfsUnderline,sStyle ) > 0 )then
Style := Style + [ fsUnderline ];

if( Pos( csfsStrikeout,sStyle ) > 0 )then
Style := Style + [ fsStrikeout ];
end;
end;

 
这样应该行吧:
for i := fsbold to fsStrikeOut do
case ord(i) of
1:...
2:...
3:...
4:...
end;
 
SuperMMX应该是最好的。
 
结束吧!别占地方了~~
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
2K
import
I
顶部