mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls, ComCtrls, Buttons;
  6. type
  7.   TForm1 = class(TForm)
  8.     GroupBox1: TGroupBox;
  9.     Label1: TLabel;
  10.     Edit1: TEdit;
  11.     Edit2: TEdit;
  12.     Label2: TLabel;
  13.     ProgressBar1: TProgressBar;
  14.     Label3: TLabel;
  15.     BitBtn1: TBitBtn;
  16.     BitBtn2: TBitBtn;
  17.     OpenDialog1: TOpenDialog;
  18.     SaveDialog1: TSaveDialog;
  19.     BitBtn3: TBitBtn;
  20.     BitBtn4: TBitBtn;
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure BitBtn1Click(Sender: TObject);
  23.     procedure BitBtn2Click(Sender: TObject);
  24.     procedure BitBtn4Click(Sender: TObject);
  25.     procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  26.     procedure BitBtn3Click(Sender: TObject);
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.   end;
  32. var
  33.   Form1: TForm1;
  34.   Copying:Boolean;
  35. implementation
  36. {$R *.dfm}
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. begin
  39.  OpenDialog1.FileName:='';
  40.  OpenDialog1.InitialDir:=ExtractFileDir(Application.ExeName);
  41.  SaveDialog1.FileName:='';
  42.  SaveDialog1.InitialDir:=ExtractFileDir(Application.ExeName);
  43.  BitBtn3.Enabled:=False;
  44.  Copying:=False;
  45. end;
  46. procedure TForm1.BitBtn1Click(Sender: TObject);
  47. begin
  48.  With Opendialog1 do
  49.   if Execute then
  50.    Edit1.Text:=Opendialog1.FileName;
  51.  if ((Edit1.Text<>'') and (Edit2.Text<>'')) then
  52.    BitBtn3.Enabled:=True
  53.  else
  54.    BitBtn3.Enabled:=False;
  55. end;
  56. procedure TForm1.BitBtn2Click(Sender: TObject);
  57. begin
  58.  With SaveDialog1 do
  59.   if Execute then
  60.    Edit2.Text:=Savedialog1.FileName;
  61.  if ((Edit1.Text<>'') and (Edit2.Text<>'')) then
  62.    BitBtn3.Enabled:=True
  63.  else
  64.    BitBtn3.Enabled:=False;  
  65. end;
  66. procedure TForm1.BitBtn4Click(Sender: TObject);
  67. begin
  68.  Close;
  69. end;
  70. procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  71. begin
  72.  if Copying then
  73.   CanClose:=False
  74.  else
  75.   CanClose:=True;
  76. end;
  77. procedure TForm1.BitBtn3Click(Sender: TObject);
  78. var
  79.  FromFile,ToFile:File;
  80.  NumRead,NumWrite:Integer;
  81.  Buf:Array[1..2048] of byte;
  82. begin
  83.  AssignFile(FromFile,Opendialog1.FileName);
  84.  Reset(FromFile,1);
  85.  AssignFile(ToFile,SaveDialog1.FileName);
  86.  Rewrite(ToFile,1);
  87.  ProgressBar1.Max:=(FileSize(FromFile)*2 div sizeof(buf));
  88.  repeat
  89.    BlockRead(FromFile,Buf,Sizeof(Buf),NumRead);
  90.    ProgressBar1.StepIt;
  91.    BlockWrite(ToFile,Buf,NumRead,NumWrite);
  92.    ProgressBar1.StepIt;
  93.    Copying:=True;
  94.    Application.ProcessMessages;
  95.  until ((NumRead=0) or (NumWrite<>NumRead));
  96.  CloseFile(FromFile);
  97.  CloseFile(ToFile);
  98.  Copying:=False;
  99.  ShowMessage('复制文件过程成功完成.');  
  100. end;
  101. end.