搜索
查看: 687|回复: 0

vb强大的窗口hook

[复制链接]

432

主题

573

帖子

2543

积分

核心成员

Rank: 8Rank: 8

积分
2543
发表于 2014-4-1 15:18:43 | 显示全部楼层 |阅读模式
  1. Option Explicit  Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hwnd As Long) As Long ''use in NT5
  2.   Option Explicit
  3.   Public Declare Function RegisterShellHookWindow Lib "user32" (ByVal hwnd As Long) As Long  ''use in NT5
  4.   Public Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
  5.   Public Declare Function DeregisterShellHookWindow Lib "user32" (ByVal hwnd As Long) As Long
  6.   Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
  7.   Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  8.   Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
  9.   Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthW" (ByVal hwnd As Long) As Long
  10.   Private Const HSHELL_WINDOWCREATED = 1 '' 顶级窗体被创建
  11.   Private Const HSHELL_WINDOWDESTROYED = 2 '' 顶级窗体即将被关闭
  12.   Private Const HSHELL_ACTIVATESHELLWINDOW = 3 '' SHELL 的主窗体将被激活
  13.   Private Const HSHELL_WINDOWACTIVATED = 4 '' 顶级窗体被激活
  14.   Private Const HSHELL_GETMINRECT = 5 '' 顶级窗体被最大化或最小化(本例未用)
  15.   Private Const HSHELL_REDRAW = 6 '' Windows 任务栏被刷新
  16.   Private Const HSHELL_TASKMAN = 7 '' 任务列表的内容被选中
  17.   Private Const HSHELL_LANGUAGE = 8 '' 中英文切换或输入法切换
  18.   Private Const HSHELL_SYSMENU = 9 ''显示系统菜单
  19.   Private Const HSHELL_ENDTASK = 10 ''顶级窗体被强制关闭
  20.   Private Const HSHELL_ACCESSIBILITYSTATE = 11
  21.   Private Const HSHELL_APPCOMMAND = 12 ''没有被程序处理的APPCOMMAND。见WM_APPCOMMAND
  22.   Private Const HSHELL_WINDOWREPLACED = 13 ''wParam=被替换的顶级窗口的hWnd
  23.   Private Const HSHELL_WINDOWREPLACING = 14 ''wParam=替换顶级窗口的窗口hWnd
  24.   Private Const HSHELL_HIGHBIT = &H8000& ''掩码
  25.   Private Const HSHELL_FLASH = (HSHELL_REDRAW Or HSHELL_HIGHBIT) ''标题闪烁
  26.   Private Const HSHELL_RUDEAPPACTIVATED = (HSHELL_WINDOWACTIVATED Or HSHELL_HIGHBIT) ''不知道干吗的
  27.   Public Const GWL_WNDPROC = -4
  28.   Private Const MAX_PATH = 260
  29.   Public Shell_Hook_Msg_ID As Long
  30.   Public LogWinOldProc As Long
  31.   Public Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  32.   Dim strCapTion As String
  33.   Select Case wMsg
  34.   Case Shell_Hook_Msg_ID
  35.   Dim szTmp As String
  36.   Select Case wParam
  37.   Case HSHELL_WINDOWACTIVATED
  38.   szTmp = String(MAX_PATH, vbNullChar)
  39.   Call GetWindowText(lParam, szTmp, MAX_PATH)
  40.   strCapTion = Left$(szTmp, GetWindowTextLength(lParam))
  41.   If InStr(strCapTion, "Windows 图片和传真查看器") <> 0 Then
  42.   MsgBox "抓图瞬间,这个时候可以抓图"   '---------这里可以换成窗体事件 比如:Form1.Timer1.Enabled = True    TIMER 控件的控制属性
  43.   End If
  44.   If InStr(LCase(strCapTion), "acdsee") <> 0 Then
  45.   MsgBox "抓图瞬间,这个时候可以抓图"
  46.   End If
  47.   If InStr(strCapTion, "画图") <> 0 Then
  48.   MsgBox "抓图瞬间,这个时候可以抓图"
  49.   End If
  50.   End Select
  51.   End Select
  52.   WndProc = CallWindowProc(LogWinOldProc, hwnd, wMsg, wParam, lParam)
  53.   End Function
  54.   Option Explicit
  55.   Private Sub Form_Load()
  56.   Shell_Hook_Msg_ID = RegisterWindowMessage("SHELLHOOK")
  57.   RegisterShellHookWindow (Me.hwnd)  '' 调用未公开的函数进行注册
  58.   LogWinOldProc = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf WndProc)
  59.   End Sub
  60.   Private Sub Form_Unload(Cancel As Integer)
  61.   DeregisterShellHookWindow Me.hwnd
  62.   SetWindowLong Me.hwnd, GWL_WNDPROC, LogWinOldProc
  63.   End Sub
复制代码
您可以更新记录, 让好友们知道您在做什么...
您需要登录后才可以回帖 登录 | Join BUC

本版积分规则

Powered by Discuz!

© 2012-2015 Baiker Union of China.

快速回复 返回顶部 返回列表