Open And Close CD Tray
Note: I use Visual Basic Enterprise 6 for this tutorial.
Hey there! this is just a simple visual basic tutorial for those of you who have problems with opening and closing your CD tray.
Common Uses:
1. too lazy to bend over and open it yourself.
2. its stuck for an unknown reason.
First off open up your Visual Basic program
and create a new Standard EXE
After doing so you will want to grab the Command Button tool and on the form draw a random button.
Off to the side in the
Properties menu select the box to the right of (name) and rename it cmdOpen. Then in the box to the right of Caption replace it with Open Tray!
Or you can use this image to help you!
You will want to do that again but instead of Open Tray and cmdOpen you will want to use Close Tray and cmdClose.
You an also add another command button and name it cmdPClose with the caption of Close Program.
Now what you want to do is double click on the cmdOpen command button use the following code
Code
mciSendString "set cdaudio door open", 0, 0, 0 'Open the CD Tray
MsgBox "Tray Opened!" 'say the cd tray was opened
So far the code that the code window should show is:
Code
Private Sub cmdOpen_Click()
mciSendString "set cdaudio door open", 0, 0, 0 'Open the CD Tray
MsgBox "Tray Opened!" 'say the cd tray was opened
End Sub
Next, double click the Close Tray button and add:
Code
mciSendString "set cdaudio door closed", 0, 0, 0 'Close Tray
msgbox "Tray Closed!"
With that done theres really only 1 thing to do which is the cmdPClose command button.
With that, double click Close Program button and add:
Code
Me.Hide 'Close the program
MsgBox "Goo Bye!" 'Good bye ^^
That should be it But before you go to compile you will want to add one last code to the top of the document that will make this program work for real which is:
Code
Option Explicit
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
That should go above ALL OTHER CODING or you may get an error.
When thats done feel free to add graphics or other random things then click F5 to test.
There is a download in the downloads section of the website with a preview of this :)
Hope you learned something!