Link to home
Start Free TrialLog in
Avatar of jpenev1
jpenev1

asked on

Handles of Controls in a Form

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.
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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
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
Avatar of Matvey
Matvey

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!
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...
hi matvey,

your comment seems be good enough to be an answer.

meikl
ASKER CERTIFIED SOLUTION
Avatar of AttarSoftware
AttarSoftware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial