'程序说明:
' 本例使用API函数 CDdoor 来控制光驱门的开和关
'程序十分简单,由于 CDdoor 函数自身包含了对异常
'错误的处理机制,因此这个程序的通用性很高,你可
'以把这段代码移植到你的程序中,实现某些多媒体播
'放器所常用的开关光驱的功能。
'-------------------------------------------
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) 用以打开光驱门