以下是源代碼:
-------------------------------------------
利用API函數(shù)控制光驅(qū)的開和關(guān)
-------------------------------------------
程序說明:
本例使用API函數(shù) CDdoor 來控制光驅(qū)門的開和關(guān)
程序十分簡單,由于 CDdoor 函數(shù)自身包含了對異常
錯誤的處理機制,因此這個程序的通用性很高,你可
以把這段代碼移植到你的程序中,實現(xiàn)某些多媒體播
放器所常用的開關(guān)光驅(qū)的功能。
-------------------------------------------
Option Explicit
說明:CDdoor函數(shù)是安裝WINDOWS時所自帶的winmm.dll文件中包含的函數(shù)
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用來標(biāo)示光驅(qū)開與關(guān)的狀態(tài)
Dim CDOpen As Boolean
Private Sub Command1_Click()
On Error Resume Next
如果關(guān)閉則打開,并且按鈕做相應(yīng)變化
If CDOpen = False Then
Call CDdoor("set CDAudio door open", 0, 0, 0)
CDOpen = True
Command1.Caption = "點擊關(guān)閉光驅(qū)"
Else
否則關(guān)閉
Call CDdoor("set CDAudio door closed", 0, 0, 0)
CDOpen = False
Command1.Caption = "點擊打開光驅(qū)"
End If
End Sub
Private Sub Form_Load()
CDOpen = False
Call CDdoor("set CDAudio door closed", 0, 0, 0)
End Sub
CDdoor函數(shù)是安裝WINDOWS時所自帶的winmm.dll文件中包含的函數(shù),我們只須先加入如下的聲明后就能引用這個API函數(shù):
Private Declare Function CDdoor Lib "winmm.dll" Alias "mciSendStringA" _
( _
ByVal lpstrCommand As String, _ String,這是控制命令參數(shù)
ByVal lpstrReturnString As String, _ String,這是返回值
ByVal uReturnLength As Long, _ Long,返回值長度參數(shù)
ByVal hwndCallback As Long _
) As Long
引用的語法是:Call CDdoor("set CDAudio door closed", 0, 0, 0) 用以關(guān)閉光驅(qū)門 Call CDdoor("set CDAudio door open", 0, 0, 0) 用以打開光驅(qū)門
程序中使用了一個布爾型變量來標(biāo)示當(dāng)前光驅(qū)門開與關(guān)的狀態(tài)。
如果配合檢測光驅(qū)是否存在的函數(shù)一起使用,此程序的通用性會更高。而關(guān)于檢測驅(qū)動器信息的函數(shù)請參看 GetDriveType,GetLogicalDrives的用法。