Delphi程序中弹出和关闭CD-ROM

要在Delphi程序中控制CD-ROM的弹出和关闭,可以利用MMSystem.pas单元的MCI(Media Control Interface)函数。有2个函数可以达到同样的效果。一个是mciSendCommand函数,另一个是mciSendString函数。下面分别用这2个函数写的OpenCDDrive函数。

1、使用mciSendCommand:

functionOpenCDDrive(IsOpen:Boolean;DriveName: char = # 0 ):Boolean;
var
op:TMCI_Open_Parms;
flags:LongWord;
begin
FillChar(op,Sizeof(TMCI_Open_Parms),
0 );
op.lpstrDeviceType:
= PChar(MCI_DEVTYPE_CD_AUDIO);
flags:
= MCI_OPEN_TYPEorMCI_OPEN_TYPE_ID;
if DriveName <> # 0 then
begin
op.lpstrElementName:
= PChar(DriveName + ' :');
flags: = flagsorMCI_OPEN_ELEMENT;
end;
Result:
= mciSendCommand( 0 ,MCI_OPEN,flags,LongWord(@op)) = 0 ;
if notResultthenExit;
if IsOpenthen
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_OPEN,
0 )
else
mciSendCommand(op.wDeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,
0 );
mciSendCommand(op.wDeviceID,MCI_CLOSE,MCI_WAIT,
0 );
end;

2、使用mciSendString:

functionOpenCDDrive(IsOpen:Boolean;DriveName: char = # 0 ):Boolean;
var
s:
string ;
begin
if IsOpenthens: = ' open '
else s: = ' closed ' ;
if DriveName = # 0 then
Result:
= mciSendString(PChar( ' setcdaudiodoor ' + s),nil, 0 , 0 ) = 0
else
begin
Result:
= mciSendString(PChar( ' open ' + DriveName + ' :typecdaudioaliascdrom ' ),nil, 0 , 0 ) = 0 ;
ifResult then
begin
mciSendString(PChar(
' setcdromdoor ' + s),nil, 0 , 0 );
mciSendString(
' closecdrom ' ,nil, 0 , 0 );
end;
end;
end;

OpenCDDrive函数有2个参数,IsOpen为True弹出CD-ROM,False关闭RD_ROM;DriveName为CD-ROM驱动器名称,缺省为#0。返回True成功,否则失败。

打开和关闭缺省CD-ROM,即第一个CD-ROM驱动器可以分别用:

OpenCDDrive(True);和OpenCDDrive(False);

使用CD-ROM驱动器名称调用可以分别为(假设CD-ROM驱动器名称为“F:”):

OpenCDDrive(True, 'F');和OpenCDDrive(False, 'F');

如有错误请指正:[email protected]

你可能感兴趣的:(F#,Delphi)