如果我知道另一个应用程序中一个TWebBrowser实例的句柄,该如何访问它的IHTMLDocument2接口,即Document属性?
   
  FindControl只能得到nil.

解决方案 »

  1.   

    {************************************************************
      函数名:GetIEFromHWND
      参数:hWnd,WebBrowser控件的窗口句柄
      功能:通过WM_HTML_GETOBJECT取得控件的IWebbrowser2接口
    ************************************************************}
    function GetIEFromHWND(hWnd: HWND; var IE: IWebbrowser2): HRESULT;
    var
    hInst: THandle;
    lRes: Cardinal;
    MSG: Integer;
    pDoc: IHTMLDocument2;
    ObjectFromLresult: TObjectFromLresult;
    begin
        result:=S_OK;
        if (hWnd<=0) then
            exit;
    hInst := LoadLibrary('Oleacc.dll');
        if (hInst>0) then
        try
            @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
            MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
            SendMessageTimeOut(hWnd, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
            Result := ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, pDoc);
            if Result = S_OK then
            begin
                (pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2,    IE);
            end;
        finally
            FreeLibrary(hInst);
        end;
    end;
      

  2.   

    兄弟,TObjectFromLresult, IServiceprovider这两个接口从哪里来的呀?
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, DB, DBClient, StdCtrls, OleCtrls, SHDocVw, MSHTML;<==============type
      TForm1 = class(TForm)
        ClientDataSet1: TClientDataSet;
        WebBrowser1: TWebBrowser;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var
      Doc: IHTMLDocument2;
    begin
      Doc := WebBrowser1.Document as IHTMLDocument2
    end;end.
      

  4.   

    完整的源代码:
    http://www.delphihackerspages.nl/IEGetCurrentPage.htm
      

  5.   

    楼上朋友:
      http://www.delphihackerspages.nl/IEGetCurrentPage.htm上
    的代码跑起来出错呀???
      

  6.   

    如果是SHDocVW_TLB, MSHTML_TLB引起的错误
    直接把这两个单元去掉就行了(对D6而言)