?? main.pas
字號:
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+'*.txt'); //
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.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -