懂HOOK API的给个VB的例子,是vb的,要全的,最好有注释, 如果好还会追加分! 100
2个回答
展开全部
'模块------------------------------
Option Explicit
'对话框句柄
Public hwndMsgBox As Long
'自定义类型
Public Type CUSTOM_MSG_PARAMS
hOwnerThread As Long
hOwnerWindow As Long
dwStyle As Long
bUseTimer As Boolean
dwTimerDuration As Long
dwTimerInterval As Long
dwTimerExpireButton As Long
dwTimerCountDown As Long
sTitle As String
sPrompt As String
End Type
Public Cmp As CUSTOM_MSG_PARAMS
'常数
Public Const MB_ICONINFORMATION As Long = &H40&
Private Const MB_ABORTRETRYIGNORE As Long = &H2&
Private Const MB_TASKMODAL As Long = &H2000&
Private Const MB_YESNOCANCEL As Long = &H3&
'Windows MessageBox 返回值
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const IDABORT = 3
Private Const IDRETRY = 4
Private Const IDIGNORE = 5
Private Const IDYES = 6
Private Const IDNO = 7
'这部分有用户自定义常量,来表示按钮的动作,在现有的MessageBox常量的基础上
Public Const MB_SELECTBEGINSKIP As Long = MB_YESNOCANCEL
Public Const IDSELECTYES = IDYES
Public Const IDSELECTNO = IDNO
Public Const IDSELECTCANCEL = IDCANCEL
Public Const IDPROMPT = &HFFFF&
'其它api常数
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
'用户自定义类型在hook时传递数据。
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type
'变量
Private MHP As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
'Public Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
'Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Public Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'当messagebox 显示时,我们改变标题,提示信息和按钮caption
If uMsg = HCBT_ACTIVATE Then
'在HCBT_ACTIVATE消息中,
'wparam参数是messagebox的句柄,在timer事件中需要使用
hwndMsgBox = wParam
'设置messagebox的按钮caption
SetDlgItemText wParam, IDSELECTYES, "是(&Y)"
SetDlgItemText wParam, IDSELECTNO, "否(&N)"
SetDlgItemText wParam, IDSELECTCANCEL, "取消"
'脱钩
UnhookWindowsHookEx MHP.hHook
End If
'正常处理继续
MsgBoxHookProc = False
End Function
Public Function TimedMessageBoxH(Cmp As CUSTOM_MSG_PARAMS) As Long
Dim hInstance As Long
Dim hThreadId As Long
'挂钩
hInstance = GetWindowLong(Cmp.hOwnerThread, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
'填写 MSGBOX_HOOK_PARAMS 结构值
'将hook设为其中一个参数,我们就能截获消息并能操作对话框。
With MHP
.hwndOwner = Cmp.hOwnerWindow
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
End With
'设置倒计数 0
Cmp.dwTimerCountDown = 0
'如果bUseTimer, 那么就enable timer. 因为
'MessageBox API 和 MsgBox 类似,必须关闭才能执行下一条语句。
'对话框一显示,timer的事件就动态更新message box的显示
With Form1.Timer1
.Interval = Cmp.dwTimerInterval
.Enabled = Cmp.bUseTimer
End With
'调用 MessageBox API
TimedMessageBoxH = MessageBox(Cmp.hOwnerWindow, Cmp.sPrompt, Cmp.sTitle, Cmp.dwStyle)
'timer不需要了
Form1.Timer1.Enabled = False
End Function
'窗口-------------------------
'form1中添加text1,command1,timer1
Option Explicit
Private Sub Command1_Click()
'显示messagebox,
'传递 CUSTOM_MSG_PARAMS结构
With Cmp
.sTitle = "倒计时 MessageBox Hook Demo"
.sPrompt = "要马上执行,选择是." & vbCrLf & "或点击否." & vbCrLf & vbCrLf & "将在 10 后自动关闭!" & Space$(20)
.dwStyle = MB_SELECTBEGINSKIP Or MB_ICONINFORMATION
.bUseTimer = True '如果true,timer会更新显示每 dwTimerInterval
.dwTimerDuration = 10 '等待秒数
.dwTimerInterval = 1000 '倒计时秒数
.dwTimerExpireButton = IDSELECTNO 'timeout执行默认动作
.dwTimerCountDown = 0 '
.hOwnerThread = Me.hwnd '调用窗口句柄
.hOwnerWindow = Me.hwnd '包含窗口(me.hwnd or desktop).
End With
Select Case TimedMessageBoxH(Cmp)
Case IDSELECTYES: Text1.Text = "在 timeout 前 是 按钮被按下"
Case IDSELECTNO: Text1.Text = "按下 否 或者 timeout "
Case IDSELECTCANCEL: Text1.Text = " timeout 前 按下 取消"
End Select
End Sub
Private Sub Timer1_Timer()
Dim hWndTargetBtn As Long
If hwndMsgBox <> 0 Then
'计数
Cmp.dwTimerCountDown = Cmp.dwTimerCountDown + 1
'更新提示计时信息
SetDlgItemText hwndMsgBox, IDPROMPT, _
"要马上执行,选择是." & vbCrLf & "或点击否." & vbCrLf & vbCrLf & "将在 " & CStr(10 - Cmp.dwTimerCountDown) & " 秒后自动关闭!"
'如果倒计时完毕,要模拟点击
If Cmp.dwTimerCountDown = Cmp.dwTimerDuration Then
'停止timer
Timer1.Enabled = False
'获取按钮句柄
hWndTargetBtn = GetDlgItem(hwndMsgBox, Cmp.dwTimerExpireButton)
If hWndTargetBtn <> 0 Then
'在按钮上设置焦点
Call PutFocus(hWndTargetBtn)
'给PutFocus时间
DoEvents
'模拟点击
Call SendMessage(hWndTargetBtn, WM_LBUTTONDOWN, 0, 0)
Call SendMessage(hWndTargetBtn, WM_LBUTTONUP, 0, 0)
End If
End If
End If
End Sub
Option Explicit
'对话框句柄
Public hwndMsgBox As Long
'自定义类型
Public Type CUSTOM_MSG_PARAMS
hOwnerThread As Long
hOwnerWindow As Long
dwStyle As Long
bUseTimer As Boolean
dwTimerDuration As Long
dwTimerInterval As Long
dwTimerExpireButton As Long
dwTimerCountDown As Long
sTitle As String
sPrompt As String
End Type
Public Cmp As CUSTOM_MSG_PARAMS
'常数
Public Const MB_ICONINFORMATION As Long = &H40&
Private Const MB_ABORTRETRYIGNORE As Long = &H2&
Private Const MB_TASKMODAL As Long = &H2000&
Private Const MB_YESNOCANCEL As Long = &H3&
'Windows MessageBox 返回值
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const IDABORT = 3
Private Const IDRETRY = 4
Private Const IDIGNORE = 5
Private Const IDYES = 6
Private Const IDNO = 7
'这部分有用户自定义常量,来表示按钮的动作,在现有的MessageBox常量的基础上
Public Const MB_SELECTBEGINSKIP As Long = MB_YESNOCANCEL
Public Const IDSELECTYES = IDYES
Public Const IDSELECTNO = IDNO
Public Const IDSELECTCANCEL = IDCANCEL
Public Const IDPROMPT = &HFFFF&
'其它api常数
Private Const WH_CBT = 5
Private Const GWL_HINSTANCE = (-6)
Private Const HCBT_ACTIVATE = 5
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
'用户自定义类型在hook时传递数据。
Private Type MSGBOX_HOOK_PARAMS
hwndOwner As Long
hHook As Long
End Type
'变量
Private MHP As MSGBOX_HOOK_PARAMS
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
'Public Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
Private Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As String, ByVal lpCaption As String, ByVal wType As Long) As Long
'Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function PutFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Public Declare Function SetDlgItemText Lib "user32" Alias "SetDlgItemTextA" (ByVal hDlg As Long, ByVal nIDDlgItem As Long, ByVal lpString As String) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Public Function MsgBoxHookProc(ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'当messagebox 显示时,我们改变标题,提示信息和按钮caption
If uMsg = HCBT_ACTIVATE Then
'在HCBT_ACTIVATE消息中,
'wparam参数是messagebox的句柄,在timer事件中需要使用
hwndMsgBox = wParam
'设置messagebox的按钮caption
SetDlgItemText wParam, IDSELECTYES, "是(&Y)"
SetDlgItemText wParam, IDSELECTNO, "否(&N)"
SetDlgItemText wParam, IDSELECTCANCEL, "取消"
'脱钩
UnhookWindowsHookEx MHP.hHook
End If
'正常处理继续
MsgBoxHookProc = False
End Function
Public Function TimedMessageBoxH(Cmp As CUSTOM_MSG_PARAMS) As Long
Dim hInstance As Long
Dim hThreadId As Long
'挂钩
hInstance = GetWindowLong(Cmp.hOwnerThread, GWL_HINSTANCE)
hThreadId = GetCurrentThreadId()
'填写 MSGBOX_HOOK_PARAMS 结构值
'将hook设为其中一个参数,我们就能截获消息并能操作对话框。
With MHP
.hwndOwner = Cmp.hOwnerWindow
.hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, hInstance, hThreadId)
End With
'设置倒计数 0
Cmp.dwTimerCountDown = 0
'如果bUseTimer, 那么就enable timer. 因为
'MessageBox API 和 MsgBox 类似,必须关闭才能执行下一条语句。
'对话框一显示,timer的事件就动态更新message box的显示
With Form1.Timer1
.Interval = Cmp.dwTimerInterval
.Enabled = Cmp.bUseTimer
End With
'调用 MessageBox API
TimedMessageBoxH = MessageBox(Cmp.hOwnerWindow, Cmp.sPrompt, Cmp.sTitle, Cmp.dwStyle)
'timer不需要了
Form1.Timer1.Enabled = False
End Function
'窗口-------------------------
'form1中添加text1,command1,timer1
Option Explicit
Private Sub Command1_Click()
'显示messagebox,
'传递 CUSTOM_MSG_PARAMS结构
With Cmp
.sTitle = "倒计时 MessageBox Hook Demo"
.sPrompt = "要马上执行,选择是." & vbCrLf & "或点击否." & vbCrLf & vbCrLf & "将在 10 后自动关闭!" & Space$(20)
.dwStyle = MB_SELECTBEGINSKIP Or MB_ICONINFORMATION
.bUseTimer = True '如果true,timer会更新显示每 dwTimerInterval
.dwTimerDuration = 10 '等待秒数
.dwTimerInterval = 1000 '倒计时秒数
.dwTimerExpireButton = IDSELECTNO 'timeout执行默认动作
.dwTimerCountDown = 0 '
.hOwnerThread = Me.hwnd '调用窗口句柄
.hOwnerWindow = Me.hwnd '包含窗口(me.hwnd or desktop).
End With
Select Case TimedMessageBoxH(Cmp)
Case IDSELECTYES: Text1.Text = "在 timeout 前 是 按钮被按下"
Case IDSELECTNO: Text1.Text = "按下 否 或者 timeout "
Case IDSELECTCANCEL: Text1.Text = " timeout 前 按下 取消"
End Select
End Sub
Private Sub Timer1_Timer()
Dim hWndTargetBtn As Long
If hwndMsgBox <> 0 Then
'计数
Cmp.dwTimerCountDown = Cmp.dwTimerCountDown + 1
'更新提示计时信息
SetDlgItemText hwndMsgBox, IDPROMPT, _
"要马上执行,选择是." & vbCrLf & "或点击否." & vbCrLf & vbCrLf & "将在 " & CStr(10 - Cmp.dwTimerCountDown) & " 秒后自动关闭!"
'如果倒计时完毕,要模拟点击
If Cmp.dwTimerCountDown = Cmp.dwTimerDuration Then
'停止timer
Timer1.Enabled = False
'获取按钮句柄
hWndTargetBtn = GetDlgItem(hwndMsgBox, Cmp.dwTimerExpireButton)
If hWndTargetBtn <> 0 Then
'在按钮上设置焦点
Call PutFocus(hWndTargetBtn)
'给PutFocus时间
DoEvents
'模拟点击
Call SendMessage(hWndTargetBtn, WM_LBUTTONDOWN, 0, 0)
Call SendMessage(hWndTargetBtn, WM_LBUTTONUP, 0, 0)
End If
End If
End If
End Sub
追问
你这个其实也不算是一个HOOK API的例子,只是钩住消息了而已,并没有钩住API
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询