Solved

Handles of Controls in a Form

Posted on 1998-11-04
6
166 Views
Last Modified: 2010-04-06
I have a running application that is written by someone.
I can get it's handle by WinApi FindWindow(...).Let suppose
that there are some controls on it like Edit, ComboBox,
ListBox, etc. controls.
How can I get the handles of these controls ?
It is possible : WinSight32 do it.
I'll appriciate any example.
0
Comment
Question by:jpenev1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1345555
hi jpenev1,

i don't know does it works,

componentHandle := getwindow(YourFormHandle,GW_CHILD)

should find first componenthandle

componentHandle := getwindow(componentHandle,GW_HWNDNEXT);

should find next componenthandle

You must yourself experiment with this.
By this method i have changed a label text from a exe-file (was not a delphi-program)

meikl
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1345556
I think meikl deserves the points since that's what I was going to say, and is the way to go with it... YOu can also use a few other like GetWindowClass() if you need to see what's the name of the class and also can use SetWindowText() to set the text of any control even if it's not in your app... Although I think that SetWindowsText() works only for the Caption of forms and probably some other controls, but not for all...

Cheers,
Viktor
0
 
LVL 3

Expert Comment

by:Matvey
ID: 1345557
Hi guys. I wrote these two, and they work quite well. They are on the same principle as the first sugestions:



function FindByIndex(ControlClass: String; ControlIndex: Integer; ParentW: THandle): THandle;
var ch1: THandle;
    CurIndex: Integer;
begin
  Result := 0;
  CurIndex := 0;
  ch1 := GetWindow(ParentW, GW_Child);
  while not(ch1=0) do begin
    if LowerCase(ClassName(ch1)) = LowerCase(ControlClass) then begin
      inc(CurIndex);
      if CurIndex=ControlIndex then begin
        Result := ch1;
        exit;
      end;
    end;
    ch1 := GetNextWindow(ch1, GW_HWNDNEXT);
  end;
  ch1 := GetWindow(ParentW, GW_Child);
  while not(ch1=0) do begin
    if GetWindow(ch1, GW_Child)<>0 then Result := FindByIndex(ControlClass, ControlIndex - CurIndex, ch1);
    if Result<>0 then exit
    else ch1 := GetNextWindow(ch1, GW_HWNDNEXT);
  end;
end;

function FindByCaption(ControlClass: String; Caption: String; ParentW: THandle): THandle;
var ch1: THandle;
begin
  Result := 0;
  ch1 := GetWindow(ParentW, GW_Child);
  while not(ch1=0) do begin
    if(LowerCase(ClassName(ch1))=LowerCase(ControlClass))and(LowerCase(ControlCaption(ch1))=LowerCase(Caption)) then begin
        Result := ch1;
        exit;
    end;
    ch1 := GetNextWindow(ch1, GW_HWNDNEXT);
  end;
  ch1 := GetWindow(ParentW, GW_Child);
  while not(ch1=0) do begin
    if GetWindow(ch1, GW_Child)<>0 then Result := FindByCaption(ControlClass, Caption, ch1);
    if Result<>0 then exit
    else ch1 := GetNextWindow(ch1, GW_HWNDNEXT);
  end;
end;



The parameter names speak for themselvs: the window class, it's index or caption, and the parent window to search in.

Good luck!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:Matvey
ID: 1345558
An example:

FindByCaption('button', '&OK', hWindow)
FindByIndex('edit', 3, hWindow)

-which find the OK button and the 3-d edit in the Z order.
NOTE: the functions search recursively, so you don't have to worry if the window is inside another window which is inside another wind...
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1345559
hi matvey,

your comment seems be good enough to be an answer.

meikl
0
 
LVL 1

Accepted Solution

by:
AttarSoftware earned 30 total points
ID: 1345560
Sorry for butting in but I think that you can do it with the "EnumChildWindows" API call.
Sorry no example as I am in a rush :)
John
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 156
select query - oracle 16 103
JAudiorecorder record freezing the app 29 91
FMX TEdit KeyUp handler detecting  "enter" key 4 62
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 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