相关疑难解决方法(0)

如何在TForm以外的控件中捕获WM_DEVICECHANGE?

直到今天,我使用以下代码来捕获WM_DEVICECHANGE应用程序主窗体中的消息,并且它完美地工作.但是如果我尝试在我的自定义控件中使用它,我不会在设备插入或删除时收到通知.怎么了 ?

  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;
Run Code Online (Sandbox Code Playgroud)

delphi delphi-2009 windows-messages

1
推荐指数
1
解决办法
1462
查看次数

标签 统计

delphi ×1

delphi-2009 ×1

windows-messages ×1