如何获取有关Webbrowser控件实例或IE Webrowser的滚动条的信息?

Sal*_*dor 3 c++ delphi winapi webbrowser-control iwebbrowser2

我需要获取有关外部应用程序的Webbrowser控件的滚动条(位置,大小,可见性)的信息,我尝试使用上一个问题中GetScrollBarInfo函数,但该函数总是返回false,我使用其他应用程序检查了此函数,工作正常,但不适用于IE或Webbrowser控件.So how I can get information about the scrollbars of an Webbrowser control instance or the IE Webbrowser?

kob*_*bik 7

您可以将WM_HTML_GETOBJECT消息发送到"Internet Explorer_Server"外部应用程序的类窗口来获取IHtmlDocument2,然后使用IServiceProvider您可以获取IWebBrowser2接口.
以下是Delphi中的一些示例代码:

uses
  ActiveX, MSHTML;

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

function GetIEFromHWND(WHandle: HWND; var IE: IWebbrowser2): HRESULT;
var
  hInst: HWND;
  lRes: Cardinal;
  Msg: Integer;
  pDoc: IHTMLDocument2;
  ObjectFromLresult: TObjectFromLresult;
begin
  Result := S_FALSE;
  hInst := LoadLibrary('Oleacc.dll');
  @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
  if @ObjectFromLresult <> nil then
  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;

procedure TForm1.Button1Click(Sender: TObject);
var
  Wnd, WndChild: HWND;
  IE: IWebBrowser2;
  Document: IHtmlDocument2;
  ScrollTop, ScrollLeft: Integer;
begin
  Wnd := FindWindow('IEFrame', nil); // top level IE
  if Wnd = 0 then Exit;
  WndChild := FindWindowEX(Wnd, 0, 'Shell DocObject View', nil);
  if WndChild = 0 then Exit;
  WndChild := FindWindowEX(WndChild, 0, 'Internet Explorer_Server', nil);
  if WndChild = 0 then Exit;

  GetIEFromHWnd(WndChild, IE);
  if IE <> nil then
  begin
    ShowMessage(IE.LocationURL);
    Document := IE.Document as IHtmlDocument2;
    ScrollTop := ((Document as IHTMLDocument3).documentElement as IHTMLElement2).scrollTop;
    ScrollLeft := ((Document as IHTMLDocument3).documentElement as IHTMLElement2).scrollLeft;
    ShowMessage(Format('%d;%d', [ScrollTop, ScrollLeft]));

    // visible|hidden|scroll|auto|no-display|no-content
    ShowMessage(OleVariant(Document).documentElement.currentStyle.overflowX);
    ShowMessage(OleVariant(Document).documentElement.currentStyle.overflowY);
  end;
end;
Run Code Online (Sandbox Code Playgroud)

编辑:当页面使用<!DOCTYPE>指令将IE6切换到严格的标准兼容模式时使用document.documentElement.(IHTMLDocument3)在预标准模式下,body表示可滚动区域,因此您可以检索滚动位置 document.body.scrollTop.在标准模式下,HTML元素是可滚动的,因此您应该使用document.documentElement.scrollTop.

如果document.documentElement.clientWidth <> 0使用documentElement元素属性,则使用该body元素.
滚动信息相关的可用性能也clientHeight,scrollWidth,scrollHeight.