shell_notifyicon使用困惑
大家好,我在VB中写了个托盘程序,使用到了shell_notifyicon这个API,在参照了MSDN上的帮助后,实现了托盘图标的加载与删除,但就是在托盘图标上点击得不到响应,我把MSDN上的代码复制是可以成功的。我没看到自己写的和它有什么不同。这是FORM_LOAD()
里的代码
Dim tNID As NOTIFYICONDATA
tNID.hIcon = Me.Icon
tNID.hwnd = Me.hwnd
tNID.szTip = "Author:yjgahpgh@163.com"
tNID.uCallbackMessage = WM_MOUSEMOVE
tNID.uFlags = NIF_MESSAGE Or NIF_TIP Or NIF_ICON
tNID.uID = 0&
tNID.cbSize = Len(tNID)
rtnval = Shell_NotifyIcon(NIM_ADD, tNID)
然后我在FORM_MOUSEMOVE事件里写道:
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim msg As Long
If Me.ScaleMode = vbPixels Then
msg = X
Else
msg = X / Screen.TwipsPerPixelX
End If
Select Case msg
Case WM_LBUTTONDBLCLK
Call mnuAbout_Click
Case WM_RBUTTONDOWN
PopupMenu mnuFile
End Select
End Sub
请各位给些指点。如使用这个API要注意的地方,有人说要自定义什么。请赐教。 --------------------编程问答-------------------- 试试Case WM_RBUTTONUP --------------------编程问答-------------------- 貌似没什么问题! 你把msdn的也贴上来看看! --------------------编程问答-------------------- 请到我的资源http://download.csdn.net/source/570264里面下载“VBtray系统托盘图标动态绘制VB源码 ” --------------------编程问答-------------------- 推荐LZ子类,这样成功率高 --------------------编程问答-------------------- 在Form_MouseMove里处理消息? --------------------编程问答-------------------- http://www.m5home.com/blog2/blogview.asp?logID=217
送你一个封装好的类,直接在事件里处理即可. --------------------编程问答--------------------
顶老马 --------------------编程问答-------------------- 以下是MSDN上的示例程序,放上两个按钮,一个通用对话框就可以实验了。汗了,真没看出与我写的有什么不同。只不过他把shell_notifyicon声明成布尔型的了。大伙看看。
'Declare a user-defined variable to pass to the Shell_NotifyIcon
'function.
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
'Declare the constants for the API function. These constants can be
'found in the header file Shellapi.h.
'The following constants are the messages sent to the
'Shell_NotifyIcon function to add, modify, or delete an icon from the
'taskbar status area.
Private Const NIM_ADD = &H0
Private Const NIM_MODIFY = &H1
Private Const NIM_DELETE = &H2
'The following constant is the message sent when a mouse event occurs
'within the rectangular boundaries of the icon in the taskbar status
'area.
Private Const WM_MOUSEMOVE = &H200
'The following constants are the flags that indicate the valid
'members of the NOTIFYICONDATA data type.
Private Const NIF_MESSAGE = &H1
Private Const NIF_ICON = &H2
Private Const NIF_TIP = &H4
'The following constants are used to determine the mouse input on the
'the icon in the taskbar status area.
'Left-click constants.
Private Const WM_LBUTTONDBLCLK = &H203 'Double-click
Private Const WM_LBUTTONDOWN = &H201 'Button down
Private Const WM_LBUTTONUP = &H202 'Button up
'Right-click constants.
Private Const WM_RBUTTONDBLCLK = &H206 'Double-click
Private Const WM_RBUTTONDOWN = &H204 'Button down
Private Const WM_RBUTTONUP = &H205 'Button up
'Declare the API function call.
Private Declare Function Shell_NotifyIcon Lib "shell32" _
Alias "Shell_NotifyIconA" _
(ByVal dwMessage As Long, pnid As NOTIFYICONDATA) As Boolean
'Dimension a variable as the user-defined data type.
Dim nid As NOTIFYICONDATA
Private Sub Command1_Click()
'Click this button to add an icon to the taskbar status area.
'Set the individual values of the NOTIFYICONDATA data type.
nid.cbSize = Len(nid)
nid.hWnd = Form1.hWnd
nid.uId = vbNull
nid.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
nid.uCallBackMessage = WM_MOUSEMOVE
nid.hIcon = Form1.Icon
nid.szTip = "Taskbar Status Area Sample Program" & vbNullChar
'Call the Shell_NotifyIcon function to add the icon to the taskbar
'status area.
Shell_NotifyIcon NIM_ADD, nid
End Sub
Private Sub Command2_Click()
'Click this button to delete the added icon from the taskbar
'status area by calling the Shell_NotifyIcon function.
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub Form_Load()
'Set the captions of the command button when the form loads.
Command1.Caption = "Add an Icon"
Command2.Caption = "Delete Icon"
End Sub
Private Sub Form_Terminate()
'Delete the added icon from the taskbar status area when the
'program ends.
Shell_NotifyIcon NIM_DELETE, nid
End Sub
Private Sub Form_MouseMove _
(Button As Integer, _
Shift As Integer, _
X As Single, _
Y As Single)
'Event occurs when the mouse pointer is within the rectangular
'boundaries of the icon in the taskbar status area.
Dim msg As Long
Dim sFilter As String
msg = X / Screen.TwipsPerPixelX
Select Case msg
Case WM_LBUTTONDOWN
Case WM_LBUTTONUP
Case WM_LBUTTONDBLCLK
CommonDialog1.DialogTitle = "Select an Icon"
sFilter = "Icon Files (*.ico)|*.ico"
sFilter = sFilter & "|All Files (*.*)|*.*"
CommonDialog1.Filter = sFilter
CommonDialog1.ShowOpen
If CommonDialog1.filename <> "" Then
Form1.Icon = LoadPicture(CommonDialog1.filename)
nid.hIcon = Form1.Icon
Shell_NotifyIcon NIM_MODIFY, nid
End If
Case WM_RBUTTONDOWN
Dim ToolTipString As String
ToolTipString = InputBox("Enter the new ToolTip:", _
"Change ToolTip")
If ToolTipString <> "" Then
nid.szTip = ToolTipString & vbNullChar
Shell_NotifyIcon NIM_MODIFY, nid
End If
Case WM_RBUTTONUP
Case WM_RBUTTONDBLCLK
End Select
End Sub
--------------------编程问答-------------------- 不要迷信MSDN上的示例程序! --------------------编程问答-------------------- 你把shell_notifyicon 声明成什么类型了啊,应该LONG也可以的 我觉得会不会是你的菜单有什么问题 --------------------编程问答-------------------- WM_RBUTTONDOWN这些常量是否已经定义,并要注意是鼠标右键,而不是左键。
补充:VB , API