?
Solved

Handles of Controls in a Form

Posted on 1998-11-04
6
Medium Priority
?
169 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 60 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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 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