VB 托盘菜单问题

我用VB做了个托盘,右击托盘弹出菜单,但是我点其他地方的时候他弹出的菜单就是不消失,问别人说编辑菜单失去焦点事件什么的,请问,改如何操作,要简单易懂的,谢谢,成功的话,我... 我用VB做了个托盘,右击托盘弹出菜单,但是我点其他地方的时候他弹出的菜单就是不消失,问别人说编辑菜单失去焦点事件什么的,请问,改如何操作,要简单易懂的,谢谢,成功的话,我加分
一楼的复制别人的,这个方法试过,不行,老是出错 MsgBox Curwindow,这一句,老是弹出,谈到卡死
展开
 我来答
kkdhw17
2010-12-26 · TA获得超过217个赞
知道答主
回答量:303
采纳率:100%
帮助的人:196万
展开全部
此问题可以解决,具体分二步:
一、创建模块,并在模块中复制下面代码,然后保存。
'====模块代码====
Option Explicit

Private Const WH_MOUSE_LL As Long = 14
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_MOUSEWHEEL As Long = &H20A
Private Const WM_RBUTTONDOWN As Long = &H204
Private Const WM_RBUTTONUP As Long = &H205

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type MOUSEHOOKSTRUCT
pt As POINTAPI
Hwnd As Long
wHitTestCode As Long
dwExtraInfo As Long
End Type

Private Declare Function CallNextHookEx Lib "user32.dll" ( _
ByVal hHook As Long, _
ByVal nCode As Long, _
ByVal wParam As Long, _
ByRef lParam As MOUSEHOOKSTRUCT) As Long
Private Declare Function SetWindowsHookEx Lib "user32.dll" Alias "SetWindowsHookExA" ( _
ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32.dll" ( _
ByVal hHook As Long) As Long

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Dim hHook As Long

Private Function GetWinClass(hwd As Long) As String
Dim retvalue As Long, TempStr As String * 254
retvalue = GetClassName(hwd, TempStr, 254)
GetWinClass = StrConv(LeftB(StrConv(TempStr, vbFromUnicode), retvalue), vbUnicode)
End Function

Public Function EnableMouseLLHook()
hHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf LowLevelMouseProc, App.hInstance, 0)
End Function

Public Function LowLevelMouseProc(ByVal nCode As Long, ByVal wParam As Long, ByRef lParam As MOUSEHOOKSTRUCT) As Long
LowLevelMouseProc = CallNextHookEx(hHook, nCode, wParam, lParam)
If (nCode < 0) Then Exit Function
Select Case wParam
Case WM_LBUTTONUP, WM_RBUTTONUP
Dim Curwindow As Long
Curwindow = WindowFromPoint(lParam.pt.x, lParam.pt.y)
Curwindow = GetParent(Curwindow)
If Curwindow<>0 Then
MsgBox Curwindow
End If
DisableMouseLLHook
End Select
End Function

Public Function DisableMouseLLHook()
hHook = UnhookWindowsHookEx(hHook)
End Function

'二、除了在Form_MouseMove作如下修改外,其他代码不变,然后运行,OK。
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim lMsg As Single
lMsg = X / Screen.TwipsPerPixelX
If lMsg = WM_LBUTTONUP Then Me.Visible = True
If lMsg = WM_RBUTTONUP Then
EnableMouseLLHook '监视鼠标按键动作
Me.PopupMenu sys
end if
End Sub
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
AiPPT
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图... 点击进入详情页
本回答由AiPPT提供
bd_tianda
2010-12-27 · TA获得超过114个赞
知道答主
回答量:126
采纳率:0%
帮助的人:0
展开全部
一楼的太麻烦,其实没必要使用钩子,加一句代码就ok了:
……
SetForegroundWindow Me.hwnd
Me.PopupMenu mainMenu
……
API的声明你自己写吧。
空间里有我编程的一些经验,你可以去看看,以后会陆续更新。
http://hi.baidu.com/bd_tianda/home

参考资料: http://hi.baidu.com/bd_tianda/blog/item/2df950fcbeea9790b801a094.html

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式