Tag Archives: tray

How to Open and Close the CDRom/DVDRom Tray Door using VB and VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (18 votes, average: 4.00 out of 5)

Opening or closing the optical disk drive(ODD) tray door in any Visual Basic and Visual Basic.NET language is easy to do thanks to the Windows API. This article will be using code based on the MCISendString Api which I made a complete tutorial for starting MCI. Click this link to check it out.

These codes are setup for VB.NET but all you have to do is change Integers to Longs which will then work pefectly fine with Visual Basic 6.0.

So the first thing you’ll want is this API call…


    'Api call to send the commands to the mci device
    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As _
        String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As _
        Integer) As Integer

VB 6.0

    'Api call to send the commands to the mci device
    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


Now just need to send the commands.

The code below will open and close the default CD/DVD Door and you won’t need to set/specify the ODD drive path.

'Pop it open
mciSendString("set cdaudio door open", 0, 0, 0)

'Shut it
mciSendString("set cdaudio door closed", 0, 0, 0)



Use the codes below will setup and use the variables and the specified alias you want to use.

        'This will simply get the api call return value that can be used if you use the MCIGetErrorString API.
        'This example won’t be using that API though.
        Dim retVal As Integer

        'This will contain the drive you want to open. Ex: D:\ or E:\ or whatever.
        Dim cdDrive As String
        'Now setup the path to your computers cd or dvd drive you want to open or close.

        'Remember to put Quotes around any path you pass to the mci device.
        'Also specify the correct drive letter for the CD/DVD Drive.
        'I’m using drive “D:” which is the location of the DVD/CDRom on my computer.
        'You can specify the drive path like ‘D:’ or ‘D:\’
        cdDrive = Chr(34) & "d:" & Chr(34)

        'Now its time to setup the MCI Device using the CDAudio driver.

        'First setup a new device using the CDAudio driver with alias named “cd”.
        retVal = mciSendString("open " & cdDrive & " type cdaudio alias cd wait shareable", 0, 0, 0)

        'Now that the MCI Device is programmed and ready to go, you can start sending it commands.
        'The codes below will Open the CD or DVD Tray Door and Close the CD/DVD Door…

        'This will Open the door.
        retVal = mciSendString("set cd door open", 0, 0, 0)

        'This will Close the door.
        retVal = mciSendString("set cd door closed", 0, 0, 0)

Thats all there is to it! If the code executed properly, your optical drive should have opened and/or closed. The code here will work with all VBs and VB.NET languages.

Just Change all Integer types to Long types if your using VB 6.0.

If your wanting to make a complete CD Player application, I have complete libraries for both VB 5/6 and Visual Basic.NET at my Visual Basic Code Source website. Anyways, have fun!


How to Display Text in the System Tray/Notify Icon in VB.NET and Higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 3.56 out of 5)

You’ve probably seen some applications that are able to display what looks like text in the notify icon system tray. You may have noticed that the NotifyIcon control we use in VB.NET has no feature for displaying actual text in the tray So, how could you show text in the icon tray like other applications do? A simple little trick will display your icon as text.

More Info…

You actually have to make a bitmap in memory, draw the text you want to display, and use that bitmap you made as the icon to show in the system tray or notify area as some call it. This article has full sample code to accomplish this using Visual Basic.NET, Visual Basic 2008, VB 2010, and Visual Basic 2013. You can also download an example for doing this by clicking here.

Unfortunately there is only about 16×16 pixels of space for your icon text. So it will definitely limit the amount of text you can show in each spot. You should experiment with different Font Types and Font Sizes to get the best results. I also recommend you set the TextRenderingHint property to a higher quality setting like AntiAliasing which could make the text look much better compared to the default rendering hint. Also try to fine tune the position of the text drawn on your Icon to make full use of the icon area.

Getting Started…

The first thing you want to do once you open up your project is add a NotifyIcon component to your project from the Toolbox. I named mine “notifyText”. You can also add a context menu to be used during a Right Mouse Click. Go to the Notify Icon Properties and under the ContextMenu option select the context menu if you added one. If you want, under the ‘Text’ property you can set the Tooltip Text for when the Mouse is hovering over the Notify Icon in the System Tray. There are also some events for the Notify Icon if you want to make use of them like _MouseDown, _DoubleClick, etc.

Visual Basic.NET, Visual Basic 2008, VB 2010, and Higher Source Code…

The first code is a “DestroyIcon” API Function I use to completely destroy/remove the icon from existance when i’m done with it. IF you do get a generic gdi+ error (or it may just say a Generic Error has Occured? I don’t remember), then try using this API call to kill the Icon you created in memory. I made an app that will display and update the cpu clockspeed/usage in the tray update every second or so and that Generic Error would popup every once in awhile and not really say much about it except it had something to do with something in the icon code. This function is good to use anyways to release the resources used by the icon.

    'I'm using this in the createTextIcon sub to releases all of the resources that the icon/icons would have used.
    Public Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Int32) As Int32

Remember to optimize your text icon as much as you can so you can take full advantage of the limited space you have available for clarity. Set the “System.Drawing.Text.TextRenderingHint” to “System.Drawing.Text.TextRenderingHint.AntiAlias” to get smoother and easier to read text. You should setup the font and X/Y coordinates to make full use of the small space. And of course select a brush color that the user would be able to see well.

The code here is the declaration based code for variables and such…

    'You should fine tune the font you want to use so the user can see the text you want them to see.
    'Certain Fonts will obviously display your text better than other fonts might.
    Dim fontToUse As Font = New Font("Microsoft Sans Serif", 8, FontStyle.Regular, GraphicsUnit.Pixel)

    'A basic brush with a Dark Blue Color. This should show up pretty well in the icon tray if the user uses the default tray color.
    Dim brushToUse As Brush = New SolidBrush(Color.DarkBlue)

    'A bitmap used to setup how the icon will display.
    Dim bitmapText As Bitmap = New Bitmap(16, 16)

    'A simply Grahics object to draw the text to a bitmap.
    Dim g As Graphics = Drawing.Graphics.FromImage(bitmapText)

    'Will be used to get the Handle to the bitmap Icon.
    Dim hIcon As IntPtr

The code below will draw the text onto the bitmap image and set the notify icon to display it by using its handle.

    'This code was actually taken from my CPUMonLite Application which displays the processors clockspeed in
    'the system tray. But the clockspeed code is removed
    Sub createTextIcon()


            'Clear any previous ‘stuff’ instead of creating a new bitmap.

            'Setup the text, font, brush, and position for the system tray icon. For the font type and
            'size I used, a good position for the X coordinate is a -1 or -2. And the Y coordinate seems
            'to work well at a 5.
            'You specify the actual text you want to be displayed in the draw string parameter that you
            'want to display in the notify area of the system tray. You will only be able to display a
            'few characters, depending on the font, size of the font, and the coordinates you used.
            g.DrawString("Text!", fontToUse, brushToUse, -2, 5)

            'Get a handle to the bitmap as a Icon.
            hIcon = (bitmapText.GetHicon)

            'Display that new usage value image in the system tray.
            notifyText.Icon = Drawing.Icon.FromHandle(hIcon)

            'Added this to try and get away from a rare Generic Error from the code above. Using this API Function seems to have stopped that error from happening.

        Catch exc As Exception

            MessageBox.Show(exc.InnerException.ToString, "Somethings not right?", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub


The code from this article in use!


Now all you have to do is call that Sub above each time you want the text to change/update in the System Tray! Hopefully I made some sense in this article and you were able to follow ok. Have fun!


Minor Revision: 2014