由于工作需要原因,我写的程序需要读取VirtualTrees里的一个特定值,如"照片2",判断是否有,如果有,是否被选中.
因为那个有VirtualTrees控件的程序不是本人写的,是另外一个公司开发的,
如果是treeview的话是没问题的,我已经写好,不过用在VirtualTrees上不行.
以下是取treeview值的方法:
function TreeNodeGetNext(mHandle: THandle; mTreeItem: HTreeItem): HTreeItem;
var
vParentID: HTreeItem;
begin
Result := nil;
if (mHandle <> 0) and (mTreeItem <> nil) then begin
Result := TreeView_GetChild(mHandle, mTreeItem);
if Result = nil then
Result := TreeView_GetNextSibling(mHandle, mTreeItem);
vParentID := mTreeItem;
while (Result = nil) and (vParentID <> nil) do begin
vParentID := TreeView_GetParent(mHandle, vParentID);
Result := TreeView_GetNextSibling(mHandle, vParentID);
end;
end;
end; { TreeNodeGetNext }
function TreeNodeGetLevel(mHandle: THandle; mTreeItem: HTreeItem): Integer;
var
vParentID: HTreeItem;
begin
Result := -1;
if (mHandle <> 0) and (mTreeItem <> nil) then begin
vParentID := mTreeItem;
repeat
Inc(Result);
vParentID := TreeView_GetParent(mHandle, vParentID);
until vParentID = nil;
end;
end; { TreeNodeGetLevel }
procedure GetTreeViewText(mHandle: THandle);
var
vItemCount: Integer;
vBuffer: array[0..255] of Char;
vProcessId: DWORD;
vProcess: THandle;
vPointer: Pointer;
vNumberOfBytesRead: Cardinal;
I: Integer;
vItem: TTVItem;
vTreeItem: HTreeItem;
tmpstr:string;
h1:Hwnd;
begin
tmpstr:='';
GetWindowThreadProcessId(mHandle, @vProcessId);
vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, vProcessId);
vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,PAGE_READWRITE);
try
vItemCount := TreeView_GetCount(mHandle);
vTreeItem := TreeView_GetRoot(mHandle);
for I := 0 to vItemCount - 1 do begin
with vItem do begin
mask := TVIF_TEXT;
cchTextMax := SizeOf(vBuffer);
pszText := Pointer(Cardinal(vPointer) + SizeOf(vItem));
hItem := vTreeItem;
end;
WriteProcessMemory(vProcess, vPointer, @vItem,SizeOf(vItem), vNumberOfBytesRead);
SendMessage(mHandle, TVM_GETITEM, 0, lparam(vPointer));
ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)), @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);
tmpStr:=lowercase(StringOfChar(#9, TreeNodeGetLevel(mHandle, vTreeItem)) + vBuffer);
showmessage(tmpstr);
if (pos('照片2',tmpstr)>0) or (pos('照片1',tmpstr)>0) then
begin
h1:=GetParent(mHandle);
end else
vTreeItem := TreeNodeGetNext(mHandle, vTreeItem);
end;
finally
VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
CloseHandle(vProcess);
end;
end;
function GetTreeSelectText(const TreeHand: HWND): String;
var
hCurItem: HTREEITEM;
lNodeText: String;
ltvItem: TTVItem;
h: HWND;
C, i: integer;
Buff: PChar;
S: array[0..1024] of char;
PID: THandle;
PRC: THandle;
R: Cardinal;
begin
Result := '';
hCurItem := HTREEITEM(SendMessage( TreeHand, TVM_GETNEXTITEM, TVGN_CARET, 0 ));
if (hCurItem = nil) then Exit;
GetWindowThreadProcessId(TreeHand, @PID);
PRC := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE, False, PID);
Buff := VirtualAllocEx(PRC, nil, 4096, MEM_RESERVE or MEM_COMMIT, PAGE_READWRITE);
FillChar(ltvItem, SizeOf(ltvItem), 0);
with ltvItem do
begin
mask := TVIF_TEXT;
hItem := hCurItem;
cchTextMax := 2048;
pszText := PChar(integer(Buff) + SizeOf(ltvItem) * 4);
end;
WriteProcessMemory(PRC, Buff, @ltvItem, Sizeof(ltvItem), R);
SendMessage(TreeHand, TVM_GETITEM, 0, LongInt(Buff));
ReadProcessMemory(PRC, Buff, @ltvItem, Sizeof(ltvItem), R);
ReadProcessMemory(PRC, PChar(integer(Buff) + SizeOf(ltvItem) * 4), @S[0], SizeOf(S), R);
Result := Trim(S);
VirtualFreeEx(PRC, Buff, 0, MEM_RELEASE);
CloseHandle(PRC);
end;