程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 用Delphi實現對光驅盤盒的開關控制(二)

用Delphi實現對光驅盤盒的開關控制(二)

編輯:Delphi

2)打開CDROM盒的函數 function OpenCDROM(Drive:pChar):Boolean; // 打開CDROM
var
 Res:MciError;
 OpenParm:TMCI_OPEN_Parms;
 Flags:Dword;
 s:string;
 DeviceID:Word;
begin
 Result:=false;
 s:=Drive+':';
 flags:=mci_Open_Type or mci_Open_Element;
 With OpenParm do
 begin
  dwCallBack:=0;
  lpstrDeviceType:='CDAudio';
  lpstrElementName:=PChar(s);
 end;
 Res:=mciSendCommand(0,mci_Open,Flags,Longint(@OpenParm));
 If Res<>0 then exit;
 DeviceID:=OpenParm.wDeviceID ;
 try
  Res:=mciSendCommand(DeviceID,MCI_SET,MCI_SET_DOOR_OPEN,0);
  If Res=0 then exit;
  Result:=True;
 finally
  mciSendCommand(DeviceID,mci_Close,Flags,Longint(@OpenParm));
 end;
end;
  3)關閉CDROM盒的函數

function CloseCDROM(Drive:pChar):Boolean; // 關閉CDROM
var
 Res:MciError;
 OpenParm:TMCI_OPEN_Parms;
 Flags:Dword;
 s:string;
 DeviceID:Word;
 begin
  Result:=false;
  s:=Drive+':';
  flags:=mci_Open_Type or mci_Open_Element;
  With OpenParm do
  begin
   dwCallBack:=0;
   lpstrDeviceType:='CDAudio';
   lpstrElementName:=PChar(s);
  end;
  Res:=mciSendCommand(0,mci_Open,Flags,Longint(@OpenParm));
  If Res<>0 then exit;
  DeviceID:=OpenParm.wDeviceID ;
  try
   Res:=mciSendCommand(DeviceID,MCI_SET,MCI_SET_DOOR_CLOSED,0);
   If Res=0 then exit;
   Result:=True;
  finally
   mciSendCommand(DeviceID,mci_Close,Flags,Longint(@OpenParm));
  end;
 end;
  4)置程序啟動時執行菜單鼠標事件

procedure TForm1.mAutorunClick(Sender: TObject);
var
 Reg: TRegistry;
begin
 if Application.ExeName='' then // 判斷應用程序文件名是否為空
 begin
  MessageBox(Handle,'應用程序名稱不可以為空。','錯誤',MB_OK+MB_ICONERROR);
  Exit;
 end;
 // 初始化AppFileName
 //GetMem(Application.ExeName,256);
 // edit1.text.GetTextBuf(AppFileName,256);
 Reg:=TRegistry.Create;
 try
  Reg.RootKey:=HKEY_LOCAL_MACHINE;
  if (Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False))=True then
  begin
   // 在注冊表中添加數值
   Reg.WriteString('MyStartup',Application.ExeName);
  end
  else
   MessageBox(Handle,'打開注冊表失敗。','錯誤',MB_OK+MB_ICONERROR);
  finally
   Reg.CloseKey;
   Reg.Free;
  end;
 end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved