Author: Martin Gamulin
How to list all app's ctrls & menus in a TTreeView?
Answer:
1 uses
2 ComCtrls, Menus, Classes, Forms, Controls, Windows, Messages;
3
4 function GetControlCaption(Control: TWinControl): ShortString;
5 // Slightly modified version of Twister's Tip
6 //
7 // function GetCaptionAtPoint(pt: TPoint): string;
8 //
9 var
10 TextLength: Integer;
11 Text: PChar;
12 begin
13 if not Boolean(Control.Handle) then
14 Exit;
15
16 Result := Control.Name; // if Control doesn't have Caption
17 // Control.Name is returned
18
19 TextLength := SendMessage(Control.Handle, WM_GETTEXTLENGTH, 0, 0);
20 if TextLength 0 then
21 begin
22 GetMem(Text, TextLength + 1);
23 SendMessage(Control.Handle, WM_GETTEXT, TextLength + 1, Integer(Text));
24 Result := Text;
25 FreeMem(Text);
26 end;
27 end;
28
29 // function GetCaptionAtPoint(pt: TPoint): ShortString;
30 // begin
31 // Result:= GetControlCaption(FindVCLWindow(pt));
32 // end;
33
34 procedure FindAllMenuItems(AppTree: TTreeView; MenuItem: TMenuItem; Parent:
35 TTreeNode);
36 var
37 loop: Integer;
38 Node: TTreeNode;
39 mItem: TMenuItem;
40 Name: ShortString;
41 begin
42 for loop := 0 to MenuItem.Count - 1 do
43 begin
44 mItem := MenuItem.Items[loop];
45 Name := mItem.Caption;
46 Node := AppTree.Items.AddChildObject(Parent, Name, mItem);
47 if mItem.Count 0 then
48 findAllMenuItems(AppTree, mItem, Node);
49 end;
50 end;
51
52 procedure FindAllControls(AppTree: TTreeView; Comp: TComponent; Parent: TTreeNode);
53 var
54 Child: TComponent;
55 loop, start, Index: Integer;
56 Name: ShortString;
57 Node, Mnode: TTreeNode;
58 begin
59 start := 0;
60 if Comp is TApplication then
61 begin
62 // Parent:= AppTree.Items.AddObjectFirst(Parent, 'Application', nil);
63 // if you want to see the root ('Application') uncomment
64 start := 1;
65 end;
66
67 for loop := start to Comp.ComponentCount - 1 do
68 begin
69 Child := Comp.Components[loop];
70 Name := Child.Name;
71
72 if Child is TControl then
73 begin
74 if Child is TWinControl then
75 begin // does Child have Caption property??
76 Name := GetControlCaption(TWinControl(Child));
77 end;
78 Node := AppTree.Items.AddChildObject(Parent, Name, Child);
79 if Child.ComponentCount 0 then
80 FindAllControls(AppTree, Child, Node);
81 end;
82
83 if Child is TMenu then
84 begin
85 Node := AppTree.Items.AddChildObject(Parent, Name, Child);
86 for Index := 0 to TMenu(Child).Items.Count - 1 do
87 begin
88 Mnode := AppTree.Items.AddChildObject(Node,
89 TMenu(Child).Items[Index].Caption,
90 TMenu(Child).Items[Index]);
91 FindAllMenuItems(AppTree, TMenu(Child).Items[Index], Mnode);
92 end;
93 end;
94 end;
95 end;
|