Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1680646
  • 博文数量: 584
  • 博客积分: 13857
  • 博客等级: 上将
  • 技术积分: 11883
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-16 09:34

分类: WINDOWS

2011-08-10 14:46:36

  1. (*//
  2. 标题:获取其他进程中ListView的文本
  3. 说明:Window2000+Delphi6调试通过
  4. 设计:Zswang
  5. 支持:wjhu111@21cn.com
  6. 日期:2004-03-25
  7. //*)

  8. uses CommCtrl;

  9. function ListViewColumnCount(mHandle: THandle): Integer;
  10. begin
  11.    Result := Header_GetItemCount(ListView_GetHeader(mHandle));
  12. end; { ListViewColumnCount }

  13. function GetListViewText(mHandle: THandle; mStrings: TStrings): Boolean;
  14. var
  15.    vColumnCount: Integer;
  16.    vItemCount: Integer;
  17.    I, J: Integer;
  18.    vBuffer: array[0..255] of Char;
  19.    vProcessId: DWORD;
  20.    vProcess: THandle;
  21.    vPointer: Pointer;
  22.    vNumberOfBytesRead: Cardinal;
  23.    S: string;
  24.    vItem: TLVItem;
  25. begin
  26.    Result := False;
  27.    if not Assigned(mStrings) then Exit;
  28.    vColumnCount := ListViewColumnCount(mHandle);
  29.    if vColumnCount <= 0 then Exit;
  30.    vItemCount := ListView_GetItemCount(mHandle);
  31.    GetWindowThreadProcessId(mHandle, @vProcessId);
  32.    vProcess := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or
  33.        PROCESS_VM_WRITE, False, vProcessId);
  34.    vPointer := VirtualAllocEx(vProcess, nil, 4096, MEM_RESERVE or MEM_COMMIT,
  35.        PAGE_READWRITE);
  36.    mStrings.BeginUpdate;
  37.    try
  38.        mStrings.Clear;
  39.        for I := 0 to vItemCount - 1 do begin
  40.            S := ' ';
  41.            for J := 0 to vColumnCount - 1 do begin
  42.                with vItem do begin
  43.                    mask := LVIF_TEXT;
  44.                    iItem := I;
  45.                    iSubItem := J;
  46.                    cchTextMax := SizeOf(vBuffer);
  47.                    pszText := Pointer(Cardinal(vPointer) + SizeOf(TLVItem));
  48.                end;
  49.                WriteProcessMemory(vProcess, vPointer, @vItem,
  50.                    SizeOf(TLVItem), vNumberOfBytesRead);
  51.                SendMessage(mHandle, LVM_GETITEM, I, lparam(vPointer));
  52.                ReadProcessMemory(vProcess, Pointer(Cardinal(vPointer) + SizeOf(TLVItem)),
  53.                    @vBuffer[0], SizeOf(vBuffer), vNumberOfBytesRead);
  54.                S := S + #9 + vBuffer;
  55.            end;
  56.            Delete(S, 1, 1);
  57.            mStrings.Add(S);
  58.        end;
  59.    finally
  60.        VirtualFreeEx(vProcess, vPointer, 0, MEM_RELEASE);
  61.        CloseHandle(vProcess);
  62.        mStrings.EndUpdate;
  63.    end;
  64.    Result := True;
  65. end; { GetListViewText }

  66. //Example
  67. procedure TForm1.FormCreate(Sender: TObject);
  68. begin
  69.    RegisterHotKey(Handle, 1, MOD_WIN, VK_F2);
  70. end;

  71. procedure TForm1.FormDestroy(Sender: TObject);
  72. begin
  73.    UnRegisterHotKey(Handle, 1);
  74. end;

  75. procedure TForm1.WMHOTKEY(var Msg: TWMHOTKEY);
  76. begin
  77.    case Msg.HotKey of
  78.        1:
  79.            GetListViewText(
  80.                WindowFromPoint(Point(Mouse.CursorPos.X, Mouse.CursorPos.Y)),
  81.                MemoText.Lines);
  82.    end;
  83. end;
阅读(668) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~