//檢測U盤插入、拔出
測試通過
1、新建工程;
2、定義一個私有過程
procedure WMDeviceChange(var Msg: TMessage);
message WM_DEVICECHANGE;
完整代碼如下:
unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants,
Classes, Graphics, Controls, Forms,
Dialogs, AppEvnts, ExtCtrls,
StdCtrls;
type
TForm1 = class(TForm)
procedure
Button1Click(Sender: TObject);
private
{ Private declarations
}
procedure WMDeviceChange(var Msg: TMessage); message
WM_DEVICECHANGE;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.WMDeviceChange (var Msg:
TMessage);
var
myMsg : String;
begin
Case Msg.WParam of
32768:
begin
myMsg :='U盤插入';
MessageBox(0,'注意!U盤已插入!!!','AutoCopy
Information',MB_ICONASTERISK and MB_ICONINFORMATION);
end;
32772:
begin
myMsg :='U盤拔出';
MessageBox(0,'注意!U盤已取走!!!','AutoCopy
Information',MB_ICONASTERISK and MB_ICONINFORMATION);
end;
end;
end;
===========================================
// 取U盤盤符代碼
使用delphi程序?qū)崿F(xiàn)U盤盤符的獲取其實不是很難,不過對于新手來說可能有點困難,把代碼貼出來,供大家學(xué)習(xí)
============================================
procedure
TForm1.Button1Click(Sender: TObject);
var
buf:array [0..MAX_PATH-1] of char;
m_Result:Integer;
i:Integer;
str_temp:string;
begin
m_Result:=GetLogicalDriveStrings(MAX_PATH,buf);
for i:=0 to (m_Result div
4) do
begin
str_temp:=string(buf[i*4]+buf[i*4+1]+buf[i*4+2]);
if GetDriveType(pchar(str_temp)) = DRIVE_REMOVABLE then
begin
ShowMessage(str_temp+'盤為U盤');
ListBox1.Items.Add(str_temp);
end;
end;
end;
=========================================//磁盤類型常量參考
DRIVE_UNKNOWN = 0;
{$EXTERNALSYM DRIVE_UNKNOWN}
DRIVE_NO_ROOT_DIR =
1;
{$EXTERNALSYM DRIVE_NO_ROOT_DIR}
DRIVE_REMOVABLE = 2;
{$EXTERNALSYM
DRIVE_REMOVABLE}
DRIVE_FIXED = 3;
{$EXTERNALSYM
DRIVE_FIXED}
DRIVE_REMOTE = 4;
{$EXTERNALSYM DRIVE_REMOTE}
DRIVE_CDROM
= 5;
{$EXTERNALSYM DRIVE_CDROM}
DRIVE_RAMDISK = 6;