When my control is created I must use DeviceWnd:=AllocateHWnd(DeviceWindowProc);
to receive WM_DEVICECHANGE
message. And then...
procedure TFileList.DeviceWindowProc(var Message: TMessage);
begin
case Message.Msg of
WM_DEVICECHANGE: begin
case Message.WParam of
DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEARRIVAL:
if PDEV_BROADCAST_HDR(Message.LParam).dbch_devicetype = DBT_DEVTYP_VOLUME then
OnDeviceChange;
end;
end;
end;
Message.Result:=DefWindowProc(DeviceWnd, Message.Msg, Message.WParam, Message.LParam);
end;
This works well, but why I don't reveive that message when I do like this ? :
TFileList = class(TCustomControl)
private
procedure DeviceChage(var AMessage:TMessage); message WM_DEVICECHANGE;
end;
procedure TFileList.DeviceWindowProc(var Message: TMessage);
begin
Message.Result:=DefWindowProc(DeviceWnd, Message.Msg, Message.WParam, Message.LParam);
end;
procedure TFileList.DeviceChage(var AMessage:TMessage);
begin
case AMessage.WParam of
DBT_DEVICEREMOVECOMPLETE, DBT_DEVICEARRIVAL:
if PDEV_BROADCAST_HDR(AMessage.LParam).dbch_devicetype = DBT_DEVTYP_VOLUME then
OnDeviceChange;
end;
end;
From the documentation of
RegisterDeviceNotification
:The window that you create with
AllocateHWnd
is a top-level window. Hence it receives the broadcast messages.Your custom control is not a top level window. If you want it to receive messages you will have to call
RegisterDeviceNotification
passing its window handle. If you do that, be sure to deal with VCL window recreation by registering inCreateWnd
and unregistering inDestroyWnd
.As a general rule of thumb,
AllocateHwnd
is the preferred way to listen for notifications. That's because it is not subject to VCL window recreation, and so cannot miss notifications. When a VCL window is being recreated then there is a window of opportunity for notifications to be sent, but your application not having a window ready to receive.That's certainly going to be an issue in your case and so you should use
AllocateHwnd
. You can arrange that the window you create withAllocateHwnd
is owned by your custom control, and then you can route the notification to that control's code.