随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

问题来源: http://www.cnblogs.com/del/archive/2008/02/28/1085432.html#1247615

致 "混岗" 同学: 你同时提到 IE 中的 "输入框", 没有明白你的意思; 这个例子只是找顶层窗口, IE 的 "输入框" 是 IE 窗口的子窗口, 需要在此基础上继续枚举子窗口.

本例效果图(测试时, 我打开了: 搜狐、谷歌和我的博客):



代码文件:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Memo1.Align := alTop;
  Memo1.ScrollBars := ssBoth;
  Memo1.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  className = 'IEFrame'; {这是 IE 浏览器的类名}
var
  h: HWnd;
  buf: array[Byte] of Char;
begin
  h := GetWindow(Handle, GW_HWNDFIRST);
  while h <> 0 do
  begin
    GetClassName(h, buf, Length(buf));
    if buf = className then {找到咋处理? 显示它的标题吧}
    begin
      GetWindowText(h, buf, Length(buf));
      Memo1.Lines.Add(buf)
    end;
    h := GetWindow(h, GW_HWNDNEXT);
  end;
end;

end.

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 168
  ClientWidth = 319
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 126
    Top = 127
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object Memo1: TMemo
    Left = 8
    Top = 8
    Width = 185
    Height = 113
    Lines.Strings = (
      'Memo1')
    TabOrder = 1
  end
end

posted on 2008-07-07 23:16  万一  阅读(...)  评论(...编辑  收藏