我想得到TreeView里面各项的文字
请问这样能行的吗?

解决方案 »

  1.   

    我也在找类似的代码..只找到delphi的,只有代码,没有文章,看不懂阿
      

  2.   

    Option Explicit
    Private Const LVM_FIRST = &H1000
    Private Const LVM_GETITEMCOUNT = (LVM_FIRST + 4)Private Const LVM_GETITEM = (LVM_FIRST + 5)
    Private Const LVM_GETSTRINGWIDTH = (LVM_FIRST + 17)
    Private Const LVM_GETCOLUMN = (LVM_FIRST + 25)
    Private Const LVM_GETITEMTEXT = (LVM_FIRST + 45)
    Private Const LVM_GETHEADER = LVM_FIRST + 31
    Private Const WC_HEADERA = "SysHeader32"
    Private Const WC_HEADER = WC_HEADERA
    Private Const HDM_FIRST = &H1200                    '// Header messages
    Private Const HDM_GETITEMCOUNT = (HDM_FIRST + 0)
    Private Const HDM_ORDERTOINDEX = (HDM_FIRST + 15)Private Const PROCESS_QUERY_INFORMATION = 1024
    Private Const PROCESS_VM_OPERATION = &H8
    Private Const PROCESS_VM_READ = &H10
    Private Const PROCESS_VM_WRITE = &H20
    Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
    Private Const MAX_LVMSTRING As Long = 255
    Private Const MEM_COMMIT = &H1000
    Private Const MEM_RELEASE = &H8000
    Private Const PAGE_READWRITE = &H4
    Private Const LVIF_TEXT As Long = &H1Private Type LV_ITEMA
       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 TypePrivate 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, ByRef 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 Any, 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
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Private Const TCM_FIRST = &H1300
    Private Const TCM_SETCURFOCUS = (TCM_FIRST + 48)Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
    Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As LongDim lngHwnd         As Long
    Dim CardNo As Long
    Dim DeviceNo As Long
    Dim SysTab As LongDim PrevCardNo As String
    Dim eSafeFile As String
    Public Function GetListviewItem(ByVal hWindow As Long, ByVal ProcessID As Long, ByVal pColumn As Long, ByVal pRow As Long) As String
        Dim result              As Long
        Dim myItem              As LV_ITEMA
        Dim pHandle             As Long
        Dim pStrBufferMemory    As Long
        Dim pMyItemMemory       As Long
        Dim strBuffer()         As Byte
        Dim index               As Long
        Dim tmpString           As String
        Dim strLength           As Long
        
        '**********************
        'init the string buffer
        '**********************
        ReDim strBuffer(MAX_LVMSTRING)
        
        '***********************************************************
        'open a handle to the process and allocate the string buffer
        '***********************************************************
        pHandle = OpenProcess(PROCESS_VM_OPERATION Or PROCESS_VM_READ Or PROCESS_VM_WRITE, False, ProcessID)
        pStrBufferMemory = VirtualAllocEx(pHandle, 0, MAX_LVMSTRING, MEM_COMMIT, PAGE_READWRITE)
            
        '************************************************************************************
        'initialize the local LV_ITEM structure
        'The myItem.iSubItem member is set to the index of the column that is being retrieved
        '************************************************************************************
        myItem.mask = LVIF_TEXT
        myItem.iSubItem = pColumn
        myItem.pszText = pStrBufferMemory
        myItem.cchTextMax = MAX_LVMSTRING
        
        '**********************************************************
        'write the structure into the remote process's memory space
        '**********************************************************
        pMyItemMemory = VirtualAllocEx(pHandle, 0, Len(myItem), MEM_COMMIT, PAGE_READWRITE)
        result = WriteProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
        
        '*************************************************************
        'send the get the item message and write back the memory space
        '*************************************************************
        result = SendMessage(hWindow, LVM_GETITEMTEXT, pRow, ByVal pMyItemMemory)
        result = ReadProcessMemory(pHandle, pStrBufferMemory, strBuffer(0), MAX_LVMSTRING, 0)
        result = ReadProcessMemory(pHandle, pMyItemMemory, myItem, Len(myItem), 0)
          
        '**************************************************
        'turn the byte array into a string and send it back
        '**************************************************
        
        tmpString = StrConv(strBuffer, vbUnicode)
        If InStr(tmpString, Chr(0)) > 0 Then
            tmpString = Left(tmpString, InStr(tmpString, Chr(0)) - 1)
        End If
        
        tmpString = Trim(tmpString)
        
        '**************************************************
        'deallocate the memory and close the process handle
        '**************************************************
        result = VirtualFreeEx(pHandle, pStrBufferMemory, 0, MEM_RELEASE)
        result = VirtualFreeEx(pHandle, pMyItemMemory, 0, MEM_RELEASE)
        
        result = CloseHandle(pHandle)
        
        If Len(tmpString) > 0 Then GetListviewItem = tmpStringEnd Function