获取一个窗口的所有子窗口(包括嵌套) - 回复 "asian" 的问题

问题来源: http://www.cnblogs.com/del/archive/2008/12/10/1345752.html#1397451


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.


 
   

窗体文件:

object Form1: TForm1

  Left = 0

  Top = 0

  Caption = 'Form1'

  ClientHeight = 206

  ClientWidth = 318

  Color = clBtnFace

  Font.Charset = DEFAULT_CHARSET

  Font.Color = clWindowText

  Font.Height = -11

  Font.Name = 'Tahoma'

  Font.Style = []

  OldCreateOrder = False

  PixelsPerInch = 96

  TextHeight = 13

  object Panel1: TPanel

    Left = 8

    Top = 8

    Width = 177

    Height = 190

    Caption = 'Panel1'

    TabOrder = 0

    object Panel2: TPanel

      Left = 8

      Top = 112

      Width = 161

      Height = 75

      Caption = 'Panel2'

      TabOrder = 0

      object Edit5: TEdit

        Left = 24

        Top = 8

        Width = 121

        Height = 21

        TabOrder = 0

        Text = 'Edit5'

      end

    end

    object Edit3: TEdit

      Left = 32

      Top = 16

      Width = 121

      Height = 21

      TabOrder = 1

      Text = 'Edit3'

    end

    object Edit4: TEdit

      Left = 32

      Top = 43

      Width = 121

      Height = 21

      TabOrder = 2

      Text = 'Edit4'

    end

  end

  object Edit1: TEdit

    Left = 191

    Top = 24

    Width = 121

    Height = 21

    TabOrder = 1

    Text = 'Edit1'

  end

  object Edit2: TEdit

    Left = 191

    Top = 51

    Width = 121

    Height = 21

    TabOrder = 2

    Text = 'Edit2'

  end

  object Button1: TButton

    Left = 216

    Top = 152

    Width = 75

    Height = 25

    Caption = 'Button1'

    TabOrder = 3

    OnClick = Button1Click

  end

end


 
   

你可能感兴趣的:(问题)