main.~pas
上传用户:titech
上传日期:2010-04-06
资源大小:175k
文件大小:4k
源码类别:

家庭/个人应用

开发平台:

Delphi

  1. unit main;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs, StdCtrls,ShellApi;
  6. type
  7.   TForm1 = class(TForm)
  8.     Button1: TButton;
  9.     procedure Button1Click(Sender: TObject);
  10.     procedure FormCreate(Sender: TObject);
  11.   private
  12.     { Private declarations }
  13.   public
  14.     procedure WMDeviceChange( var Msg:Tmessage);message WM_DEVICECHANGE ;
  15.   end;
  16. const
  17.    BDT_DEVICEARRIVAL=$8000;   //U盘插上时的消息
  18. type
  19.   TU_drive=array[1..24] of integer;// 保存 U盘的数字符号
  20. var
  21.   Form1: TForm1;
  22.   U_cnt: integer;  //U 盘个数;
  23.   U_drive:TU_drive;
  24. implementation
  25. {$R *.dfm}
  26. {---------文件复制函数----------}
  27.   procedure Xcopy(ToDir,FromDir:String);
  28.   var
  29.     OpStruc:TSHFileOpStruct;  //声明一个TSHFileOpStruct 类型, 在ShellApi.pas 里
  30.     FromBuf,ToBuf: Array[0..128] of char;
  31.   begin
  32.       FillChar(FromBuf,sizeof(FromBuf),0);
  33.       FillChar(ToBuf,sizeof(ToBuf),0);
  34.       StrPCopy(FromBuf,FromDir+'*.mp3');     //
  35.       StrPCopy(ToBuf,ToDir);
  36.       with  OpStruc do
  37.       begin
  38.          Wnd:=form1.Handle ;
  39.          wFunc:=FO_COPY; //执行拷贝操作
  40.          pFrom:=@FromBuf;
  41.          pTo:=@ToBuf;  //FOF_SILENT or FOF_NOCONFIRMMKDIR or
  42.          fFlags:= FOF_SILENT or FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION ;
  43.                //不带进度条     直接建立文件夹       一切询问以 YES 回答
  44.          fAnyOperationsAborted:=False;
  45.          hNameMappings:=Nil;
  46.          lpszProgressTitle:=Nil;
  47.       end;
  48.       ShFileOperation(OpStruc);// 调用API函数,完成操作
  49.       showmessage('copying');
  50.   end;
  51. {----------- 初始话 ------------}
  52.   procedure  init();
  53.   var
  54.     i:integer;
  55.   begin
  56.     U_cnt:=0;
  57.     for i:=1 to 24 do
  58.       U_drive[i]:=0;
  59.   end;
  60. {---------- ↓判断是否为U盘 ↓--------}
  61.   function isudriver( num:integer): boolean;
  62.   var
  63.     Drive:string;
  64.     DriveType: WORD;
  65.   begin
  66.     Drive:= char(ord('A') + num) + ':';
  67.     DriveType:=GetDriveType(Pchar(Drive));
  68.     if DriveType=DRIVE_REMOVABLE then
  69.       result:=true
  70.     else
  71.       result:=false;
  72.   end;
  73. {----------↓本地盘扫描 ↓------------}
  74. procedure scan();
  75.  var
  76.    i:integer;
  77.  begin
  78.     init(); //初始化,避免重复记数
  79.     for i:=3 to 25 do      //从D 盘开始扫描
  80.       begin
  81.          if isudriver(i) then
  82.            begin
  83.              U_cnt:=U_cnt + 1;
  84.              U_drive[U_cnt]:=i;   //将盘符的数字代号保存
  85.            end;
  86.       end;
  87.  end;
  88. {-------------  test  -----------------}
  89. procedure TForm1.Button1Click(Sender: TObject);
  90. var
  91.   i:integer;
  92.   str:string;
  93. begin
  94.   scan();
  95.   i:=U_drive[1];
  96.   form1.Caption:=inttostr(U_cnt) + ' 个盘,该盘为' + char(i + ord('A') );
  97.   str:=char(i+ord('A'));
  98.   Xcopy('c:temp', pchar(str+ ':'));
  99. end;
  100.  {-----------↓ 初始化 ↓-----------}
  101. procedure TForm1.FormCreate(Sender: TObject);
  102. begin
  103.    init();
  104. end;
  105. {----------↓ U盘插入事件 ↓ ----------}
  106.   procedure TForm1.WMDeviceChange(var Msg:Tmessage);
  107.   var
  108.     i:integer;
  109.     str:string;
  110.   begin
  111.     inherited; //什么意思?
  112.     case Msg.WParam of
  113.         BDT_DEVICEARRIVAL:       // U 盘插上事件
  114.            begin
  115.              form1.Tag:=form1.Tag+1;
  116.              form1.Caption:=inttostr(form1.tag);
  117.              if form1.tag mod 3=0 then
  118.                begin
  119.                  scan();
  120.                  if U_cnt>0 then
  121.                    for i:=1 to U_cnt do
  122.                      begin
  123.                      str:=char(U_drive[i]+ ord('A'));  //取盘符
  124.                      showmessage('begin copy '+ str);
  125.                      Xcopy('c:temp', pchar(str+ ':'));
  126.                      showmessage('end of copy!');
  127.                      end;
  128.                end;
  129.            end;
  130.     end;  // end of case
  131.  end;
  132. end.