2006. május 4., csütörtök

CDROM notification (CDROM inserted or ejected)


Problem/Question/Abstract:

When the user inserts a CD in Drive E:, my application should do something. How can I get notified about a new CD being inserted?

Answer:

When a new CD is inserted, Windows posts to all processes the WM_DEVICECHANGE message. Its wParam value contain the information you need:

OUTPUT -> $8004;
INSERT -> $8000;

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure CDROM_Notification(var msg: TMessage);
      message WM_DEVICECHANGE;
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  CD_OUTPUT = $8004;
  CD_INPUT = $8000;

procedure TForm1.CDROM_Notification(var msg: TMessage);
begin
  if msg.wParam = CD_INPUT then
    ShowMessage('There is a new CD.')
  else if msg.wParam = CD_OUTPUT then
    ShowMessage('CD was ejected.');
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése