Solved

Handles of Controls in a Form

Posted on 1998-11-04
6
162 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
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

18 Experts available now in Live!

Get 1:1 Help Now