假如有这样一个文本文件
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循环,行数怎么求得啊

解决方案 »

  1.   

    for i := Low(item) to High(Item) do还有下次不要放到非技术区
      

  2.   

    假设文本文件按你提供的内容, 存储于C盘根目录下。读出后画到Form上procedure YourProc(DrawType, R1, R2, R3, R4 : string);//这个过程实际上就是你提供的一段程序
    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;
      

  3.   

    Var
       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;
      

  4.   

    SS := TStringList.Create;
        SS.Text := StringReplace(S, ' ', #10, [rfReplaceAll]);
    上边的这段代码什么意思啊?谢谢!
      

  5.   

    上边的这段代码什么意思啊?谢谢!
    ---------------------------------借用了一个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函数据原字串中的空格分隔符换成换行符
      

  6.   

    把原字串中的空格分隔符换成换行符并赋给SS(字符串列表)的Text属性。