Solved

List all component from a application

Posted on 2006-11-23
6
169 Views
Last Modified: 2012-05-05
Hello guys,

I find the window now I need to get all components from it, a list, is there any way?

I did this:

var
  h : thandle;
begin
  h := findwindow('Babylon',nil);
  If h = 0 Then Abort;

end


0
Comment
Question by:hidrau
6 Comments
 
LVL 28

Expert Comment

by:TName
Comment Utility
Hi, you could try something like this:  (get the parent window, find and list the children - or click on a button)

function EnumChildProc( ph: PCardinal; lParam:Integer):Boolean;
var
h:HWND;
len:Integer;
p:PChar;
begin
 h:=ph^;
 len:=Succ(GetWindowTextLength(h));
 p:=AllocMem(len);
 GetWindowText(h,p,len);
 if len>1 then begin
    Form1.Memo1.Lines.Add(p);  // add child window captions/texts etc to memo
    //if p='Button1' then      //or use these 2 lines to press a button with the caption 'Button1'
      //SendMessage(h, BM_CLICK, 0, 0);
 end;
 result:=true;
end;

procedure TForm1.Button1Click(Sender: TObject);
var hnd: THandle;
begin
  hnd := FindWindow(nil, 'TestApp'); // or 'Babylon' ...
  //if hnd<>0 then
  if IsWindow(hnd) then
        EnumChildWindows(hnd,@EnumChildProc,0);
end;
0
 
LVL 11

Expert Comment

by:calinutz
Comment Utility
To enumerate all the components on a form:

var
  i:integer;
begin
for i := 0 to componentcount-1 do
log.Memo1.Lines.Add(components[i].Name);
end;
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
to list all component from my form I know.

But this didn't work with babylon :((

Could you make a test with babylon Tname?

Thnsk
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Accepted Solution

by:
CodedK earned 500 total points
Comment Utility
Hi.

Add a TreeView and a Button in your Form.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    TreeView1: TTreeView;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  List : TStringList;
implementation

{$R *.dfm}

type
  EnumClass = class
  public
    Treeview: TTreeview;
    ParentNode: TTreeNode;
  end;
//--------------------------------------------------

function MainEnumProc(Hwnd, lParam: Longint): Boolean; stdcall;
var
  EC: EnumClass;
  ECChild: EnumClass;
  NewNode: TTreeNode;
  Buffer: PChar;
  RtnLen: Longint;
  NodeCaption, Caption, ClassName: string;
begin
  {Get the EnumClass Passed in}

  EC := ENumClass(lParam);
  {Allocate a buffer length}
  {Allocate a string buffer (PChar)}
  Buffer := StrAlloc(501);
  try
    {First get the caption/Text of the window were currently looking at}
    RtnLen := GetWindowText(Hwnd, Buffer, 500);
    Caption := Copy(Buffer, 1, RtnLen);
    {Now get the class name of the window were currently looking at}
    RtnLen := GetClassName(Hwnd, Buffer, 500);
    ClassName := Copy(Buffer, 1, RtnLen);
    {Set the caption of the new node}
    NodeCaption := Format('HWND: %d | ClassName: "%s" | Caption: "%s"', [HWND, ClassName, Caption]);
    {Creat the new node}

    if (List.IndexOfObject(TObject(HWnd)) < 0) then begin // Test for duplicate handles
    List.AddObject('found', TObject(HWnd));
    NewNode := EC.Treeview.Items.AddChild(EC.ParentNode, NodeCaption);
    {The following is for looking at children of this window
    this basically makes the function recursive}
    ECChild := EnumClass.Create;
    try
      ECChild.Treeview := EC.Treeview;
      ECChild.ParentNode := NewNode;
      {Call this function again :) }
      EnumChildWindows(Hwnd, @MainEnumProc, Longint(ECChild));
    finally
      ECChild.Free;
    end;
    end;
  finally
    {Free the buffer we created at the beginning of
    the procedure}
    StrDispose(Buffer);
  end;
  {Return True this will tell windows to
  carry on passing us the information}
  Result := True;
end;

//--------------------------------------------------


procedure TForm1.Button1Click(Sender: TObject);
var
  EC: EnumClass;
  TN: TTreeNode;
  wnd: HWND;

begin
  wnd := findwindow(nil,'Calculator');
  If wnd = 0 Then Abort;
  List := TStringlist.Create;

  {Stop the flockering}
  LockWindowUpdate(TreeView1.Handle);
  try
    {Clear all the items out of the treeview}
    TreeView1.Items.Clear;
    {Create a node which will be the parent}
    TN := TreeView1.Items.Add(nil, 'Window List');
    {Create our own EnumClass.  This will be used as the
    lParam of the EnumWindows Function and will contain
    a TreeView and Parent Node}
    EC := EnumClass.Create;
    try
      EC.Treeview := TreeView1;
      EC.ParentNode := TN;
      {Call the API with the ENumclass}
      if EnumChildWindows(wnd, @MainEnumProc, Longint(EC)) then
        TN.Expand(False);
    finally
      {Ensure we free the object when were done with it}
      EC.Free;
    end;
  finally
    LockWindowUpdate(0);
    List.Free;
  end;
end;

end.
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

Hope this helps :)
0
 
LVL 16

Expert Comment

by:CodedK
Comment Utility
Thanks.
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
you're welcome
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now