[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 293
  • Last Modified:

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!
0
AngeloX
Asked:
AngeloX
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now