type
TMyCustomControl = class(tcustomcombobox)
private
procedure InitResources_1;
procedure InitResources_2;
protected
procedure CreateWnd; override;
procedure WndProcNew(var Message: TMessage);
public
destructor; Destroy; override;
end;
const
DBT_DEVTYP_DEVICEINTERFACE = $5;
DEVICE_NOTIFY_WINDOW_HANDLE = $0;
DEVICE_NOTIFY_ALL_INTERFACE_CLASSES = $4;
//THIS IS A GUI FOR USB
GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';
type
DEV_BROADCAST_HDR_DEVICE_TYPE = Cardinal;
DEV_BROADCAST_HDR = record
dbch_size: Cardinal;
dbch_devicetype: DEV_BROADCAST_HDR_DEVICE_TYPE;
dbch_reserved: Cardinal;
end;
type
DEV_BROADCAST_DEVICEINTERFACE_W = record
dbcc_size: Cardinal;
dbcc_devicetype: Cardinal;
dbcc_reserved: Cardinal;
dbcc_classguid: TGuid;
dbcc_name: array[0..0] of AnsiChar;
end;
var _dev: Dev_Broadcast_DeviceInterface_W;
var DevBlockRem: DEV_BROADCAST_HDR;
FDevNotify: HDEVNOTIFY;
NewHandle: HWND;
procedure TMyCustomControl.CreateWnd;
begin
inherited CreateWnd;
//These two calls are equivalent like the have constructed here
//USE ONLY ONE OF THEM !!!
//Call this if you want define some type of resources, see documentation
InitResources_1;
//Call this if you want define Hardware resources
//InitResources_2;
end;
procedure TMyCustomControl.InitResources_1;
begin
NewHandle:=AllocateHwnd(WndProcNew);
ZeroMemory(@_dev, sizeOf(Dev_Broadcast_DeviceInterface_W));
with _dev do
begin
dbcc_size:=sizeOf(Dev_Broadcast_DeviceInterface_W);
dbcc_devicetype:= DBT_DEVTYP_DEVICEINTERFACE;
dbcc_reserved:=0;
dbcc_classguid:= GUID_DEVINTERFACE_USB_DEVICE;
dbcc_name:=chr(0);
end;
//Use "DEVICE_NOTIFY_ALL_INTERFACE_CLASSES" if you want catch all messages from all classes (ignore dbcc_classguid)
FDevNotify:=RegisterDeviceNotification(NewHandle, @_dev, DEVICE_NOTIFY_WINDOW_HANDLE or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
Win32Check(FDevNotify <> nil);
end;
procedure TMyCustomControl.InitResources_2;
begin
NewHandle := AllocateHWnd(WndProcNew);
ZeroMemory(@DevBlockRem, sizeOf(DevBlockRem));
DevBlockRem.dbch_size := sizeof(DEV_BROADCAST_HDR)+sizeof(DEV_BROADCAST_DEVICEINTERFACE_W);
DevBlockRem.dbch_devicetype := DBT_DEVTYP_DEVICEINTERFACE;
DevBlockRem.dbch_reserved := 0;
FDevNotify := nil;
FDevNotify := RegisterDeviceNotification(NewHandle, @DevBlockRem, DEVICE_NOTIFY_WINDOW_HANDLE or DEVICE_NOTIFY_ALL_INTERFACE_CLASSES);
Win32Check(FDevNotify <> nil);
end;
destructor TMyCustomControl.Destroy;
begin
if FDevNotify <> nil then
UnregisterDeviceNotification(FDevNotify);
inherited;
end;
procedure TMyCustomControl.WndProcNew(var Message: TMessage);
begin
case Message.Msg of
WM_DEVICECHANGE: //for example
begin
writeln(Message.WParam);
//Message.Result := 0; // Messagge handled
end;
end;
end;