HOOK LISTVIEW的代码,就是取不到值
这个代码是HOOK监控LISTVIEW事件的,代码也是参考MSDN和C++例子来的。但就是取不到值
窗体代码:
程序代码:
Private Sub Command1_Click() ListView1.ListItems.Add , , "1234342" End Sub Private Sub Form_Load() InitalListView hHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf CallBackHookProc, App.hInstance, App.ThreadID) End Sub Private Sub Form_Terminate() Call UnhookWindowsHookEx(hHook) End Sub Private Sub Form_Unload(Cancel As Integer) Call UnhookWindowsHookEx(hHook) End Sub Sub InitalListView() ListView1.ListItems.Clear '清空列表 ListView1.ColumnHeaders.Clear '清空列表头 ListView1.View = lvwReport '设置列表显示方式 ListView1.GridLines = True '显示网络线 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.FullRowSelect = True '选择整行 ListView1.ColumnHeaders.Add , , "ID", 500 '给列表中添加列名 ListView1.ColumnHeaders.Add , , "本地 IP", 1500 ListView1.ColumnHeaders.Add , , "本地端口", 1200 ListView1.ColumnHeaders.Add , , "协议", 550 ListView1.ColumnHeaders.Add , , "远程 IP", 1500 ListView1.ColumnHeaders.Add , , "远程端口", 900 ListView1.ColumnHeaders.Add , , "当前状态", 900 ListView1.ColumnHeaders.Add , , "连接时间", 900 '------------------------------------------------------- Dim X X = ListView1.ListItems.Count + 1 ListView1.ListItems.Add , , X ListView1.ListItems(X).SubItems(1) = "00:00:00" ListView1.ListItems(X).SubItems(2) = "2008-01-01" ListView1.ListItems(X).SubItems(3) = "(无)" '------------------------------------------------------- ListView1.ListItems.Clear '清空列表 ListView1.ListItems.Add , , "1" ListView1.ListItems(1).SubItems(1) = "00:00:00" ListView1.ListItems(1).SubItems(2) = "2008-01-01" ListView1.ListItems(1).SubItems(3) = "(无)" ListView1.ListItems.Add , , "2" ListView1.ListItems(2).SubItems(1) = "00:00:01" ListView1.ListItems(2).SubItems(2) = "2008-01-01" ListView1.ListItems(2).SubItems(3) = "(无)" '------------------------------------------------------- ListView1.View = lvwReport '设置显示方式为列表 ListView1.AllowColumnReorder = True '对行进行程序排列,用鼠标进行排列 ListView1.Arrange = lvwAutoLeft '图标横排列 ListView1.Arrange = lvwAutoTop '图标竖排列 ListView1.FlatScrollBar = False '显示滚动条 ListView1.FlatScrollBar = True '隐藏滚动条 ListView1.FullRowSelect = True '选择整行 ListView1.LabelEdit = lvwManual '禁止标签编辑 ListView1.GridLines = True '显示网络线 ListView1.LabelWrap = True '图标可以换行 ListView1.MultiSelect = True '可以选择多个项目 ListView1.PictureAlignment = lvwTopLeft '图片对齐方式是左顶部,其他有右顶部(1)、左底部(2)、右底部(3)、居中(4)、平铺(5) ListView1.Checkboxes = True '显示复选框 'ListView1.DropHighlight = ListView1.ListItems.Item(2) '显示系统颜色 End Sub
模块代码:
程序代码:
Public 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 Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _ ByVal nCode As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, _ Source As Any, _ ByVal Length As Long) Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long Public Const WH_CALLWNDPROC = 4 Public Const WM_COMMAND = &H111 Public Const EN_CHANGE = &H300 Private Const WM_SYSCOMMAND = &H112 Public Const WM_NOTIFY = &H4E Public Const WM_NOTIFYFORMAT = &H55 Public Const LVM_FIRST = &H1000 Public Const LVM_INSERTITEM = (LVM_FIRST + 7) Public Const WM_NCLBUTTONDOWN = &HA1 '当鼠标左键在窗口非客户区按下 Public Const WM_NCLBUTTONUP = &HA2 '当鼠标左键在窗口非客户区放开 Public Type CWPSTRUCT lParam As Long '返回的是子窗口句柄 wParam As Long message As Long '返回的是消息类型,listview对应的是WM_NOTIFY hwnd As Long '返回的是父窗体句柄。 End Type Public Type NMHDR hwndFrom As Long idFrom As Long code As Long End Type Public Type NMLISTVIEW hdr As Long iItem As Long iSubItem As Long uNewState As Long uOldState As Long uChanged As Long ptAction As Long lParam As Long End Type Public Const HC_ACTION = 0 Public hHook As Long Public Function CallBackHookProc(ByVal nCode As Long, _ ByVal wParam As Long, ByVal lParam As Long) As Long Dim progressId As Long Dim strBuffer As NMLISTVIEW If nCode = HC_ACTION Then Dim Msg As CWPSTRUCT Call CopyMemory(Msg, ByVal lParam, Len(Msg)) If Msg.message = WM_NOTIFY Then '注意,一定要生成EXE测试,不然会取到工程窗口。 Call GetWindowThreadProcessId(Msg.hwnd, progressId) Form1.Text1.Text = progressId result = ReadProcessMemory(progressId, Msg.lParam, strBuffer, Len(strBuffer), 0) Form1.List1.AddItem strBuffer.iItem End If End If CallBackHookProc = CallNextHookEx(hHook, nCode, wParam, lParam) End Function