在VB6.0的開發環境中,添加一個按鈕,編寫如下代碼:
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
Dim CDOpen As Boolean
Private Sub cmdControl_Click()
On Error Resume Next
''如果關閉則打開,並且按鈕做相應變化
If CDOpen = False Then
Call CDdoor("打開", 0, 0, 0)
CDOpen = True
cmdControl.Caption = "點擊關閉光驅"
Else
''否則關閉
Call CDdoor("關閉", 0, 0, 0)
CDOpen = False
cmdControl.Caption = "點擊打開光驅"
End If
End Sub
Private Sub Form_Load()
CDOpen = False
Call CDdoor("關閉光驅", 0, 0, 0)
End Sub
//注:F5運行,就可以控制光驅了。本示例在Win2003,VB6.0環境下運行提通過。