Unit Unit1;
Interface
Uses
Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms,
Dialogs, AppEvnts, ExtCtrls, StdCtrls, ShellAPI;
Type
TForm1 = Class(TForm)
Button1: TButton;
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;
i: char;
Begin
Case Msg.WParam Of
32768: Begin
ShowMessage('U盘插入');
For i := 'D' To 'Z' Do
If GetDriveType(PChar(i + ':"')) = DRIVE_REMOVABLE Then Begin
ShellExecute(Handle, 'open', 'Explorer.exe', PChar(i + ':"'), Nil, SW_SHOWNORMAL);
End;
End;
32772: Begin
ShowMessage('U盘退出')
End;
End;
End;
End.
Interface
Uses
Windows, Messages, SysUtils, Variants, classes, Graphics, Controls, Forms,
Dialogs, AppEvnts, ExtCtrls, StdCtrls, ShellAPI;
Type
TForm1 = Class(TForm)
Button1: TButton;
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;
i: char;
Begin
Case Msg.WParam Of
32768: Begin
ShowMessage('U盘插入');
For i := 'D' To 'Z' Do
If GetDriveType(PChar(i + ':"')) = DRIVE_REMOVABLE Then Begin
ShellExecute(Handle, 'open', 'Explorer.exe', PChar(i + ':"'), Nil, SW_SHOWNORMAL);
End;
End;
32772: Begin
ShowMessage('U盘退出')
End;
End;
End;
End.