|
|||||||
|
|
|
|||||
|
|
|||||||
[Sample Mission]
This module includes the most basic functions for using the
system tray.
[Usage]
____________________________
frmMain.frm:
ComboBox: Combo1
____________________________
frmSysTray.frm:
Menu: mnuSysTray
mnuOpen &Open
mnuExit E&xit
ImagList: ilsStatusIcons
Icon1 Statu1
Icon2 Status2
____________________________
[Customize]
[Quick Use]
1. The status will be set as status 0 when starting automatically. 2. Click the Combobox to select 1 ot 2 to change into another status. You can find a system tray icon appears in the tray. 3. Minimize the main window. 4. Click or bouble click the tray icon to restore the main window 5. Right click the tray icon to select 'open' also to restore the main window. 6. Choose status 0 to delect the tray icon. That's to say the application is running in status 0. 7. Cose the main window or right click the tray icon to exit the application.
[NOTES]
1. To delete the tray icon when exiting, you'd better use below codes:
Unload frmSysTray
2. This form use below codes to change the tray status:
frmSysTray.CurTrayStatus = status
3. This form use a skill to hide the main window when minimized at #HIDEWINDOW in Form_Resize()
[Other Variations]
[Codes as following]
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'+++++ +++++'
'+++++ FUNCTIONS +++++'
'+++++ +++++'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'''''''''''''''''''''''
''' Event Functions '''
''' '''
'''''''''''''''''''''''
Private Sub Form_Load()
Combo1.ListIndex = 0
End Sub
Private Sub Form_Resize()
If Me.WindowState = vbMinimized Then '#HIDEWINDOW
'To hide me when minimized, if not STATUS #0
If frmSysTray.CurTrayStatus <> 0 Then Me.Hide
End If
End Sub
Private Sub Combo1_Click()
Dim status As Integer
status = Left$(Combo1.Text, 1)
frmSysTray.CurTrayStatus = status
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload frmSysTray
End Sub
'''''''''''''''''''''''''''''''
''' Usage of frmSysTray.frm '''
''' '''
'''''''''''''''''''''''''''''''
'[Mission]
'This module demonstrates how to use the tray icons to show the application's running status.
'
'
'[Customize]
'1. System Tray Status, Icons and Tips
' A). Decide how many status you want you application has. Then set the fintTotalTrayStatus constant
' to suit the user's application requirement at #TotalStatus. (it's default 2 at here)
'
' B). Select icons (16 X 16) for each status except of status 0 that has no any icon. Add those
' icons into the ilsStatusIcons ImageList control in this form. (there are two demo icons already)
'
' C). Choose property words to descript each status in its icon's tip string.
'
' D). Modify some codes related the tray status at #TRAYSTATUS
' a. If need, modify the system tray flag (default is NIF_ICON Or NIF_TIP Or NIF_MESSAGE)
' b. Add more 'Case' to implement what to do in every tray status.
' c. Modify each status tips (default is 'STATUS#n')
'
'2. System Tray Popup-menu
' A). Add some menuitems you need into this form's menu: mnuSysTray
' B). Add or Edit the codes where at #MENU
'
'3. How to activate system tray
' Edit the codes where at #ACTIVATE. (Default way is by both MOUSE_CLICK and MOUSE_DBCLICK)
'4. If need, Modify the error number and description at #INVALID_VALUE_CurTrayStatus.
'
'[Quick Use]
'1. Set/Get the current tray status by the property of CurTrayStatus:
' 0: STATUS #0 no any ICON
' 1: STATUS #1 Show tray icon1 and tip as 'STATUS #1'
' 2: STATUS #2 Show tray icon2 and tip as 'STATUS #2':
'
'2. Get the total tray status by the property of TotalTrayStatus
'
'3. Use the following codes to END you application:
' Unload frmSysTray
' this function will unload all form in you application and delete the tray icon.
'
'
'[NOTES]
'1. This form MUSt work with the mdlSysTray module.
'2. You'd better use the form 'frmMain' as the main form of you application. (See the codes in frmMain.frm for details)
'
'
'[Other Variations]
'
'
'[See Also]
'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'+++++ +++++'
'+++++ MODULE LEVEL VARIABLES, CONSTANTS, DECLARATIONS, +++++'
'+++++ +++++'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'''''''''''''''
''' Public '''
''' '''
'''''''''''''''
'''''''''''''''
''' Private '''
''' '''
'''''''''''''''
Const fintTotalTrayStatus As Integer = 2 '#TotalStatus: MUST be set to suit the user's application before calling this form!
Private fintCurTrayStatus As Integer
'Consts for calling the system tray functions
Const NIF_MESSAGE = &H1
Const NIF_ICON = &H2
Const NIF_TIP = &H4
'Consts for handling the system tray mouse click events
Const WM_MOUSEMOVE = &H200
Const WM_LBUTTONDOWN = &H201 'Button down
Const WM_LBUTTONUP = &H202 'Button up
Const WM_LBUTTONDBLCLK = &H203 'Double-click
Const WM_RBUTTONDOWN = &H204 'Button down
Const WM_RBUTTONUP = &H205 'Button up
Const WM_RBUTTONDBLCLK = &H206 'Double-click
'Declarations
Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'+++++ +++++'
'+++++ FUNCTIONS +++++'
'+++++ +++++'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'''''''''''''''''''''
''' COM Functions '''
''' '''
'''''''''''''''''''''
Public Property Let CurTrayStatus(newValue As Integer)
If newValue >= 0 And newValue <= fintTotalTrayStatus Then
Call SetTrayStatus(newValue)
Else
'#INVALID_VALUE_CurTrayStatus: in this sample, set the error number as 100 and display below error description.
' User may selects another.
Err.Raise vbObjectError + 100, "PDVB5_SyatemTray.frmSysTray", _
"Invalid CurTrayStatus value! " & Chr$(13) & _
"CurTrayStatus must be the value between 0 to the TotalTrayStatus."
End If
End Property
Public Property Get CurTrayStatus() As Integer
CurTrayStatus = fintCurTrayStatus
End Property
Public Property Get TotalTrayStatus() As Integer
TotalTrayStatus = fintTotalTrayStatus
End Property
Private Sub RestoreMainFormWindow()
Dim result As Long
frmMain.WindowState = vbNormal
frmMain.Show
result = SetForegroundWindow(frmMain.hwnd)
End Sub
Private Sub UnloadAllForms()
'Unload all forms when exiting, except Forms(0) that is usually ME.
Dim i As Integer
For i = Forms.Count - 1 To 1 Step -1
Unload Forms(i)
Next
End Sub
'''''''''''''''''''''''
''' Event Functions '''
''' '''
'''''''''''''''''''''''
'This procedure receives the callback from the System Tray icon.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As _
Single, Y As Single)
''''''''''''''''''''''''''''''''''''''''''
''' CallBackMessage of the System Tray '''
''' '''
''''''''''''''''''''''''''''''''''''''''''
Dim msg As Long
'The value of X will vary depending upon the scalemode setting
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg '#ACTIVATE
Case WM_LBUTTONUP '&H202 (514)
Call RestoreMainFormWindow
Case WM_LBUTTONDBLCLK '&H203 (515)
Call RestoreMainFormWindow
Case WM_RBUTTONUP '&H205 (517)
'display popup menu
Me.PopupMenu Me.mnuSysTray
End Select
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
'This removes the icon from the system tray
Me.CurTrayStatus = 0
Call UnloadAllForms
End Sub
'Below are for menu event handling functions
Private Sub mnuOpen_Click() '#MENU
Call RestoreMainFormWindow
End Sub
Private Sub mnuExit_Click() '#MENU
Unload Me
End Sub
'''''''''''''''''''''''''
''' Private Functions '''
''' '''
'''''''''''''''''''''''''
'Private Sub Form_Initialize()
' fintCurTrayStatus = 0 'Set the tray status to 0 when this form is starting
'End Sub
Private Sub SetTrayStatus(newTrayStatus As Integer)
On Error GoTo Err_SetTrayStatus
Dim flg As Long, callbackmsg As Long, icn As Long
Dim tip As String
flg = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
callbackmsg = WM_MOUSEMOVE
Select Case newTrayStatus '#TRAYSTATUS: Here you can add tray status or customize their handling codes.
Case 0
'...
If fintCurTrayStatus <> 0 Then Call DeleteTrayIcon
Case 1
'...
icn = Me.ilsStatusIcons.ListImages("Status1").Picture
tip = "STATUS #1"
If fintCurTrayStatus <> 0 Then
Call ChangeTrayIcon(Me, flg, callbackmsg, icn, tip)
Else
Call AddTrayIcon(Me, flg, callbackmsg, icn, tip)
End If
Case 2
'...
icn = Me.ilsStatusIcons.ListImages("Status2").Picture
tip = "STATUS #2"
If fintCurTrayStatus <> 0 Then
Call ChangeTrayIcon(Me, flg, callbackmsg, icn, tip)
Else
Call AddTrayIcon(Me, flg, callbackmsg, icn, tip)
End If
Case Else
'...
End Select
fintCurTrayStatus = newTrayStatus 'Update current tray status
Exit Sub
Err_SetTrayStatus:
MsgBox Err.Description
End Sub
''''''''''''''''''''''''''''''''''
''' Usage of mdlSysTray.bas '''
''' '''
''''''''''''''''''''''''''''''''''
'[Mission]
'This module includes the most basic functions for using the system tray.
'
'
'[Customize]
'
'
'[Quick Use]
'1. Add a new icon to the system tray:
'
' AddTrayIcon(frm As Form, flg As Long, callbackmsg As Long, icn As Long, tip As String)
'
' frm - the window that will receive the notification messages associated with an icon on the tray.
'
' flg - An array of flags indicating which of the other structure members contain valid data.
' The flg argument can be a combination of the following:
' NIF_ICON (&H1) hIcon is valid
' NIF_MESSAGE (&H2) uCallbackMessage is valid.
' NIF_TIP (&H4) szTip is valid.
' (e.g. flg = NIF_ICON Or NIF_TIP Or NIF_MESSAGE)
'
' callbackmsg - An application-defined message identifier.
' (e.g. callbackmsg = WM_MOUSEMOVE)
'
' icn - The handle of the system tray icon
'
' tip - The text for the system tray icon's tooltip
'
'
'2. Modify an existing icon on the system tray:
'
' ChangeTrayIcon(frm As Form, flg As Long, callbackmsg As Long, icn As Long, tip As String)
'
' (its parameters specification is same with the AddTrayIcon function)
'
'
'3. Remove (delete) an icon from the system tray:
' DeleteTrayIcon()
'
'
'[NOTES]
'
'
'[Other Variations]
'
'
'[See Also]
'
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'+++++ +++++'
'+++++ MODULE LEVEL VARIABLES, CONSTANTS, DECLARATIONS, +++++'
'+++++ +++++'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'''''''''''''''
''' Public '''
''' '''
'''''''''''''''
'''''''''''''''
''' Private '''
''' '''
'''''''''''''''
'User defined type required by Shell_NotifyIcon API call
Private 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
Private nid As NOTIFYICONDATA
'Constants required by Shell_NotifyIcon API call:
Const NIM_ADD = &H0
Const NIM_MODIFY = &H1
Const NIM_DELETE = &H2
'Declarations
Private Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'+++++ +++++'
'+++++ FUNCTIONS +++++'
'+++++ +++++'
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
'''''''''''''''''''''
''' COM Functions '''
''' '''
'''''''''''''''''''''
Public Sub AddTrayIcon(frm As Form, flg As Long, callbackmsg As Long, icn As Long, tip As String)
With nid
.cbSize = Len(nid)
.hwnd = frm.hwnd
.uId = vbNull
.uFlags = flg
.uCallBackMessage = callbackmsg
.hIcon = icn
.szTip = tip & vbNullChar
End With
Shell_NotifyIcon NIM_ADD, nid
End Sub
Public Sub ChangeTrayIcon(frm As Form, flg As Long, callbackmsg As Long, icn As Long, tip As String)
With nid
.cbSize = Len(nid)
.hwnd = frm.hwnd
.uId = vbNull
.uFlags = flg
.uCallBackMessage = callbackmsg
.hIcon = icn
.szTip = tip & vbNullChar
End With
Shell_NotifyIcon NIM_MODIFY, nid
End Sub
Public Sub DeleteTrayIcon()
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Copyright (c) 1999 - 2001, robert han, all rigths are reserved.