不完善的可以获取其它窗口内容的代码,
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long '打开进程Private Declare Function VirtualAllocEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long '获取内存空间
Private Declare Function VirtualFreeEx Lib "kernel32" (ByVal hProcess As Long, ByVal lpAddress As Long, ByVal dwSize As Long, ByVal dwFreeType As Long) As Long '释放内存空间
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByVal lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long '向内存写数据
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long '向内存读数据
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByRef lParam As Any) As Long '发送消息
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long '关闭进程
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long '获得进程ID
'常数申明
Const LVM_FIRST As Long = &H1000
Const LVM_GETHEADER As Long = LVM_FIRST + 31
Const LVM_GETITEMCOUNT As Long = LVM_FIRST + 4
Const LVM_GETITEMTEXT As Long = LVM_FIRST + 45
Const HDM_FIRST As Long = &H1200
Const HDM_GETITEMCOUNT As Long = (HDM_FIRST + 0)
Const PROCESS_VM_OPERATION As Long = &H8
Const PROCESS_VM_READ As Long = &H10
Const PROCESS_VM_WRITE As Long = &H20
Const MAX_LVMSTRING As Long = 255
Const MEM_COMMIT As Long = &H1000
Const MEM_RELEASE As Long = &H8000&
Const PAGE_READWRITE As Long = &H4
Const LVIF_TEXT As Long = &H1
Option Explicit
Private Type LV_ITEMA
mask As Long
iItem As Long
iSubItem As Long
State As Long
stateMask As Long
pszText As Long
cchTextMax As Long
End Type
'类型申明
Private Sub Form_Load()
End Sub
'API申明
Public Function GetListViewTextArray(ByVal hWindow As Long) As String()
Dim myItem() As LV_ITEMA
Dim PHandle As Long
Dim ProcessId As Long
Dim PStrBufferMemory As Long
Dim PMyItemMemory As Long
Dim StrBuffer(MAX_LVMSTRING) As Byte
Dim TmpString As String
Dim Ih As Long, J As Long, HCount As Long
Dim StrArr() As String, ItemString As String
Dim Ji As Long, MyItemLength() As Long
GetWindowThreadProcessId hWindow, ProcessId
HCount = SendMessage(hWindow, LVM_GETHEADER, 0, 0) '获取列数
If HCount > 0 Then
HCount = SendMessage(HCount, HDM_GETITEMCOUNT, 0, 0) - 1
Else 'NOT HCOUNT...
HCount = 0
End If
PHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, 0, ProcessId)
ReDim myItem(HCount)
ReDim MyItemLength(HCount)
PStrBufferMemory = VirtualAllocEx(PHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
PMyItemMemory = VirtualAllocEx(PHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
Ji = SendMessage(hWindow, LVM_GETITEMCOUNT, 0, 0) - 1
On Error GoTo err1
ReDim StrArr(Ji)
For Ih = 0 To HCount
myItem(Ih).mask = LVIF_TEXT
myItem(Ih).iSubItem = Ih
myItem(Ih).pszText = PStrBufferMemory
myItem(Ih).cchTextMax = MAX_LVMSTRING
MyItemLength(Ih) = Len(myItem(Ih))
Next Ih
For J = 0 To Ji
ItemString = ""
For Ih = 0 To HCount
WriteProcessMemory PHandle, PMyItemMemory, myItem(Ih), MyItemLength(Ih), 0
If SendMessage(hWindow, LVM_GETITEMTEXT, J, ByVal PMyItemMemory) > 0 Then
ReadProcessMemory PHandle, PStrBufferMemory, StrBuffer(0), MAX_LVMSTRING, 0
TmpString = StrConv(StrBuffer, vbUnicode)
TmpString = Left(TmpString, InStr(TmpString, vbNullChar) - 1)
ItemString = ItemString & TmpString & Chr(9) ' Chr$(32)
End If
Next Ih
If ItemString <> "" Then
StrArr(J) = Left(ItemString, Len(ItemString) - 1)
End If
Next J
VirtualFreeEx PHandle, PMyItemMemory, 0, MEM_RELEASE
VirtualFreeEx PHandle, PStrBufferMemory, 0, MEM_RELEASE
CloseHandle (PHandle)
ItemString = ""
GetListViewTextArray = StrArr
Exit Function
err1:
MsgBox "不是Listview类吧?", vbInformation
End Function
请高手给完善一下,