To use this example, add one module and a form to your project. In the module you just call the API and some procedures which we are using in this project. In the form place a picture box and insert a picture which we want to display in the system tray. Create a menu which contain a 'Exit' option to exit from the program using menu editor.
Add the following code to the module
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias _
"Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As _
NOTIFYICONDATA) As Long
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
Public Type NOTIFYICONDATA
cbSize As Long
hwnd As Long
uID As Long
uFlags As Long
uCallbackMessage As Long
hIcon As Long
szTip As String * 64
End Type
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4
Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Public Const WM_MOUSEMOVE = &H200
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_RBUTTONUP = &H205
Public Sub CreateIcon()
Dim Tic As NOTIFYICONDATA
Tic.cbSize = Len(Tic)
Tic.hwnd = cdfrm.icopic.hwnd
Tic.uID = 1&
Tic.uFlags = NIF_DOALL
Tic.uCallbackMessage = WM_MOUSEMOVE
Tic.hIcon = cdfrm.icopic.Picture
Tic.szTip = "Easy CD Access" & Chr$(0)
erg = Shell_NotifyIcon(NIM_ADD, Tic)
End Sub
Public Sub DeleteIcon()
Dim Tic As NOTIFYICONDATA
Tic.cbSize = Len(Tic)
Tic.hwnd = cdfrm.icopic.hwnd
Tic.uID = 1&
erg = Shell_NotifyIcon(NIM_DELETE, Tic)
End Sub
Public Sub opencd()
retvalue = mciSendString("set CDAudio door open", _
returnstring, 127, 0)
End Sub
Public Sub closecd()
retvalue = mciSendString("set CDAudio door closed", _
returnstring, 127, 0)
End Sub
Next, paste this code into the Form's code window:
Private Sub mnuExit_Click()
Unload Me
End Sub
Private Sub Form_Load()
CreateIcon
End Sub
Private Sub Form_Unload()
DeleteIcon
End Sub
Private Sub picPic_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Select Case Button
Case vbLeftButton
closecd
Case vbRightButton
cdfrm.PopupMenu cdmenu
End Select
End Sub
Private Sub picPic_DblClick()
opencd
End Sub
Comments