已经得到IE窗口的句柄,如何枚举出里面的所有控件,比如Edit,Button等,以达到控制自动填充表单的目的。用EnumChildWindows只能得到IE框架的控件,比如地址栏等,但是表单里面的控件用这个得不到

解决方案 »

  1.   

    How to get IHTMLDocument2 from a HWND 
    About MSHTML
      

  2.   

    谢谢,有没有For Delphi的代码阿
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,MSHTML,comobj,activex, StdCtrls;type
      TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
      procedure GetIEFromHWnd(Wnd: HWND; var pDoc: IHTMLDocument2);
      procedure GetAllElement;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}
    procedure TForm1.GetIEFromHWnd(Wnd: HWND; var pDoc: IHTMLDocument2);
    type
      TObjectFromLResult = function(LRESULT: LRESULT; const IID: TGUID; wParam: WPARAM; out PObject): HRESULT; stdcall;
    var
      GetDocPtr: TObjectFromLResult;
      hModule: THandle;
      Msg: Cardinal;
      lRes: Cardinal;
    begin
      hModule := LoadLibrary('OLEACC.DLL');
      try
        if hModule <> 0 then
        begin
          GetDocPtr := GetProcAddress(hModule, 'ObjectFromLresult');
          if @GetDocPtr <> nil then
          begin
            Msg := RegisterWindowMessage('WM_HTML_GETOBJECT');
            SendMessageTimeOut(Wnd, Msg, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
            if GetDocPtr(lRes, IID_IHTMLDocument2, 0, pDoc) <> S_OK then
            begin
              pDoc := nil;
            end;
          end;
        end;
      finally
        FreeLibrary(hModule);
      end;
    end;procedure TForm1.GetAllElement;
    var
      IE: IHTMLDocument2;
      Wnd: HWND;
      WndChild: HWND;
      i:integer;
      chele:IHTMLElement;
    begin
      Wnd := FindWindow('IEFrame', nil);
      if Wnd = 0 then
      begin
          MessageDlg ('No Running instance of Internet Explorer!',mtError, [mbOK], 0);
          exit;
      end;  WndChild := FindWindowEX(Wnd, 0, 'TabWindowClass', nil);  //  如果不是IE7,这条语句可以不要
      if WndChild = 0 then  WndChild:=Wnd;
      WndChild := FindWindowEX(WndChild, 0, 'Shell DocObject View', nil);
      if WndChild <> 0 then
      begin
        WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);
        if WndChild <> 0 then
        begin
          GetIEFromHWnd(WndChild, IE);
          if (assigned(IE)) then
          begin
            for i:=0 to iE.all.length-1 do
            begin
              chele:=iE.all.item(i,emptyparam) as IHTMLElement;
              memo1.Lines.Add(chele.tagname);
            end;
          end;
        end;
      end;
      end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      GetAllElement;
    end;end.