Delphi更改另一个窗口的SysListView32项的文本颜色

时间:2014-06-27 00:49:53

标签: delphi

我如何更改另一个应用程序中SysListView32项目的文本颜色

下面的代码更改了另一个应用程序中SysListView32项目的文本颜色

procedure TMainForm.btn1Click(Sender: TObject);
begin
ChangingTextColor([Here is the SysListView32 hwnd],22,RGB(0,0,0));
end;

完整的程序如下:

procedure ChangingTextColor(LVHandle: HWND; N: Integer;ClrCode:Integer);
var
  lvi: LV_ITEM;
  _lvi: PLVITEM;
  Item: array[0..1000] of char;
  _Item, _subitem: PChar;
  pid: Cardinal;
  process: THANDLE;
  writebytes,readbytes:ULONG_PTR;
begin
  GetWindowThreadProcessId(LVHandle, @pid);

  process := OpenProcess(PROCESS_VM_OPERATION or PROCESS_VM_READ or PROCESS_VM_WRITE or PROCESS_QUERY_INFORMATION, FALSE, pid);
  Win32Check(process<>0);
  try
    _lvi := PLVITEM(VirtualAllocEx(process, Nil, sizeof(TLVITEM), MEM_COMMIT, PAGE_READWRITE));
    Win32Check(_lvi<>nil);
    try
      _item := PChar(VirtualAllocEx(process, Nil, 512, MEM_COMMIT, PAGE_READWRITE));
      Win32Check(_item<>nil);
      try
        _subitem :=PChar(VirtualAllocEx(process, Nil, 512, MEM_COMMIT, PAGE_READWRITE));
        Win32Check(_subitem<>nil);
        try
          lvi.cchTextMax := 512;
          lvi.iSubItem := 0;
          lvi.pszText := '55';
          lvi.mask := LVIF_STATE;
          lvi.iItem := N;
          lvi.state := LVIS_FOCUSED or LVIS_SELECTED;
          lvi.stateMask := LVIS_FOCUSED or LVIS_SELECTED;

          Win32Check(WriteProcessMemory(process, _lvi, @lvi, sizeof(TLVITEM), writebytes));
          Win32Check(ReadProcessMemory(process, _item, @item, 512, readbytes));

          **SendMessage(LVHandle, LVM_SETTEXTCOLOR, LPARAM(_lvi), ClrCode);
          SendMessage(LVHandle, WM_SETTINGCHANGE, 0, 0);**

        finally
          Win32Check(VirtualFreeEx(process, _subitem, 0, MEM_RELEASE));
        end;
      finally
        Win32Check(VirtualFreeEx(process, _item, 0, MEM_RELEASE));
      end;
    finally
      Win32Check(VirtualFreeEx(process, _lvi, 0, MEM_RELEASE));
    end;
  finally
    CloseHandle(process);
  end;

end;

问题:颜色(ClrCode)应该是整数还是TColor?

1 个答案:

答案 0 :(得分:2)

Read the documentationLVM_SETTEXTCOLOR不会将LVITEM作为输入,WParam必须为0,因此您的所有虚拟分配都是无用的(并非开始时它们是正确的):

procedure ChangingTextColor(LVHandle: HWND; ClrCode:Integer);
begin
    SendMessage(LVHandle, LVM_SETTEXTCOLOR, 0, ClrCode);
end;

在这种情况下,您只需使用ListView_SetTextColor()

ListView_SetTextColor(LVHandle, ClrCode);

无法使用ListView API设置每项文本颜色,没有用于此目的的命令。每项自定义需要ListView拥有的应用程序进行自定义绘制。除非您将自己的代码注入其他应用程序并通过子类化其绘图消息来控制ListView的绘图,否则这是您无法控制的。