VB中使用systray.ocx这个托盘控件时的问题 5

在托盘中点击它时,想用popupmenu方法让它弹出快捷菜单,怎样设定菜单的位置呢?systray.ocx的mouseup事件中好像没有x,y两个阐参数... 在托盘中点击它时,想用popupmenu方法让它弹出快捷菜单,怎样设定菜单的位置呢?
systray.ocx 的 mouseup事件中好像没有x,y两个阐参数
展开
 我来答
343053069
2007-11-13 · TA获得超过401个赞
知道答主
回答量:75
采纳率:0%
帮助的人:0
展开全部
不知道你用的是什么控件
但是托盘图标的话,我是用api做的,
这是自定义模块中写的内容

Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Public Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal hicon As Long) As Long
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 GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Public Declare Function TrackPopupMenu Lib "user32" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal X As Long, ByVal Y As Long, ByVal nReserved As Long, ByVal hwnd As Long, lprc As Any) As Long
Public Declare Function GetMenu Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Public Type POINTAPI
X As Long
Y As Long
End Type
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 Const NIM_ADD = 0 '添加图标
Public Const NIM_MODIFY = 1 '修改图标
Public Const NIM_DELETE = 2 '删除图标

Public Const NIF_MESSAGE = 1 '当有鼠标事件发生时产生消息
Public Const NIF_ICON = 2 '
Public Const NIF_TIP = 4 '图标有提示字符串

'Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_USER = &H400
Public Const WM_NOTIFYICON = WM_USER + &H100
Public Const WM_COMMAND = &H111
Public Const WM_DESTROY = &H2
Public Const WM_DRAWITEM = &H2B
Public Const WM_INITDIALOG = &H110
Public Const WM_PAINT = &HF
Public Const WM_MENUSELECT = &H11F

Public Const GWL_WNDPROC = (-4) '替换窗口处理函数
'Global lproc As Long

Dim pmenu As Long
Dim submenu As Long

Function CMenu() As Boolean
'这个函数获得form4的子菜单
Dim l As Long
Dim l1 As Long

pmenu = GetMenu(Form4.hwnd)
submenu = GetSubMenu(pmenu, 0)
If submenu Then
CMenu = True
Else
CMenu = False
End If
End Function
Function Icon_Del(ihwnd As Long) As Long
Dim ano As NOTIFYICONDATA
Dim l As Long

ano.hwnd = ihwnd
ano.uID = 0
ano.cbSize = Len(ano)
'删除图标
Icon_Del = Shell_NotifyIcon(NIM_DELETE, ano)
End Function
'这个函数接收图标句柄和窗口句柄并且新建图标
Function Icon_Add(ihwnd As Long, hicon As Long) As Long
Dim ano As NOTIFYICONDATA
Dim astr As String

'为图标添加提示行
'astr = LTrim$(InputBox$("Input the tips you wanted to add."))
ano.szTip = "双击显示窗体,右键可以弹出功能"
'设置消息接收窗口
ano.hwnd = ihwnd
ano.uID = 0
'图标有提示并且可以发送消息
ano.uFlags = NIF_ICON Or NIF_TIP Or NIF_MESSAGE
ano.hicon = hicon
ano.cbSize = Len(ano)
'将图标的回调消息设置为WM_NOTIFYICON,当在图标区域有鼠标消息,系统就会向
'消息接收窗口发送WM_NOTIFYICON消息。
ano.uCallbackMessage = WM_NOTIFYICON
Icon_Add = Shell_NotifyIcon(NIM_ADD, ano)
End Function

Function DialogProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'该函数为Form2的窗口处理函数。
Dim l As Long
Dim l1 As Long
Dim po As POINTAPI
Select Case uMsg
Case WM_INITDIALOG
Case WM_DESTROY
Case WM_COMMAND
Case WM_DRAWITEM
Case WM_NOTIFYICON '有鼠标事件产生
Select Case lParam
Case WM_LBUTTONDBLCLK
Form1.Show
Case WM_RBUTTONDOWN '按下鼠标右键弹出菜单
If submenu Then
l = GetCursorPos(po) '获的光标位置
'在光标位置处弹出菜单
l1 = TrackPopupMenu(submenu, (TPM_LEFTALIGN Or TPM_RIGHTBUTTON), po.X, po.Y, 0, Form4.hwnd, Null)
End If
Case Else
End Select
Case Else
DialogProc = False
End Select
DialogProc = True
End Function

主form里写这个
'‘***********************************************初始化托盘图标
Dim l As Long
Dim f As String
l = Icon_Del(xs.hwnd)
l = SetWindowLong(xs.hwnd, GWL_WNDPROC, lproc)
f = App.Path & "\" & "T123.ico"
p123.Picture = LoadPicture(f)
If (Icon_Add(xs.hwnd, p123.Picture)) Then
xb = CMenu() '添加弹出菜单

'将DialogProc函数设置为Form2的窗口处理函数并且保存原来窗口处理函数句柄
lproc = SetWindowLong(xs.hwnd, GWL_WNDPROC, AddressOf DialogProc)
End If

其中xs.hwnd是空白form的句饼,你可以写form2.hwnd
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式