AutoCAD 3DMAX C语言 Pro/E UG JAVA编程 PHP编程 Maya动画 Matlab应用 Android
Photoshop Word Excel flash VB编程 VC编程 Coreldraw SolidWorks A Designer Unity3D
 首页 > Delphi

用Delphi程序实现对光驱盘盒的开关控制

51自学网 http://www.wanshiok.com

 

  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;

  5)程序自动执行无效的菜单鼠标事件

procedure TForm1.mNotautorunClick(Sender: TObject);
var
 Reg: TRegistry;
begin
 Reg:=TRegistry.Create;
 try
  Reg.RootKey:=HKEY_LOCAL_MACHINE;
  if (Reg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Run',False))=True then
  begin
   // 在注册表中添加数值
   Reg.DeleteValue('MyStartup');
  end
  else
   MessageBox(Handle,'打开注册表失败。','错误',MB_OK+MB_ICONERROR);
  finally
   Reg.CloseKey;
   Reg.Free;
  end;
 end;

  6)打开光驱子菜单的事件过程

procedure TForm1.MenuOpenCdrom(Sender : TObject);
var i:integer;
begin
 with Sender as TMenuItem do begin
  if Menuindex = mOpenCDROM.Count-1 then //判断鼠标是否点击”所有光驱”子菜单项
  begin
   for i := 0 to Menuindex-1 do //打开所有光驱
   begin
    // Menuindex:=i;
    Mycdrom :=pchar(mopenCdrom.Items[i].Caption);
    OpenCdrom(Mycdrom);
   end;
  end else
  begin
   Mycdrom :=pchar(mopenCdrom.Items[Menuindex].Caption);
   OpenCdrom(Mycdrom);
  end;
 end;

  7)关闭光驱子菜单事件过程

procedure TForm1.MenuCloseCdrom(Sender : TObject);
var i:integer;
begin
 with Sender as TMenuItem do begin
  if Menuindex = mCloseCDROM.Count-1 then //判断鼠标是否点击”所有光驱”子菜单项
  begin
   for i := 0 to Menuindex-1 do // //关闭所有光驱
   begin
    Mycdrom :=pchar(mCloseCdrom.Items[i].Caption);
    CloseCdrom(Mycdrom);
   end;
   end else
    Mycdrom :=pchar(mCloseCdrom.Items[Menuindex].Caption);
    CloseCdrom(Mycdrom);
   end;
  end;

  8)关闭控制程序子菜单事件过程:

procedure TForm1.mCloseAppClick(Sender: TObject);
begin
 Application.terminate; //程序终止
end;

  通过上述的函数和过程实现了对光驱的控制,运行以下该程序,用鼠标右键点击所见窗口,弹出图2菜单效果,选择所要控制开关的光驱盘号,显然光驱盒开始听任程序的摆布。该程序可以进一步改造后将其窗体隐去,放入状态栏中,实现程序托盘功能等,由于限于篇幅,将此部分省去,需要程序或源代码的读者可以通过我的EMAIL:hzhyan@sohu.com索要并欢迎交流Delphi的编程经验。

  本程序Windows 2000操作系统+ Delphi 6.0 实现和调试通过。


图2 最终弹出菜单的效果图

 
 

上一篇:Delphi+Word的数据库系统设计  下一篇:用Delphi为窗体的系统菜单中添加选项