获得ie句柄,并根据句枘操作iwebbrowser2对象

var

Wnd: HWND;
WndChild:HWND;

Hdoct:IHtmlDocument2;
IE: iwebbrowser2;
All: IHtmlElementCollection;
HtmlElement: IHtmlElement;
objovt:OleVariant;
input:   IHTMLInputElement;
obj:IHTMLObjectElement;

begin

    Wnd := FindWindow('IEFrame', nil);
    WndChild := FindWindowEx(Wnd, 0,'Frame Tab', nil);
    WndChild :=  FindWindowEx(WndChild, 0,'TabWindowClass', nil );
    WndChild := FindWindowEX(WndChild, 0, 'Shell DocObject View', nil);
    WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);//找到'Internet Explorer_Server'类的句柄

  if  WndChild <> 0  then
 begin

  GetIEFromHWnd(WndChild, IE);


   if(IE <>nil)   then
   begin

        Hdoct:=IE.Document as IHtmlDocument2;
        All:=    Hdoct.all;
        obj:= All.item('cardPrint',0) as IHTMLObjectElement;
        input:= All.item(0,0) as IHTMLInputElement;

        //input.value:='赋值';
        input.select();

    end;

 end;

end;

 

function TActiveFormX.GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
  hInst: HWND;
  lRes: Cardinal;
  MSG: Integer;
  pDoc: IHTMLDocument2;
  ObjectFromLresult: TObjectFromLresult;
begin
  hInst := LoadLibrary('Oleacc.dll');
  @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
  if @ObjectFromLresult <> nil then
  begin
    try
      MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
      SendMessageTimeOut(WHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
      Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
      if Result = S_OK then
        (pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
    finally
      FreeLibrary(hInst);
    end;
  end;
end;

 

//在type加TObjectFromLResult 定义

type
  TObjectFromLResult = function(LRESULT: lResult; const IID: TIID; WPARAM: wParam; out pObject): HRESULT; stdcall;

你可能感兴趣的:(获得ie句柄,并根据句枘操作iwebbrowser2对象)