系统设备变化,系统向应用程序发出WM_DEVICECHANGE消息,应用程序注册一个消息处理函数,来获取设备变化通知.
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, AppEvnts, ExtCtrls, StdCtrls; type TForm1 = class(TForm) Label1: TLabel; private { Private declarations } procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.WMDeviceChange (var Msg: TMessage); var myMsg : String; begin Case Msg.WParam of 32768: begin myMsg :='USB设备插入'; Label1.Caption:=myMsg end; 32772: begin myMsg :='USB设备拔出'; Label1.Caption:=myMsg; end; end; end; end.