给你个,这个是监视U盘变化的
//pas
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellApi;
type
TForm1 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
OldWindowProc: TWndMethod;
procedure FormWndProc(var Message: TMessage);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVTYP_VOLUME = $00000002; // logical volume
DBTF_MEDIA = $0001; // media comings and goings
type
PDEV_BROADCAST_HDR = ^TDEV_BROADCAST_HDR;
TDEV_BROADCAST_HDR = packed record
dbch_size : DWORD;
dbch_devicetype : DWORD;
dbch_reserved : DWORD;
end;
PDEV_BROADCAST_VOLUME = ^TDEV_BROADCAST_VOLUME;
TDEV_BROADCAST_VOLUME = packed record
dbcv_size : DWORD;
dbcv_devicetype : DWORD;
dbcv_reserved : DWORD;
dbcv_unitmask : DWORD;
dbcv_flags : WORD;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OldWindowProc:= WindowProc;
WindowProc := FormWndProc;
end;
procedure TForm1.FormWndProc(var Message: TMessage);
var
lpdb : PDEV_BROADCAST_HDR;
lpdbv : PDEV_BROADCAST_VOLUME;
unitmask
WORD;
i: Integer;
begin
lpdb := PDEV_BROADCAST_HDR(Message.LParam);
OldWindowProc(Message);
if (Message.Msg=WM_DEVICECHANGE) then
begin
if (Message.WParam=DBT_DEVICEARRIVAL) then
begin
Memo1.Lines.Add('Usb Disk Insert!');
if lpdb.dbch_devicetype=DBT_DEVTYP_VOLUME then
begin
lpdbv := PDEV_BROADCAST_VOLUME(lpdb);
unitmask:=lpdbv.dbcv_unitmask;
for i:=0 to 25 do
begin
if Boolean(unitmask and $1)then
break;
unitmask := unitmask shr 1;
end;
Memo1.Lines.Add('USB No : ' + Char(Ord('A')+i) );
end;
end;
if (Message.WParam=DBT_DEVICEREMOVECOMPLETE) then
begin
Memo1.Lines.Add('Usb Disk 退出!');
end;
end;
end;
end.
//dfm
object Form1: TForm1
Left = 192
Top = 107
Width = 382
Height = 295
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Memo1: TMemo
Left = 8
Top = 8
Width = 361
Height = 249
Lines.Strings = (
'Memo1')
TabOrder = 0
end
end