1,184
社区成员
发帖
与我相关
我的任务
分享
procedure InsertListViewItem(hwdLV:HWND; index:integer; str:string); //添加数据
var
pid:Cardinal;
hProc:Cardinal;
ptLV:PLVItem;
tLV:TLVItem;
ptItem:Pchar;
numByte:Cardinal;
strItem:array[0..255] of Char;
begin
if hwdLV = 0 then
Exit;
GetWindowThreadProcessId(hwdLV, pid);
hProc:=OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE or PROCESS_QUERY_INFORMATION, FALSE, pid);
if hProc = 0 then
Exit;
ptLV:=VirtualAllocEx(hProc, nil, sizeof(TLVItem), MEM_COMMIT, PAGE_READWRITE);
ptItem:=VirtualAllocEx(hProc, nil, sizeof(strItem), MEM_COMMIT, PAGE_READWRITE);
ZeroMemory(@tLV, sizeof(TLVItem));
tLV.mask:=LVIF_TEXT;
tLV.iItem:=index;
tLV.iSubItem:=0;
Move(str[1],strItem[0],Length(str)+1);
tLV.pszText:=ptItem;
WriteProcessMemory(hProc, ptItem, @strItem, sizeof(strItem), numByte);
WriteProcessMemory(hProc, ptLV, @tLV, sizeof(TLVItem), numByte);
SendMessage(hwdLV, LVM_INSERTITEM, 0, Longint(ptLV));
VirtualFreeEx(hProc, ptLV, 0, MEM_RELEASE);
VirtualFreeEx(hProc, ptItem, 0, MEM_RELEASE);
CloseHandle(hProc);
end;
function ListView_InsertItem(hWnd: HWND; const pItem: TLVItem): Integer;
begin
Result := Integer(SendMessage(hWnd, LVM_INSERTITEM, 0, LPARAM(@pItem)));
end;
参考这个,增加新行,ListView_SetItemCount...那几个添加列(如果需要),注意pItem: TLVItem数据必须在目标进程中,可以用VirtualQueryEx在目标进程中申请内存,并写入TLVItem结构体,然后把这个内存地址的指针当然LPARAM传递给ListView的句柄,就可以添加了。
这些函数都在系统自带的单元 CommCtrl.pas中