Search code examples
delphidelphi-2009message

Why I don't receive messages in my control when I use "message" directive?


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;

Solution

  • From the documentation of RegisterDeviceNotification:

    Applications send event notifications using the BroadcastSystemMessage function. Any application with a top-level window can receive basic notifications by processing the WM_DEVICECHANGE message. Applications can use the RegisterDeviceNotification function to register to receive device notifications.

    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 in CreateWnd and unregistering in DestroyWnd.

    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 with AllocateHwnd is owned by your custom control, and then you can route the notification to that control's code.