procedure TCDEvents.WndProc(var Msg: TMessage);
begin
if (Msg.Msg = WM_DEVICECHANGE) then begin
if FEnabled and (fNotifyMode = nmEvent) then
try
WMDeviceChange(TWMDeviceChange(Msg));
except
Application.HandleException(Self);
end
end
else
Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;
function TCDEvents.GetFirstDriveLetter(unitmask : longint):char;
var DriveLetter : shortint;
begin
DriveLetter := Ord('A');
while (unitmask and 1)=0 do begin
unitmask := unitmask shr 1;
inc(DriveLetter);
end;
Result := Char(DriveLetter);
end;
procedure TCDEvents.WMDeviceChange(var Msg : TWMDeviceChange);
var lpdb : PDEV_BROADCAST_HDR;
lpdbv : PDEV_BROADCAST_VOLUME;
begin
(* received a wm_devicechange message *)
lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
(* look at the event send together with the wm_devicechange message *)
case Msg.Event of
DBT_DEVICEARRIVAL : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
if Assigned(fAfterArrival) then
fAfterArrival(Self,
GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
end;
end;
DBT_DEVICEREMOVECOMPLETE : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
if Assigned(fAfterRemove) then
fAfterRemove(Self,
GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
end;
end;
end;
end;