想遍歷獲取資源管理器中每一個子項的標題,百度了好久,做出了一小段代碼,但還是取不出類似 "最近訪問的職位" 這樣的標題,
類 - SysTreeView32,
hTreeView = 1379188 spy++查出來這個句柄,先直接用了,
我電腦中的Excel出現了問題,把主要code放在記事本中,請老師加以指點。

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As String, ByVal Source As Long, ByVal Length As Long)
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal flAllocationType As Long, ByVal flProtect As Long) As Long
Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long, ByVal dwFreeType As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, 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, llpBufferfer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Const TVGN_ROOT = &H0 '根項的第一個子項,指定項是該根項的一個部分
Private Const TVGN_CHILD = &H4 '第一個子項
Private Const TVGN_Next = &H1 '下一個兄弟項
Private Const TVGN_Caret = &H9 '選中的專案
Private Const TVGN_Previous = &H2 '前一個兄弟項
Private Const TVGN_Parent = &H3 '指定項的父項
Private Const TVGN_FirstVisible = &H5 '第一個可見的項
Private Const TVGN_NextVisible = &H6 '跟隨在指定項之后的下一個可視項
Private Const TVGN_PreviousVisible = &H7 '在指定項之前的第一個可視項
Private Const TVGN_LastVisible = &H10
Private Const TVGN_DropHilite = &H8 '上一次拖放操作的目標的項
Private Const TV_FIRST = &H1100
Private Const TVM_GETITEMRECT As Long = (TV_FIRST + 4)
Private Const TVM_GETNEXTITEM As Long = (TV_FIRST + 10)
Private Const TVM_SELECTITEM As Long = (TV_FIRST + 11)
Private Const TVM_DELETEITEM As Long = (TV_FIRST + 1)
Private Const TVM_GETCOUNT = (TV_FIRST + 5)
Private Const TVM_HITTEST = (TV_FIRST + 17)
Private Const TVM_GETITEM = (TV_FIRST + 12)
Private Const TVM_GETITEMA = (TV_FIRST + 12)
Private Const TVM_GETITEMW = (TV_FIRST + 12)
Private Const TVHT_ONITEMLABEL = &H4
Private Const TVIF_TEXT = &H1
Private Const GMEM_FIXED = &H0
Private Const TVM_EXPAND As Long = &H1102
Private Const WM_SETREDRAW As Long = &HB
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const MEM_DECOMMIT = &H4000
Private Const MEM_RELEASE = &H8000
Private Const MEM_COMMIT = &H1000
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Const MAX_LVMSTRING As Long = 255
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Private Type TVITEM
mask As Long
HTreeItem As Long
state As Long
stateMask As Long
pszText As Long
cchTextMax As Long
iImage As Long
iSelectedImage As Long
cChildren As Long
lParam As Long
End Type
Sub test2()
Dim Flag As Long '是否展開
Dim hTreeView As Long 'TreeView 控制元件句柄
Dim hTVRoot As Long '根節點句柄
Dim hTVItem As Long '子節點句柄
Dim vProcessId As Long
Dim vProcess As Long
Dim vPointer As Long
Dim vItemCount As Long
Dim pMyItemMemory As Long
Dim i As Long
Dim result As Long
Dim bytes As Long
Dim vItem As TVITEM
Dim tmpItem As TVITEM
Dim strBuffer() As Byte
Dim tmpString As String
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ReDim strBuffer(MAX_LVMSTRING)
hTreeView = 1379188 '獲得SysTreeView32控制元件句柄
Call GetWindowThreadProcessId(hTreeView, vProcessId)
vProcess = OpenProcess(PROCESS_ALL_ACCESS, False, vProcessId)
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'根節點 / 節點 / 展開
hTVRoot = SendMessage(hTreeView, TVM_GETNEXTITEM, TVGN_ROOT, ByVal 0&) '根節點句柄
hTVItem = SendMessage(hTreeView, TVM_GETNEXTITEM, TVGN_CHILD, ByVal hTVRoot) '根節點下第一個子節點句柄
'hTVItem = SendMessage(hTreeView, TVM_GETNEXTITEM, TVGN_Next, ByVal hTVItem) '根節點下第二個子節點句柄
SendMessage hTreeView, TVM_SELECTITEM, TVGN_Caret, ByVal hTVItem '選中指定節點
Flag = SendMessage(hTreeView, TVM_EXPAND, TVM_EXPAND, ByVal hTVRoot) '展開節點
vPointer = VirtualAllocEx(vProcess, ByVal 0&, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
With vItem
.mask = TVIF_TEXT
.HTreeItem = hTVItem
.pszText = vPointer
End With
result = WriteProcessMemory(vProcess, ByVal vPointer, vItem, LenB(vItem), 0)
result = SendMessage(hTreeView, TVM_GETITEMA, False, ByVal vPointer)
result = ReadProcessMemory(vProcess, ByVal vPointer, strBuffer(0), LenB(tmpItem), 0)
tmpString = StrConv(strBuffer, vbUnicode)
Call VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE)
Call CloseHandle(vProcess)
End Sub
uj5u.com熱心網友回復:
有老師能指點一下嗎?錯誤在哪里,如何修改轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/40380.html
標籤:API
