【Delphi】偵測光碟機是否有放入光碟片

以下的程式碼, 會在置入及退出光碟片時出現提示視窗

type  
    TForm1 = class(TForm)
    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);
const 
    CD_IN = $8000;
    CD_OUT = $8004;
var
    Msgstr : string;
begin
    inherited;
    case Msg.wParam of
        CD_IN : Msgstr := '置入光碟片';
        CD_OUT : Msgstr := '退出光碟片';
    end;
    ShowMessage(Msgstr) ;
end;