假如有这样一个文本文件
Ellipse 200 310 320 250 210
Line 126 210 235 301
Rectangle 230 210 320 350
我要读出它的每行,且画出相应的图形item[1]是每行第一个字符串,
if item[1]='Line' then
begin
canvas.moveto(strtoint(item[2]),strtoint(item[3]));
canvas.lineto(strtoint(item[4]),strtoint(item[5]));
end;
if item[1]='Rectangle' then
begin
canvas.Rectangle(strtoint(item[2]),strtoint(item[3]),strtoint(item[4]),strtoint(item[5]));
end;
if item[1]='Ellipse' then
begin
canvas.Ellipse(strtoint(item[2]),strtoint(item[3]),strtoint(item[4]),strtoint(item[5]));
end;
这样循环只能画到第一个图形,我想加一个FOR循环,行数怎么求得啊
Ellipse 200 310 320 250 210
Line 126 210 235 301
Rectangle 230 210 320 350
我要读出它的每行,且画出相应的图形item[1]是每行第一个字符串,
if item[1]='Line' then
begin
canvas.moveto(strtoint(item[2]),strtoint(item[3]));
canvas.lineto(strtoint(item[4]),strtoint(item[5]));
end;
if item[1]='Rectangle' then
begin
canvas.Rectangle(strtoint(item[2]),strtoint(item[3]),strtoint(item[4]),strtoint(item[5]));
end;
if item[1]='Ellipse' then
begin
canvas.Ellipse(strtoint(item[2]),strtoint(item[3]),strtoint(item[4]),strtoint(item[5]));
end;
这样循环只能画到第一个图形,我想加一个FOR循环,行数怎么求得啊
begin
with Form1 do
begin
if DrawType='Line' then
begin
canvas.moveto(strtoint(R1),strtoint(R2));
canvas.lineto(strtoint(R3),strtoint(R4));
end;
if DrawType='Rectangle' then
begin
canvas.Rectangle(strtoint(R1),strtoint(R2),strtoint(R3),strtoint(R4));
end;
if DrawType='Ellipse' then
begin
canvas.Ellipse(strtoint(R1),strtoint(R2),strtoint(R3),strtoint(R4));
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
F : TextFile;
S : String;
SS : TStringList;
begin
AssignFile(F, 'C:\Test.txt');
Reset(F);
while not Eof(F) do
begin
ReadLn(F, S);
SS := TStringList.Create;
SS.Text := StringReplace(S, ' ', #10, [rfReplaceAll]);
YourProc(SS[0],SS[1],SS[2],SS[3],SS[4]);
SS.Free;
end;
CloseFile(F);
end;
SL , PL : TStringList;
I : Integer;
begin
Try
SL := TStringList.Create;
SL.LoadFromFile('aa.txt');
For I := 0 To SL.Count - 1 Do
Begin
Try
PL := TStringList.Create;
PL.Text := StringReplace(SL.Strings[I], ' ', #10, [rfReplaceAll]);
If PL[0] = 'Line' Then
Begin
Canvas.MoveTo(StrToInt(PL[1]),StrToInt(PL[2]));
Canvas.LineTo(StrToInt(PL[3]),StrToInt(PL[4]));
End;
If PL[0] = 'Rectangle' Then
Begin
Canvas.Rectangle(StrToInt(PL[1]),StrToInt(PL[2]),StrToInt(PL[3]),StrToInt(PL[4]));
End;
If PL[0] = 'Ellipse' Then
Begin
Canvas.Ellipse(StrToInt(PL[1]),StrToInt(PL[2]),StrToInt(PL[3]),StrToInt(PL[4]));
End;
Finally
PL.Free;
End;
End;
Finally
SL.Free;
End;
end;
SS.Text := StringReplace(S, ' ', #10, [rfReplaceAll]);
上边的这段代码什么意思啊?谢谢!
---------------------------------借用了一个TStringList对象,把原字符串'Ellipse 200 310 320 250 210'分解为这样的形式:Ellipse
200
310
320
250
210其中SS := TStringList.Create;这句是构造TStringList类的对象SS,这应该不难理解。这句与SS.Free析构(Destroy)是相对应的。SS.Text := StringReplace(S, ' ', #10, [rfReplaceAll]);
这句是用StringReplace函数据原字串中的空格分隔符换成换行符