轉貼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 Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean Private Const NIM_ADD = &H0 Private Const NIM_MODIFY = &H1 Private Const NIM_DELETE = &H2 Global Const NIF_MESSAGE = &H1 Global Const NIF_ICON = &H2 Global Const NIF_TIP = &H4 private sub form_load() dim NIMGN as NOTIFYICONDATA With NIMGn .hwnd = me.hwnd .cbSize = Len(NIMGn) .uId = 1& .uFlags = NIF_ICON .hIcon = me.Icon .szTip = "" .ucallbackMessage = &H0 End With Shell_NotifyIcon NIM_ADD, NIMGn end sub '1. Add one model(TrayIn.bas): Option Explicit'Win32 API declaration Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean' Constants used to detect clicking on the icon Public Const WM_LBUTTONDBLCLK = &H203 Public Const WM_RBUTTONUP = &H205' Constants used to control the icon Public Const NIM_ADD = &H0 Public Const NIM_MODIFY = &H1 Public Const NIF_MESSAGE = &H1 Public Const NIM_DELETE = &H2 Public Const NIF_ICON = &H2 Public Const NIF_TIP = &H4' Used as the ID of the call back message Public Const WM_MOUSEMOVE = &H200' Used by Shell_NotifyIcon 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'create variable of type NOTIFYICONDATA Public TrayIcon As NOTIFYICONDATA'========================================================== '2. Add two forms: frmTray and frmAbout. On the form FrmTray: ' (1) add two CommandButtons: cmdHide and cmdExit. ' (2) add a image control name as imgIcon1,assign a icon or a picture to it; ' (3) add a menu to the form frmTray,name as mnuPopUp.Meanwhile,add three sub menu to mnuPopUp:mnuShow,mnuAbout and mnuExit. ' (4)the form frmTray'code like this: Option ExplicitPrivate Sub cmdExit_Click() Unload Me
End SubPrivate Sub cmdHidden_Click() Me.Hide End SubPrivate Sub Form_Load()
'centers form Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2 'sets cbSize to the Length of TrayIcon TrayIcon.cbSize = Len(TrayIcon) ' Handle of the window used to handle messages - which is the this form TrayIcon.hWnd = Me.hWnd ' ID code of the icon TrayIcon.uId = vbNull ' Flags TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE ' ID of the call back message TrayIcon.ucallbackMessage = WM_MOUSEMOVE ' The icon - sets the icon that should be used TrayIcon.hIcon = imgIcon1.Picture ' The Tooltip for the icon - sets the Tooltip that will be displayed TrayIcon.szTip = "Mind's Tray Icon Example" & Chr$(0)
' Add icon to the tray by calling the Shell_NotifyIcon API 'NIM_ADD is a Constant - add icon to tray Call Shell_NotifyIcon(NIM_ADD, TrayIcon)
' Don't let application appear in the Windows task list App.TaskVisible = FalseEnd Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Static Message As Long Static RR As Boolean
'x is the current mouse location along the x-axis Message = X / Screen.TwipsPerPixelX
If RR = False Then RR = True Select Case Message ' Left double click (This should bring up a dialog box) Case WM_LBUTTONDBLCLK Me.Show ' Right button up (This should bring up a menu) Case WM_RBUTTONUP Me.PopupMenu mnuPopUp End Select RR = False End If
End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) TrayIcon.cbSize = Len(TrayIcon) TrayIcon.hWnd = Me.hWnd TrayIcon.uId = vbNull 'Remove icon for Tray Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)
End SubPrivate Sub Form_Resize()
If Me.WindowState = vbMinimized Then Me.Hide
End SubPrivate Sub mnuAbout_Click()
frmAbout.Show
End SubPrivate Sub mnuShow_Click() Me.WindowState = 0 Me.Show End SubPrivate Sub mnuExit_Click()
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 Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
Global Const NIF_MESSAGE = &H1
Global Const NIF_ICON = &H2
Global Const NIF_TIP = &H4
private sub form_load()
dim NIMGN as NOTIFYICONDATA
With NIMGn
.hwnd = me.hwnd
.cbSize = Len(NIMGn)
.uId = 1&
.uFlags = NIF_ICON
.hIcon = me.Icon
.szTip = ""
.ucallbackMessage = &H0
End With
Shell_NotifyIcon NIM_ADD, NIMGn
end sub
'1. Add one model(TrayIn.bas):
Option Explicit'Win32 API declaration
Declare Function Shell_NotifyIcon Lib "shell32" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean' Constants used to detect clicking on the icon
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONUP = &H205' Constants used to control the icon
Public Const NIM_ADD = &H0
Public Const NIM_MODIFY = &H1
Public Const NIF_MESSAGE = &H1
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_TIP = &H4' Used as the ID of the call back message
Public Const WM_MOUSEMOVE = &H200' Used by Shell_NotifyIcon
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'create variable of type NOTIFYICONDATA
Public TrayIcon As NOTIFYICONDATA'==========================================================
'2. Add two forms: frmTray and frmAbout. On the form FrmTray:
' (1) add two CommandButtons: cmdHide and cmdExit.
' (2) add a image control name as imgIcon1,assign a icon or a picture to it;
' (3) add a menu to the form frmTray,name as mnuPopUp.Meanwhile,add three sub menu to mnuPopUp:mnuShow,mnuAbout and mnuExit.
' (4)the form frmTray'code like this:
Option ExplicitPrivate Sub cmdExit_Click() Unload Me
End SubPrivate Sub cmdHidden_Click()
Me.Hide
End SubPrivate Sub Form_Load()
'centers form
Me.Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2 'sets cbSize to the Length of TrayIcon
TrayIcon.cbSize = Len(TrayIcon)
' Handle of the window used to handle messages - which is the this form
TrayIcon.hWnd = Me.hWnd
' ID code of the icon
TrayIcon.uId = vbNull
' Flags
TrayIcon.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
' ID of the call back message
TrayIcon.ucallbackMessage = WM_MOUSEMOVE
' The icon - sets the icon that should be used
TrayIcon.hIcon = imgIcon1.Picture
' The Tooltip for the icon - sets the Tooltip that will be displayed
TrayIcon.szTip = "Mind's Tray Icon Example" & Chr$(0)
' Add icon to the tray by calling the Shell_NotifyIcon API
'NIM_ADD is a Constant - add icon to tray
Call Shell_NotifyIcon(NIM_ADD, TrayIcon)
' Don't let application appear in the Windows task list
App.TaskVisible = FalseEnd Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)Static Message As Long
Static RR As Boolean
'x is the current mouse location along the x-axis
Message = X / Screen.TwipsPerPixelX
If RR = False Then
RR = True
Select Case Message
' Left double click (This should bring up a dialog box)
Case WM_LBUTTONDBLCLK
Me.Show
' Right button up (This should bring up a menu)
Case WM_RBUTTONUP
Me.PopupMenu mnuPopUp
End Select
RR = False
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) TrayIcon.cbSize = Len(TrayIcon)
TrayIcon.hWnd = Me.hWnd
TrayIcon.uId = vbNull
'Remove icon for Tray
Call Shell_NotifyIcon(NIM_DELETE, TrayIcon)
End SubPrivate Sub Form_Resize()
If Me.WindowState = vbMinimized Then Me.Hide
End SubPrivate Sub mnuAbout_Click()
frmAbout.Show
End SubPrivate Sub mnuShow_Click()
Me.WindowState = 0
Me.Show
End SubPrivate Sub mnuExit_Click()
Unload Me
End Sub
http://www.hongen.com/pc/program/apitutor/api0012/api01.htm托盘程序详解(二)
http://www.hongen.com/pc/program/apitutor/api0013/api01.htm