摘自UNDO的专家文档!
object Form1: TForm1
Left = 200
Top = 110
Width = 441
Height = 81
Caption = 'Form1'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object ComboBox1: TComboBox
Left = 272
Top = 16
Width = 145
Height = 21
Style = csDropDownList
ItemHeight = 13
TabOrder = 0
OnChange = ComboBox1Change
end
object MediaPlayer1: TMediaPlayer
Left = 12
Top = 8
Width = 253
Height = 30
DeviceType = dtCDAudio
Shareable = True
TabOrder = 1
end
end
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, MPlayer;
type
TForm1 = class(TForm)
ComboBox1: TComboBox;
MediaPlayer1: TMediaPlayer;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//*****************************************************************************
// Returns a string, made of all CDROM drive letters in the system.
//*****************************************************************************
Function CDList : String;
Var
Drives : Integer;
Loop : Integer;
Work : String;
begin
//*****************************************************************************
// Get a map of the drives. Bit 0= A, Bit 25=Z. If the bit is set, the drive
// is present on the system.
//*****************************************************************************
Drives := GetLogicalDrives;
Result := '';
For Loop := 0 To 25do
begin
//*****************************************************************************
// For each drive in the system, check if it is a CDROM, if so, all the letter
// to the result string.
//*****************************************************************************
If (((1 Shl Loop) And Drives)<>0) then
begin
Work := Char(65+Loop)+':/';
If (GetDriveType(PChar(Work))=DRIVE_CDROM) then
begin
Result := Result+Char(65+Loop);
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
Var
CDs : String;
Loop : Integer;
begin
//*****************************************************************************
// Get a list of CD drives.
//*****************************************************************************
CDs := CDList;
ComboBox1.Clear;
//*****************************************************************************
// Fill a combo box with a list of drives. I append : to the drive letter
// here, to make working with the TMediaPlayer later that much easier.
//*****************************************************************************
For Loop := 1 To Length(CDs)do
begin
ComboBox1.Items.Add(Copy(CDs,Loop,1)+':');
end;
end;
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
//*****************************************************************************
// DeviceID is 0 when the device is not OPEN. If it is open, we need to
// stop and close it. This isn't required will all CD players (ie multi tray
// players like the NEC 4x4), but if you have multiple physical CDs, like I
// have, then
it is required.
//*****************************************************************************
If (MediaPlayer1.DeviceId<>0) then
begin
MediaPlayer1.Stop;
MediaPlayer1.Close;
end;
//*****************************************************************************
// Here is the magic that makes the mediaplayer use a different drive.
//*****************************************************************************
MediaPlayer1.FileName := ComboBox1.Text;
//*****************************************************************************
// Obviously no good without openning it!
//*****************************************************************************
MediaPlayer1.Open;
end;
end.