?? devicechange.pas
字號:
unit DeviceChange;
interface
uses
Windows, SysUtils, Classes, Controls, Messages, Graphics, Forms, Dialogs,
DBT;
type
TDeviceType = (dtOEM,
dtDEVNODE,
dtVOLUME,
dtPORT,
dtNET,
dtDEVICEINTERFACE,
dtHANDLE);
TDeviceTypes = set of TDeviceType;
TDeviceChangeEvent = procedure (Sender: TObject;
ADeviceType: TDeviceType; APtr: Pointer) of object;
TDeviceChange = class(TComponent)
private
{ Private declarations }
FWindowHandle: HWND;
FDevices: TDeviceTypes;
FDeviceArrival: TDeviceChangeEvent;
FDeviceQueryRemove: TDeviceChangeEvent;
FDeviceQueryRemoveFailed: TDeviceChangeEvent;
FDeviceRemovePending: TDeviceChangeEvent;
FDeviceRemoveComplete: TDeviceChangeEvent;
FDeviceTypeSpecific: TDeviceChangeEvent;
FDeviceCustomEvent: TDeviceChangeEvent;
procedure WndProc(var Msg: TMessage);
procedure SetDevices(v:TDeviceTypes);
protected
{ Protected declarations }
procedure WMDeviceChange(var Msg : TWMDeviceChange); dynamic;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Devices : TDeviceTypes read FDevices write SetDevices;
property Arrival: TDeviceChangeEvent read FDeviceArrival
write FDeviceArrival;
property QueryRemove: TDeviceChangeEvent read FDeviceQueryRemove
write FDeviceQueryRemove;
property QueryRemoveFailed: TDeviceChangeEvent read FDeviceQueryRemoveFailed
write FDeviceQueryRemoveFailed;
property RemovePending: TDeviceChangeEvent read FDeviceRemovePending
write FDeviceRemovePending;
property RemoveComplete: TDeviceChangeEvent read FDeviceRemoveComplete
write FDeviceRemoveComplete;
property TypeSpecific: TDeviceChangeEvent read FDeviceTypeSpecific
write FDeviceTypeSpecific;
property CustomEvent: TDeviceChangeEvent read FDeviceCustomEvent
write FDeviceCustomEvent;
end;
implementation
{**************************** TDeviceChange component *****************************}
constructor TDeviceChange.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FWindowHandle := AllocateHWnd(WndProc);
FDevices := [dtOEM, dtDEVNODE, dtVOLUME, dtPORT, dtNET, dtDEVICEINTERFACE, dtHANDLE];
end;
destructor TDeviceChange.Destroy;
begin
DeallocateHWnd(FWindowHandle);
inherited Destroy;
end;
procedure TDeviceChange.WndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then
try
WMDeviceChange(TWMDeviceChange(Msg));
except
Application.HandleException(Self);
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
procedure TDeviceChange.WMDeviceChange(var Msg : TWMDeviceChange);
var lpdb : PDevBroadcastHdr;
begin
(* received a wm_devicechange message *)
lpdb := PDevBroadcastHdr(Msg.dwData);
case Msg.Event of
DBT_DEVICEARRIVAL :
begin
if Assigned(fDeviceArrival) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceArrival(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_DEVICEQUERYREMOVE :
begin
if Assigned(fDeviceQueryRemove) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceQueryRemove(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_DEVICEQUERYREMOVEFAILED :
begin
if Assigned(fDeviceQueryRemoveFailed) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceQueryRemoveFailed(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_DEVICEREMOVEPENDING :
begin
if Assigned(fDeviceRemovePending) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceRemovePending(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_DEVICEREMOVECOMPLETE :
begin
if Assigned(fDeviceRemoveComplete) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceRemoveComplete(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_DEVICETYPESPECIFIC :
begin
if Assigned(fDeviceTypeSpecific) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceTypeSpecific(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
DBT_CUSTOMEVENT :
begin
if Assigned(fDeviceCustomEvent) and
(TDeviceType(lpdb^.dbch_devicetype) in FDevices) then
FDeviceCustomEvent(Self,
TDeviceType(lpdb^.dbch_devicetype),
Pointer(Msg.dwData));
end;
end;
end;
procedure TDeviceChange.SetDevices(v : TDeviceTypes);
begin
if v <> fDevices then begin
fDevices := v;
end;
end;
end.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -