Solved

Handles of Controls in a Form

Posted on 1998-11-04
6
167 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
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!

 
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

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…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…

690 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