delphi獲取一個窗口的所有子窗口(包括嵌套)


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
Panel2: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{這應該使用遞歸函數}
procedure GetChildWindows(h: HWND);
var
buf: array[0..255] of Char; {這個緩沖區是獲取類名用的, 如果不需要可以刪除}
begin
h := GetWindow(h, GW_CHILD); {第一個子窗口}
while h <> 0 do
begin
{下面兩行是要執行的操作, 並假定只處理 TEdit}
GetClassName(h, buf, Length(buf));
if buf = 'TEdit' then ShowMessageFmt('%s:%d', [buf, h]);

h := GetWindow(h, GW_HWNDNEXT); {下一個子窗口}
GetChildWindows(h); {遞歸}
end;
end;

{測試}
procedure TForm1.Button1Click(Sender: TObject);
begin
GetChildWindows(Handle);
end;

end. 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM