How to catch WM_DEVICECHANGE in a control other than TForm?

ぃ、小莉子 提交于 2019-12-14 02:33:21

问题


Until today I was using the following code to catch WM_DEVICECHANGE message in application main form and it worked pefectly. But if I try to use this in my custom control I don't get notifyed on device insert or remove. What is happening ?

  TDriveBar = class(TCustomPanel)
  private
    procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;  
  end;

implementation

procedure TDriveBar.WMDeviceChange(var Msg: TMessage);
const DBT_DEVICEARRIVAL = $8000;
      DBT_DEVICEREMOVECOMPLETE = $8004;
      DBT_DEVTYP_VOLUME = 2;

type PDEV_BROADCAST_HDR = ^DEV_BROADCAST_HDR;
     DEV_BROADCAST_HDR = record
      dbch_size: dword;
      dbch_devicetype: dword;
      dbch_reserved: dword;
     end;

begin
 case Msg.WParam of

  DBT_DEVICEREMOVECOMPLETE:
   if PDEV_BROADCAST_HDR(Msg.LParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME then UpdateDrives;

  DBT_DEVICEARRIVAL:
   if PDEV_BROADCAST_HDR(Msg.LParam)^.dbch_devicetype = DBT_DEVTYP_VOLUME then UpdateDrives;

 end;
end;

回答1:


The OS sends wm_DeviceChange messages to all top-level windows. The application's main form is a top-level window, but your control is not, which is why the form receives the messages and your control does not.

For arbitrary device types, you have two alternatives:

  1. Use AllocateHWnd to create a message-only top-level window that will respond to messages by calling a function associated with your control. This will give you the same basic information as the main form receives.

    Write a method for your control that matches the signature for TWndMethod, which is what AllocateHWnd requires. It might look like this:

    procedure TDriveBar.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
                UpdateDrives;
          end;
        end;
      end;
      Message.Result := DefWindowProc(FDeviceWnd, Message.Msg, Message.WParam, Message.LParam);
    end;
    

    Then use that method when you create the message window:

    FDeviceWnd := AllocateHWnd(DeviceWindowProc);
    
  2. Call RegisterDeviceNotification to tell the OS that your control's window wants to receive notifications, too. (Make sure you handle your control's CreateWnd and DestroyWnd methods so that if your control is re-created, you renew the notification registration with the control's new window handle.) This will give you more detailed information than the default wm_DeviceChange message provides, but only for the kinds of devices you specify when you register your window handle.

However, you're interested in changes to volumes. The remarks for RegisterDeviceNotification have something to say about that (emphasis added):

The DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE events are automatically broadcast to all top-level windows for port devices. Therefore, it is not necessary to call RegisterDeviceNotification for ports, and the function fails if the dbch_devicetype member is DBT_DEVTYP_PORT. Volume notifications are also broadcast to top-level windows, so the function fails if dbch_devicetype is DBT_DEVTYP_VOLUME.

That eliminates that notification registration as an option for you, so the only solution in your case is to use AllocateHWnd.



来源:https://stackoverflow.com/questions/30711942/how-to-catch-wm-devicechange-in-a-control-other-than-tform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!