×

registerwindowmessage

关于C#的Hook技术问题?VB任务栏气泡

admin admin 发表于2022-06-01 06:51:32 浏览104 评论0

抢沙发发表评论

关于C#的Hook技术问题


只有键盘和鼠标可以在托管代码中实现全局钩子,否则要通过C++写DLL,原因貌似.net的垃圾回收机制?
思路可以这样
在DLL里面
1)用SetProp函数将C#写的一个窗口设置成特别的属性,比如:
SetProp(GetDesktopWindow(), “FLAG_WND“, target);

2)挂上钩子,捕捉到相应的Hook code后,用RegisterWindowMessage函数注册信息,然后用SendNotifyMessage函数将该信息发送给特定属性的托管窗口

VB任务栏气泡


晕,刚才回答了,居然不合格。我只有把所有代码贴出来了。’系统托盘模块代码’*************************************************************************
’**模 块 名:ShellNotifyIcon
’**描 述:显示托盘提示模块
’*************************************************************************
Option Explicit’’’’’’’’’’先向系统注册TaskbarCreated消息
’’’’’’’’’’WM_TASKBARCREATED = RegisterWindowMessage(“TaskbarCreated“);
’’’’’’’’’’然後在WndProc函数里判断这个消息,是的话重建托盤就可以了。Declare Function SetForegroundWindow Lib “user32“ (ByVal hwnd As Long) As Long
Private Declare Function SetWindowLong Lib “user32.dll“ Alias “SetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib “user32.dll“ Alias “CallWindowProcA“ (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Const WM_MOUSEISMOVING = &H200 ’在图标上移动鼠标
Private Const WM_LBUTTONDOWN = &H201 ’鼠标左键按下
Private Const WM_LBUTTONUP = &H202 ’鼠标左键释放
Private Const WM_LBUTTONDBLCLK = &H203 ’双击鼠标左键
Private Const WM_RBUTTONDOWN = &H204 ’鼠标右键按下
Private Const WM_RBUTTONUP = &H205 ’鼠标右键释放
Private Const WM_RBUTTONDBLCLK = &H206 ’双击鼠标右键
Private Const WM_MBUTTONDOWN = &H207 ’鼠标右键按下
Private Const WM_MBUTTONUP = &H208 ’鼠标右键释放
Private Const WM_MBUTTONDBLCLK = &H209 ’双击鼠标右键
Private Const WM_SETHOTKEY = &H32 ’响应您定义的热键Private Const WM_USER = &H400
Private Const WM_NOTIFYICON = WM_USER + 1 ’ 自定义消息
Private Const GWL_WNDPROC = (-4)’ 关于气球提示的自定义消息, 2000下不产生这些消息
Private Const NIN_BALLOONSHOW = (WM_USER + &H2) ’ 当 Balloon Tips 弹出时执行
Private Const NIN_BALLOONHIDE = (WM_USER + &H3) ’ 当 Balloon Tips 消失时执行(如 SysTrayIcon 被删除),’ 但指定的 TimeOut 时间到或鼠标点击 Balloon Tips 后的消失不发送此消息
Private Const NIN_BALLOONTIMEOUT = (WM_USER + &H4) ’ 当 Balloon Tips 的 TimeOut 时间到时执行
Private Const NIN_BALLOONUSERCLICK = (WM_USER + &H5) ’ 当鼠标点击 Balloon Tips 时执行。
’ 注意:在XP下执行时 Balloon Tips 上有个关闭按钮,
’ 如果鼠标点在按钮上将接收到 NIN_BALLOONTIMEOUT 消息。Private Declare Function Shell_NotifyIcon Lib “shell32.dll“ Alias “Shell_NotifyIconA“ (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function RegisterWindowMessage Lib “user32“ Alias “RegisterWindowMessageA“ (ByVal lpString As String) As LongPrivate Type NOTIFYICONDATA
cbSize As Long ’ 结构大小(字节)
hwnd As Long ’ 处理消息的窗口的句柄
uId As Long ’ 唯一的标识符
uFlags As Long ’ Flags
uCallBackMessage As Long ’ 处理消息的窗口接收的消息
hIcon As Long ’ 托盘图标句柄
szTip As String * 128 ’ Tooltip 提示文本
dwState As Long ’ 托盘图标状态
dwStateMask As Long ’ 状态掩码
szInfo As String * 256 ’ 气球提示文本
uTimeoutOrVersion As Long ’ 气球提示消失时间或版本
’ uTimeout - 气球提示消失时间(单位:ms, 10000 -- 30000)
’ uVersion - 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ’ 气球提示标题
dwInfoFlags As Long ’ 气球提示图标
End Type’ dwState to NOTIFYICONDATA structure
Private Const NIS_HIDDEN = &H1 ’ 隐藏图标
Private Const NIS_SHAREDICON = &H2 ’ 共享图标’ dwInfoFlags to NOTIFIICONDATA structure
’Private Const NIIF_NONE = &H0 ’ 无图标
’Private Const NIIF_INFO = &H1 ’ “消息“图标
’Private Const NIIF_WARNING = &H2 ’ “警告“图标
’Private Const NIIF_ERROR = &H3 ’ “错误“图标
Public Enum NIIF_InfoFlag
NIIF_NONE = &H0 ’ 无图标
NIIF_INFO = &H1 ’ “消息“图标
NIIF_WARNING = &H2 ’ “警告“图标
NIIF_ERROR = &H3 ’ “错误“图标
End Enum’ uFlags to NOTIFYICONDATA structure
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_ICON As Long = &H2
Private Const NIF_TIP As Long = &H4
Private Const NIF_STATE As Long = &H8
Private Const NIF_INFO As Long = &H10’ dwMessage to Shell_NotifyIcon
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const lngNIM_SETVERSION As Long = &H4Private lngPreWndProc As Long
Private lngMsgBarRester As Long
Private OwerForm As Form

Private IconData As NOTIFYICONDATA’是否显示Tip信息
Private TipShowing As Boolean’--------------------------------------------------------------------------------
’*************************************************************************
’**函 数 名:AddNotifyIcon
’**输 入:frm(Form) - 窗体
’** :strTitle(String) - 托盘提示标题
’** :strInfo(String) - 托盘提示信息
’** :Optional lngType(Long = 1) - 托盘提示类型 0 无 1 信息 2 警告 3 错误
’** :Optional lngTime(Long = 10000) - 停留时间
’**输 出:无
’**功能描述:显示托盘图标提示信息
Public Sub AddNotifyIcon(frm As Form, strTitle As String, strInfo As String, Optional bTipMessage As Boolean = False, Optional lngType As NIIF_InfoFlag = NIIF_INFO, Optional lngTime As Long = -1)
’ 向托盘区添加图标

Set OwerForm = frm

With IconData
.cbSize = Len(IconData)
.hwnd = OwerForm.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
If bTipMessage Then .uFlags = .uFlags Or NIF_INFO Or NIF_STATE
.uCallBackMessage = WM_NOTIFYICON
.szTip = strTitle & vbNullChar
.hIcon = OwerForm.Icon.Handle
.dwState = 0
.dwStateMask = 0
.szInfo = strInfo & vbNullChar
.szInfoTitle = strTitle & vbNullChar
.dwInfoFlags = lngType
.uTimeoutOrVersion = lngTime
End With If lngPreWndProc = 0 Then ’没有初始化
Shell_NotifyIcon NIM_ADD, IconData
lngMsgBarRester = RegisterWindowMessage(“TaskBarCreated“)
lngPreWndProc = SetWindowLong(OwerForm.hwnd, GWL_WNDPROC, AddressOf NotifyIconWindowProc)
Else ’已初始化
Shell_NotifyIcon NIM_MODIFY, IconData
End IfEnd Sub’*************************************************************************
’**函 数 名:DeleteNotifyIcon
’**输 出:无
’**功能描述:删除托盘图标
Public Sub DeleteNotifyIcon()
If lngPreWndProc 《》 0 Then
’ 删除托盘区图标
With IconData
.cbSize = Len(IconData)
.hwnd = OwerForm.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
.uCallBackMessage = WM_NOTIFYICON
.szTip = ““
.hIcon = OwerForm.Icon.Handle
End With
Shell_NotifyIcon NIM_DELETE, IconData
SetWindowLong OwerForm.hwnd, GWL_WNDPROC, lngPreWndProc
lngPreWndProc = 0
End If
End Sub’*************************************************************************
’**函 数 名:NotifyIconWindowProc
’**输 入:ByVal hwnd(Long) -
’** :ByVal msg(Long) -
’** :ByVal wParam(Long) -
’** :ByVal lParam(Long) -
’**输 出:(Long) -
’**功能描述:frmTest 窗口入口函数
’*************************************************************************
Function NotifyIconWindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
’ 拦截 WM_NOTIFYICON 消息
If msg = WM_NOTIFYICON Then
Select Case lParam

Case WM_LBUTTONUP
Debug.Print “左键单击“

Case WM_LBUTTONDBLCLK
Debug.Print “左键双击“

Case WM_RBUTTONUP
Debug.Print “右键单击“

Case WM_RBUTTONDBLCLK
Debug.Print “右键双击“

Case WM_MBUTTONUP
Debug.Print “中键单击“

Case WM_MBUTTONDBLCLK
Debug.Print “中键双击“

Case NIN_BALLOONSHOW
Debug.Print “显示气球提示“
TipShowing = True

Case NIN_BALLOONHIDE
Debug.Print “删除托盘图标“
TipShowing = False

Case NIN_BALLOONTIMEOUT
Debug.Print “气球提示消失“
TipShowing = False

Case NIN_BALLOONUSERCLICK
Debug.Print “单击气球提示“
TipShowing = False

Case WM_MOUSEISMOVING
’Debug.Print “鼠标移动“

Case Else
Debug.Print lParam

End Select
ElseIf msg = lngMsgBarRester Then
AddNotifyIcon OwerForm, IconData.szInfoTitle, IconData.szInfo
End If
NotifyIconWindowProc = CallWindowProc(lngPreWndProc, hwnd, msg, wParam, lParam)
End Function’*************************************************************************
’**函 数 名:ShowTrayTipMessage
’**输 出:无
’**功能描述:设置新的标题
Public Sub ShowTrayTipMessage(strTitle As String, strInfo As String, Optional lngType As NIIF_InfoFlag = NIIF_INFO, Optional lngTime As Long = -1)
With IconData
.uFlags = .uFlags Or NIF_INFO Or NIF_STATE
.szInfo = strInfo & vbNullChar
.szInfoTitle = strTitle & vbNullChar
.dwInfoFlags = lngType
.uTimeoutOrVersion = lngTime
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub’*************************************************************************
’**函 数 名:SetTrayTip
’**输 出:无
’**功能描述:设置新的标题
Public Sub SetTrayTip(tip As String)
With IconData
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub’*************************************************************************
’**函 数 名:SetTrayIcon
’**输 出:无
’**功能描述:设置新的图标
Public Sub SetTrayIcon(pic As Picture)
If pic.Type 《》 vbPicTypeIcon Then Exit Sub With IconData
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub
-registerwindowmessage

VB任务栏气泡


晕,刚才回答了,居然不合格。我只有把所有代码贴出来了。’系统托盘模块代码’*************************************************************************
’**模 块 名:ShellNotifyIcon
’**描 述:显示托盘提示模块
’*************************************************************************
Option Explicit’’’’’’’’’’先向系统注册TaskbarCreated消息
’’’’’’’’’’WM_TASKBARCREATED = RegisterWindowMessage(“TaskbarCreated“);
’’’’’’’’’’然後在WndProc函数里判断这个消息,是的话重建托盤就可以了。Declare Function SetForegroundWindow Lib “user32“ (ByVal hwnd As Long) As Long
Private Declare Function SetWindowLong Lib “user32.dll“ Alias “SetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib “user32.dll“ Alias “CallWindowProcA“ (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongPrivate Const WM_MOUSEISMOVING = &H200 ’在图标上移动鼠标
Private Const WM_LBUTTONDOWN = &H201 ’鼠标左键按下
Private Const WM_LBUTTONUP = &H202 ’鼠标左键释放
Private Const WM_LBUTTONDBLCLK = &H203 ’双击鼠标左键
Private Const WM_RBUTTONDOWN = &H204 ’鼠标右键按下
Private Const WM_RBUTTONUP = &H205 ’鼠标右键释放
Private Const WM_RBUTTONDBLCLK = &H206 ’双击鼠标右键
Private Const WM_MBUTTONDOWN = &H207 ’鼠标右键按下
Private Const WM_MBUTTONUP = &H208 ’鼠标右键释放
Private Const WM_MBUTTONDBLCLK = &H209 ’双击鼠标右键
Private Const WM_SETHOTKEY = &H32 ’响应您定义的热键Private Const WM_USER = &H400
Private Const WM_NOTIFYICON = WM_USER + 1 ’ 自定义消息
Private Const GWL_WNDPROC = (-4)’ 关于气球提示的自定义消息, 2000下不产生这些消息
Private Const NIN_BALLOONSHOW = (WM_USER + &H2) ’ 当 Balloon Tips 弹出时执行
Private Const NIN_BALLOONHIDE = (WM_USER + &H3) ’ 当 Balloon Tips 消失时执行(如 SysTrayIcon 被删除),’ 但指定的 TimeOut 时间到或鼠标点击 Balloon Tips 后的消失不发送此消息
Private Const NIN_BALLOONTIMEOUT = (WM_USER + &H4) ’ 当 Balloon Tips 的 TimeOut 时间到时执行
Private Const NIN_BALLOONUSERCLICK = (WM_USER + &H5) ’ 当鼠标点击 Balloon Tips 时执行。
’ 注意:在XP下执行时 Balloon Tips 上有个关闭按钮,
’ 如果鼠标点在按钮上将接收到 NIN_BALLOONTIMEOUT 消息。Private Declare Function Shell_NotifyIcon Lib “shell32.dll“ Alias “Shell_NotifyIconA“ (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
Private Declare Function RegisterWindowMessage Lib “user32“ Alias “RegisterWindowMessageA“ (ByVal lpString As String) As LongPrivate Type NOTIFYICONDATA
cbSize As Long ’ 结构大小(字节)
hwnd As Long ’ 处理消息的窗口的句柄
uId As Long ’ 唯一的标识符
uFlags As Long ’ Flags
uCallBackMessage As Long ’ 处理消息的窗口接收的消息
hIcon As Long ’ 托盘图标句柄
szTip As String * 128 ’ Tooltip 提示文本
dwState As Long ’ 托盘图标状态
dwStateMask As Long ’ 状态掩码
szInfo As String * 256 ’ 气球提示文本
uTimeoutOrVersion As Long ’ 气球提示消失时间或版本
’ uTimeout - 气球提示消失时间(单位:ms, 10000 -- 30000)
’ uVersion - 版本(0 for V4, 3 for V5)
szInfoTitle As String * 64 ’ 气球提示标题
dwInfoFlags As Long ’ 气球提示图标
End Type’ dwState to NOTIFYICONDATA structure
Private Const NIS_HIDDEN = &H1 ’ 隐藏图标
Private Const NIS_SHAREDICON = &H2 ’ 共享图标’ dwInfoFlags to NOTIFIICONDATA structure
’Private Const NIIF_NONE = &H0 ’ 无图标
’Private Const NIIF_INFO = &H1 ’ “消息“图标
’Private Const NIIF_WARNING = &H2 ’ “警告“图标
’Private Const NIIF_ERROR = &H3 ’ “错误“图标
Public Enum NIIF_InfoFlag
NIIF_NONE = &H0 ’ 无图标
NIIF_INFO = &H1 ’ “消息“图标
NIIF_WARNING = &H2 ’ “警告“图标
NIIF_ERROR = &H3 ’ “错误“图标
End Enum’ uFlags to NOTIFYICONDATA structure
Private Const NIF_MESSAGE As Long = &H1
Private Const NIF_ICON As Long = &H2
Private Const NIF_TIP As Long = &H4
Private Const NIF_STATE As Long = &H8
Private Const NIF_INFO As Long = &H10’ dwMessage to Shell_NotifyIcon
Private Const NIM_ADD As Long = &H0
Private Const NIM_DELETE As Long = &H2
Private Const NIM_MODIFY As Long = &H1
Private Const NIM_SETFOCUS As Long = &H3
Private Const lngNIM_SETVERSION As Long = &H4Private lngPreWndProc As Long
Private lngMsgBarRester As Long
Private OwerForm As Form

Private IconData As NOTIFYICONDATA’是否显示Tip信息
Private TipShowing As Boolean’--------------------------------------------------------------------------------
’*************************************************************************
’**函 数 名:AddNotifyIcon
’**输 入:frm(Form) - 窗体
’** :strTitle(String) - 托盘提示标题
’** :strInfo(String) - 托盘提示信息
’** :Optional lngType(Long = 1) - 托盘提示类型 0 无 1 信息 2 警告 3 错误
’** :Optional lngTime(Long = 10000) - 停留时间
’**输 出:无
’**功能描述:显示托盘图标提示信息
Public Sub AddNotifyIcon(frm As Form, strTitle As String, strInfo As String, Optional bTipMessage As Boolean = False, Optional lngType As NIIF_InfoFlag = NIIF_INFO, Optional lngTime As Long = -1)
’ 向托盘区添加图标

Set OwerForm = frm

With IconData
.cbSize = Len(IconData)
.hwnd = OwerForm.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
If bTipMessage Then .uFlags = .uFlags Or NIF_INFO Or NIF_STATE
.uCallBackMessage = WM_NOTIFYICON
.szTip = strTitle & vbNullChar
.hIcon = OwerForm.Icon.Handle
.dwState = 0
.dwStateMask = 0
.szInfo = strInfo & vbNullChar
.szInfoTitle = strTitle & vbNullChar
.dwInfoFlags = lngType
.uTimeoutOrVersion = lngTime
End With If lngPreWndProc = 0 Then ’没有初始化
Shell_NotifyIcon NIM_ADD, IconData
lngMsgBarRester = RegisterWindowMessage(“TaskBarCreated“)
lngPreWndProc = SetWindowLong(OwerForm.hwnd, GWL_WNDPROC, AddressOf NotifyIconWindowProc)
Else ’已初始化
Shell_NotifyIcon NIM_MODIFY, IconData
End IfEnd Sub’*************************************************************************
’**函 数 名:DeleteNotifyIcon
’**输 出:无
’**功能描述:删除托盘图标
Public Sub DeleteNotifyIcon()
If lngPreWndProc 《》 0 Then
’ 删除托盘区图标
With IconData
.cbSize = Len(IconData)
.hwnd = OwerForm.hwnd
.uId = 0
.uFlags = NIF_TIP Or NIF_ICON Or NIF_MESSAGE
.uCallBackMessage = WM_NOTIFYICON
.szTip = ““
.hIcon = OwerForm.Icon.Handle
End With
Shell_NotifyIcon NIM_DELETE, IconData
SetWindowLong OwerForm.hwnd, GWL_WNDPROC, lngPreWndProc
lngPreWndProc = 0
End If
End Sub’*************************************************************************
’**函 数 名:NotifyIconWindowProc
’**输 入:ByVal hwnd(Long) -
’** :ByVal msg(Long) -
’** :ByVal wParam(Long) -
’** :ByVal lParam(Long) -
’**输 出:(Long) -
’**功能描述:frmTest 窗口入口函数
’*************************************************************************
Function NotifyIconWindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
’ 拦截 WM_NOTIFYICON 消息
If msg = WM_NOTIFYICON Then
Select Case lParam

Case WM_LBUTTONUP
Debug.Print “左键单击“

Case WM_LBUTTONDBLCLK
Debug.Print “左键双击“

Case WM_RBUTTONUP
Debug.Print “右键单击“

Case WM_RBUTTONDBLCLK
Debug.Print “右键双击“

Case WM_MBUTTONUP
Debug.Print “中键单击“

Case WM_MBUTTONDBLCLK
Debug.Print “中键双击“

Case NIN_BALLOONSHOW
Debug.Print “显示气球提示“
TipShowing = True

Case NIN_BALLOONHIDE
Debug.Print “删除托盘图标“
TipShowing = False

Case NIN_BALLOONTIMEOUT
Debug.Print “气球提示消失“
TipShowing = False

Case NIN_BALLOONUSERCLICK
Debug.Print “单击气球提示“
TipShowing = False

Case WM_MOUSEISMOVING
’Debug.Print “鼠标移动“

Case Else
Debug.Print lParam

End Select
ElseIf msg = lngMsgBarRester Then
AddNotifyIcon OwerForm, IconData.szInfoTitle, IconData.szInfo
End If
NotifyIconWindowProc = CallWindowProc(lngPreWndProc, hwnd, msg, wParam, lParam)
End Function’*************************************************************************
’**函 数 名:ShowTrayTipMessage
’**输 出:无
’**功能描述:设置新的标题
Public Sub ShowTrayTipMessage(strTitle As String, strInfo As String, Optional lngType As NIIF_InfoFlag = NIIF_INFO, Optional lngTime As Long = -1)
With IconData
.uFlags = .uFlags Or NIF_INFO Or NIF_STATE
.szInfo = strInfo & vbNullChar
.szInfoTitle = strTitle & vbNullChar
.dwInfoFlags = lngType
.uTimeoutOrVersion = lngTime
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub’*************************************************************************
’**函 数 名:SetTrayTip
’**输 出:无
’**功能描述:设置新的标题
Public Sub SetTrayTip(tip As String)
With IconData
.szTip = tip & vbNullChar
.uFlags = NIF_TIP
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub’*************************************************************************
’**函 数 名:SetTrayIcon
’**输 出:无
’**功能描述:设置新的图标
Public Sub SetTrayIcon(pic As Picture)
If pic.Type 《》 vbPicTypeIcon Then Exit Sub With IconData
.hIcon = pic.Handle
.uFlags = NIF_ICON
End With
Shell_NotifyIcon NIM_MODIFY, IconData
End Sub
-registerwindowmessage