gzh20065
回答數:86 | 被采納數:120
類代碼
Public Const DefaultIconIndex = 1 '圖標缺省索引
Public Const WM_LBUTTONDOWN = &H201 '按鼠標左鍵
Public Const WM_RBUTTONDOWN = &H204 '按鼠標右鍵
Public Const NIM_ADD = 0 '添加圖標
Public Const NIM_MODIFY = 1 '修改圖標
Public Const NIM_DELETE = 2 '刪除圖標
Public Const NIF_MESSAGE = 1 'message 有效
Public Const NIF_ICON = 2 '圖標操作(添加、修改、刪除)有效
Public Const NIF_TIP = 4 'ToolTip(提示)有效
'API函數聲明
'圖標操作
Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
'判斷窗口是否最小化
Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
'
'設置窗口位置和狀態(position)的功能
Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags 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 Function Icon_Add(iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
'參數說明:iHwnd:窗口句柄,sTips:當鼠標移到通知欄圖標上時顯示的提示內容
'hIcon:圖標句柄,IconID:圖標Id號
Dim IconVa As NOTIFYICONDATA
With IconVa
.hwnd = iHwnd
.szTip = sTips + Chr$(0)
.hIcon = hIcon
.uID = IconID
.uCallbackMessage = WM_LBUTTONDOWN
.cbSize = Len(IconVa)
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Icon_Add = Shell_NotifyIcon(NIM_ADD, IconVa)
End With
End Function
'刪除通知欄圖標(參數說明同Icon_Add)
Function Icon_Del(iHwnd As Long, lIndex As Long) As Long
Dim IconVa As NOTIFYICONDATA
Dim L As Long
With IconVa
.hwnd = iHwnd
.uID = lIndex
.cbSize = Len(IconVa)
End With
Icon_Del = Shell_NotifyIcon(NIM_DELETE, IconVa)
End Function
'修改通知欄圖標(參數說明同Icon_Add)
Public Function Icon_Modify(iHwnd As Long, sTips As String, hIcon As Long, IconID As Long) As Long
Dim IconVa As NOTIFYICONDATA
With IconVa
.hwnd = iHwnd
.szTip = sTips + Chr$(0)
.hIcon = hIcon
.uID = IconID
.uCallbackMessage = WM_LBUTTONDOWN
.cbSize = Len(IconVa)
.uFlags = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
Icon_Modify = Shell_NotifyIcon(NIM_MODIFY, IconVa)
End With
End Function
窗口代碼
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim L
L = X \ 15
'點擊通知欄圖標,用鼠標右鍵時調出彈出菜單
If L = WM_RBUTTONDOWN Then
Me.PopupMenu main
'點擊通知欄圖標,用鼠標左鍵時,將通知欄圖標改為另外的圖標
ElseIf L = WM_LBUTTONDOWN Then
Call Icon_Del(Me.hwnd, 0) '刪除通知欄圖標
Me.WindowState = 0
Me.Show '調出窗口
End If
End Sub
Private Sub Form_Resize()
If Me.WindowState = 1 Then
'MinFlag = True
Me.Visible = False '隱藏窗口
'將窗口圖標加入通知欄
Call Icon_Add(Me.hwnd, Me.Caption, Me.Icon, 0)
End If
End Sub
Private Sub Exit_Click(Index As Integer)
Call Icon_Del(Me.hwnd, 0) '刪除通知欄圖標
End '退出程序
End Sub
Private Sub Show_Click(Index As Integer)
Call Icon_Del(Me.hwnd, 0) '刪除通知欄圖標
Me.WindowState = 0
Me.Show '調出窗口
End Sub
我試過用BUTTON代替,但是光標一到那個圖標的位置就已經反應了,我還沒按,不知道為什麼,因為這些代碼都是從網上找的,麻煩版大幫忙看看,主要是實現窗口圖標在托盤顯示而且圖標沒有走樣,謝謝。
MSDN的說明
uCallbackMessage
Application-defined message identifier. The system uses this identifier for notification messages that it sends to the window identified in hWnd. These notifications are sent when a mouse event occurs in the bounding rectangle of the icon.
意思是隻要對托盤圖標有任何動作都會向窗口發送uCallbackMessage消息,這個消息由用戶指定,VC裏隻要在WindowProc函數中case這個消息
你設置uCallbackMessage = WM_LBUTTONDOWN跟係統的左鍵消息衝突,所以圖標一動就會觸發Form_MouseDown
VB裏處理自定義消息需要自己實現WindowProc http://www.b181/bianchen/bianchen-105536.html