全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1776
推到 Plurk!
推到 Facebook!

進度表,不知道哪錯了 @@:

 
junlin
初階會員


發表:66
回覆:94
積分:42
註冊:2002-03-13

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-05-06 18:27:30 IP:61.218.xxx.xxx 未訂閱
下面是我寫的一個有進度表copy file的function 但是找了許久,還是不知道哪錯,好心人士幫忙指點一下吧 ^^ 謝謝~~ function TfrmAutoUpdate.CopyFile(sSourceFile, sTargetFile: string): Boolean; var iSourceFile, iTargetFile: Integer; iFileLength: Integer; iBytesRead, iByteCount: Integer; Buffer: PChar; begin try iSourceFile := FileOpen(sSourceFile, fmOpenRead); iTargetFile := FileCreate(sTargetFile); iFileLength := FileSeek(iSourceFile,0,2); iByteCount := 1024; FileSeek(iSourceFile,0,0); Gauge1.Visible := True; Gauge1.MaxValue := iFileLength; while iByteCount <= iFileLength do begin Buffer := PChar(AllocMem(iByteCount)); iBytesRead := FileRead(iSourceFile, Buffer, iByteCount); FileWrite(iTargetFile, Buffer, SizeOf(Buffer)); Gauge1.Progress := Gauge1.Progress iBytesRead; FileSeek(iSourceFile,iByteCount,1); FreeMem(Buffer); application.ProcessMessages; end; result := True; except FileClose(iSourceFile); FileClose(iTargetFile); result := False; end; end;
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-05-07 21:25:05 IP:192.168.xxx.xxx 未訂閱
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Gauges;    type
  TForm1 = class(TForm)
    Button1: TButton;
    Gauge1: TGauge;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    function CopyFile(sSourceFile, sTargetFile: string): Boolean;
    { Public declarations }
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}
function TForm1.CopyFile(sSourceFile, sTargetFile: string): Boolean;
var
  iSourceFile, iTargetFile: Integer;
  iFileLength: Integer;
  iBytesRead, iByteCount: Integer;
  iTotRead:integer;
  Buffer: array[0..1023] of char;
begin
  try
    iSourceFile := FileOpen(sSourceFile, fmOpenRead);
    iTargetFile := FileCreate(sTargetFile);
    iFileLength := FileSeek(iSourceFile,0,2);
    iByteCount := 1024;
    FileSeek(iSourceFile,0,0);        Gauge1.Visible := True;
    Gauge1.MaxValue := iFileLength;
    iTotRead:=0;
    while iTotRead < iFileLength do
    begin
      //Buffer := PChar(AllocMem(iByteCount));
      iBytesRead := FileRead(iSourceFile, Buffer, iByteCount);
      FileWrite(iTargetFile, Buffer, iBytesRead);
      iTotRead:=iTotRead iBytesRead;          Gauge1.Progress := iTotRead;
//      FileSeek(iSourceFile,iByteCount,1);
    //FreeMem(Buffer);
    application.ProcessMessages;
    end;
    result := True;
  except
    FileClose(iSourceFile);
    FileClose(iTargetFile);
    result := False;
  end;
end;        procedure TForm1.Button1Click(Sender: TObject);
begin
  CopyFile('c:\doctor.txt','c:\doctor2.txt');
end;    end.
P.S: 1.Stream 最好不要用Pchar存data,用Array of char比較好 2.while迴圈中的變數弄錯 3.Gauge1.Progress的設定變數弄錯 4.FileSeek(iSourceFile,iByteCount,1);不需要,因為FileRead自動會移動檔案指標 5.許多程式的觀念待加強,多參考別人的程式,要加加油喔! ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~
系統時間:2024-04-20 19:15:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!