#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2008
    Posts
    4
    Rep Power
    0

    Grabbing Text from a TListView item in another application when you know its Handle


    Hi all.

    I'm trying to make a small utility that will capture the contents of a TListView component (all the TListItems, their text and substrings) and either place them in a text file or copy them to another TListview in the local application.

    I wrote a small app here that uses 3 lables, a timer that ticks every 50 ms - it looks at the control under the current mouse position and displays its class and text.

    When it finds a TListView (delphi native tlistview, not winapi listview) I want it to populate the listview component with all the objects from the external windows tlistview.

    Any help would be appreciated, I think I have to use LVM_ messages but I dont know much about this though it should be simple enough.

    unit umain;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ExtCtrls, StdCtrls, ComCtrls, CommCtrl;

    type
    TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Timer1: TTimer;
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    private
    { Private declarations }
    procedure ShowHwndAndClassName(CrPos: TPoint) ;
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.ShowHwndAndClassName(CrPos: TPoint) ;
    var
    hWnd: THandle;
    aName,
    Text : array [0..255] of char;
    islistview : boolean;
    LF: TLogFont;
    hHeader, hCurrFont, hOldFont, hHeaderFont: THandle;
    const
    LVM_GETHEADER = LVM_FIRST + 31;

    begin
    islistview := false;

    hWnd := WindowFromPoint(CrPos) ;
    Label1.Caption := 'Handle : ' + IntToStr(hWnd) ;

    if boolean(GetClassName(hWnd, aName, 256)) then
    Label2.Caption := 'ClassName : ' + string(aName)
    else
    Label2.Caption := 'ClassName : not found';
    SendMessage(hWnd, WM_GETTEXT,
    SizeOf(Text), integer(@Text)) ;
    Label3.Caption := 'Text :' + Text;

    if string(aName) = 'TListView' then ListView1.Visible := True else listview1.Visible := false;
    if string(aName) = 'TListView' then islistview := true;

    if islistview then
    Begin
    hHeader := SendMessage(hWnd,LVM_GETHEADER,0,0);

    hCurrFont := SendMessage(hHeader, WM_GETFONT,0,0);

    if GetObject(hCurrFont, SizeOf(LF), Addr(LF)) > 0 then
    begin
    {set our custom attributes. I set a bold and underlined font style}
    LF.lfWeight := FW_BOLD;
    LF.lfUnderline := 1;
    End;


    End;


    end;

    procedure TForm1.Timer1Timer(Sender: TObject);

    var
    rPos: TPoint;
    begin
    if boolean(GetCursorPos(rPos))
    then ShowHwndAndClassName(rPos) ;

    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    Form1.FormStyle := fsStayOnTop;
    Timer1.Interval := 50;

    end;

    end.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2017
    Posts
    1
    Rep Power
    0
    Did you figure out how to do this? Can you give me some info?
    Regards
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2014
    Posts
    43
    Rep Power
    3
    You're pretty much out of luck trying to do this. To get the text of the item/subitem you either have to use LVM_GETITEM or LVM_GETITEMTEXT messages. Both of these take a pointer to a
    LVITEM structure which is not valid across processes.

IMN logo majestic logo threadwatch logo seochat tools logo