vb程序如何将图标放入工具栏
- 提问者网友:放下
- 2021-08-10 15:40
- 五星知识达人网友:北城痞子
- 2021-08-10 16:59
'窗体代码
Private lpTrayIconData As NOTIFYICONDATA
Private Sub Form_Load()
With lpTrayIconData
.cbSize = Len(lpTrayIconData)
.hIcon = Me.Icon.Handle
.hwnd = Me.hwnd
.szTip = "托盘消息演示" & vbNullChar
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
.uCallbackMessage = WM_TRAYICON
.uID = 0
End With
Shell_NotifyIcon NIM_ADD, lpTrayIconData
pWndProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
Shell_NotifyIcon NIM_DELETE, lpTrayIconData
SetWindowLong Me.hwnd, GWL_WNDPROC, pWndProc
End Sub
'模块代码:
Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4
'download by http://www.codefans.net
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 Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
Public Const WM_TRAYICON = WM_USER + 123 '托盘消息
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205
Public Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
'===========================================
Public pWndProc As Long
Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_TRAYICON Then
Select Case lParam
Case WM_RBUTTONDOWN
SetForegroundWindow hwnd '关键的一步
Case WM_RBUTTONUP
Form1.PopupMenu Form1.Mnu_Menu
End Select
End If
WndProc = CallWindowProc(pWndProc, hwnd, Msg, wParam, lParam)
End Function