Finding controls on a window.

Hey,

I need a code to find controls on a window.
Problem is there are two RichEdit20A's on a window and I can only find one of them.
hwndxyz is the window's main handle.

hwndedit1 := FindWindowEx(hwndxyz, 0, 'RichEdit20A', nil);

That's the first RichEdtit20A, now to find the other one, I tried the following.

hwndedit2 := FindWindowEx(hwndxyz, hwndedit1, 'RichEdit20A', nil);

Now, both handles are not empty, so it found both textboxes. Problem is that only the first returns text but the second handle returns nothing.

SendMessage(hwndedit1, WM_SETTEXT, 0, integer(xyz)); = text.
SendMessage(hwndedit2, WM_SETTEXT, 0, integer(xyz)); = empty, while it should return text.

I guess it didn't find the second RichEdit20A after all, so do you know a way to find the second richedit20a?

Thnx!
AngeloXAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

geobulCommented:
Hi,

You may enumerate all child windows that belong to hwndxyz and get those whose class names are RichEdit20A. Something like (not tested):

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var
  WindowsList: TStrings;

procedure WindowInfo(hWnd: HWND; List: TStrings);
var
  ErrorCode: Integer;
  TheClassName: array[0..255] of char;
begin
  ErrorCode := GetClassName(hWnd, TheClassName, 255);
  if (ErrorCode <> 0) and (TheClassName = 'RichEdit20A') then begin
    // do something with the window using its handle 'hWnd'. For instance, fill a TStrings
    List.Add(IntToStr(hWnd));
  end;
end;

function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin
  Result := TRUE;
  WindowInfo(hWnd, WindowsList);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  hwndxyz, editHwnd: HWND;
  i: integer;
  xyz: string;
begin
  ListBox1.Items.Clear;
  WindowsList := ListBox1.Items;
  xyz := 'Hello';

  hwndxyz := FindWindow(nil, 'aaa');
  if IsWindow(hwndxyz) then begin
    EnumChildWindows(hwndxyz, @EnumerateChildWindows, 0);

    // here you can use all handles from the listbox
    for i := 0 to ListBox1.Items.Count - 1 do begin
      editHwnd := StrToInt(ListBox1.Items[i]);
      SendMessage(editHwnd, WM_SETTEXT, 0, integer(xyz));
    end;
  end;
end;

Regards, Geo
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
geobulCommented:
EnumerateChildWindows function above is wrong. Should be:

function EnumerateChildWindows(hWnd: HWND; lParam: LPARAM): BOOL; stdcall;
begin
  WindowInfo(hWnd, WindowsList);
  EnumChildWindows(hWnd, @EnumerateChildWindows, 0);
  Result := TRUE;
end;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.