vb利用SendMessage获取外部程序listview数据失败,返回空值,求解~~
有人说是因为listview是属于 OwnerDraw 风格,所以无法获取数据。我用spy查看了
类名 SysListView32
窗口样式是WS_CHILD|WS_HSCROLL|WS_VISIBLE
扩展样式是WS_EX_CLIENTEDGE
我在实际运行中,发现行数是可以取到,但就是文本数据取不到。
相关代码如下
程序代码:
...省略定义 Private Type LVITEM mask As Long iItem As Long iSubItem As Long State As Long stateMask As Long pszText As Long cchTextMax As Long iImage As Long lParam As Long iIndent As Long End Type Private Sub Command1_Click() Dim hWnd As Long hWnd = FindWindow(vbNullString, "GAINCNGTMGR - Sterling Trader(R) Manager") 'hWnd = FindWindowEx(hWnd, 0, "#32770", vbNullString) hWnd = FindWindowEx(hWnd, 0, "SysListView32", vbNullString) Call ListView_SetItemText(hWnd, 5, 0, "12345667") '这句执行了无效 Call ListView_DelItem(hWnd, 5) '这句执行了看到目标sylistview32 已经删掉了第5行,但马上sylistview32又自动出现刚才被删掉的那行,出现在尾行,意思就是仍然无法删除 End Sub '************************************************************************* '**函 数 名:ListView_SetItemText '**输 入:ByVal hWnd(Long) - ListView控件句柄 '** :ByVal iItem(Long) - 行 '** :ByVal iSubItem(Long) - 列 '** :ByVal ItemText(String) - 更改的内容 '**输 出:(Boolean) - '**功能描述:更改其他程序ListView控件中某个Item的内容 '**全局变量: '**调用模块: '**作 者:bbb620 '**日 期:2008-07-27 13:26:03 '**修 改 人: '**日 期: '**版 本:V1.0.0 '************************************************************************* Public Function ListView_SetItemText(ByVal hWnd As Long, ByVal iItem As Long, ByVal iSubItem As Long, ByVal ItemText As String) As Boolean Dim PID As Long Dim hProcess As Long Dim nSize As Long Dim plvItem As Long Dim p_MyItemText As Long Dim myItem As LVITEM nSize = LenB(StrConv(ItemText, vbFromUnicode)) '获取字符串长度 GetWindowThreadProcessId hWnd, PID '获取与指定窗口关联在一起的一个进程和线程标识符 hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, PID) '打开一个现有进程的句柄 If hProcess <> 0 Then plvItem = VirtualAllocEx(hProcess, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE) '分配内存 p_MyItemText = VirtualAllocEx(hProcess, 0, nSize, MEM_COMMIT, PAGE_READWRITE) '分配内存 '初始化结构 myItem.iSubItem = iSubItem myItem.pszText = p_MyItemText If plvItem And p_MyItemText Then Call WriteProcessMemory(hProcess, p_MyItemText, ByVal ItemText, nSize, 0) '写入内存 Call WriteProcessMemory(hProcess, plvItem, myItem, Len(myItem), 0) '写入内存 Call SendMessage(hWnd, LVM_SETITEMTEXT, iItem, ByVal plvItem) ListView_SetItemText = True '释放内存 CloseHandle (hWnd) CloseHandle (hProcess) Call VirtualFreeEx(hProcess, plvItem, 0, MEM_RELEASE) Call VirtualFreeEx(hProcess, p_MyItemText, 0, MEM_RELEASE) End If End If End Function '************************************************************************* '**函 数 名:ListView_DelItem '**输 入:ByVal hwnd(Long) - ListView控件句柄 '** :ByVal i(Long) - 行 '**输 出:无 '**功能描述:删除其他程序ListView控件中的某行Item '************************************************************************* Public Function ListView_DelItem(ByVal hWnd As Long, ByVal i As Long) SendMessage hWnd, LVM_DELETEITEM, i, 0 End Function我本来是用c#的,在c#下也是无法取得listview的数据,后来参考vb代码以为可以解决,谁知道仍然是一样。
这段代码vb中尝试修改某个items的数据项,也是失败,运行中我发现listview那个目标闪了一下,但原来的值不变。删除的时候,明明看到已经删 除了,但一会马上又出现了原来那一行。难道目标的listview是禁止了SendMessage 消息,不可修改也不可取值的?
我在别处发帖子发了几天了,无人解决,所以到这边求教,希望遇到高手指点一下!
谢谢!!!!!!