利用API函数控制光驱的开和关

源代码如下: —————————————————————————— Option Explicit '说明:CDdoor函数是安装WINDOWS时所自带的winmm.dll文件中包含的函数 Private Declare Function CDdoor Lib "winmm.dll" Alias "mciSendStringA" _ (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _ ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long 'CDOpen用来标示光驱开与关的状态 Dim CDOpen As Boolean Private Sub Command1_Click() On Error Resume Next '如果关闭则打开,并且按钮做相应变化 If CDOpen = False Then Call CDdoor("set CDAudio door open", 0, 0, 0) CDOpen = True Command1.Caption = "点击关闭光驱" Else '否则关闭 Call CDdoor("set CDAudio door closed", 0, 0, 0) CDOpen = False Command1.Caption = "点击打开光驱" End If End Sub Private Sub Form_Load() CDOpen = False Call CDdoor("set CDAudio door closed", 0, 0, 0) End Sub 'CDdoor函数是安装WINDOWS时所自带的winmm.dll文件中包含的函数,我们只须先加入如下的声明后就能引用这个API函数: 'Private Declare Function CDdoor Lib "winmm.dll" Alias "mciSendStringA" _   '(ByVal lpstrCommand As String, _       String,这是控制命令参数 'ByVal lpstrReturnString As String, _    String,这是返回值 'ByVal uReturnLength As Long, _       Long,返回值长度参数 'ByVal hwndCallback As Long) As Long '引用的语法是:Call CDdoor("set CDAudio door closed", 0, 0, 0)用以关闭光驱门Call CDdoor("set CDAudio door open", 0, 0, 0)   用以打开光驱门 '程序中使用了一个布尔型变量来标示当前光驱门开与关的状态?如果配合检测光驱是否存在的函数一起使用, '此程序的通用性会更高。而关于检测驱动器信息的函数请参看 GetDriveType,GetLogicalDrives的用法。

你可能感兴趣的:(利用API函数控制光驱的开和关)