Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi tWebbrowser scrollposition ermitteln (https://www.delphipraxis.net/110089-twebbrowser-scrollposition-ermitteln.html)

FLINKER_FINGER 12. Mär 2008 18:54


tWebbrowser scrollposition ermitteln
 
Hi,

würde gerne die momentange X-und Y-Position vom tWebBrowser ermitteln. Also eigentlich nur die Positionen der Scrollbalken vom Webbrowser.

Denke mal das geht irgendwie über :

WebBrowser1.OleObject.Document.body.???

oder:

WebBrowser1.OleObject.Document.ParentWindow.???

toms 13. Mär 2008 09:25

Re: tWebbrowser scrollposition ermitteln
 
Hallo,

Die X Position kann man grundsätzlich so ermitteln (analog die Y Position)

Delphi-Quellcode:
IHTMLDocument2(IDoc).Body.getAttribute('ScrollLeft', 0)
Respektive:
Delphi-Quellcode:
Variant(IDoc).DocumentElement.scrollLeft
Dies funktioniert aber nicht, falls zu einem lokalen Folder navigiert wird.
Dann muss die ListView des Webbrowsers und dann mit GetScrollInfo die Scrollbar Positionen ermittelt werden.

Habe hierfür mal eine Funktion mit 2 Subfunktionen geschrieben.
Ist erstmal ein Entwurf und kann sicher noch schöner geschrieben werden.


Delphi-Quellcode:
uses
  MSHTML;

// Webbrowser Scrollbar X,Y Position ermitteln
function WB_GetScrollPosition(WB: TWebbrowser; var P: TPoint): Boolean;

  // Scrollbar X,Y Position der ListView ermitteln
  function WB_GetLVScrollPosition(WB: TWebbrowser; var P: TPoint): Boolean;
  var
    lpsi: TScrollInfo;
    WND, WndLV: HWND;
  begin
    Result := False;
    FillChar(lpsi, SizeOf(lpsi), 0);
    with lpsi do
    begin
      cbSize := SizeOf(lpsi);
      fMask := SIF_ALL;
    end;
    WndLV := 0;
    Wnd := GetNextWindow(WB.Handle, GW_CHILD);
    while (WndLV = 0) and (WND <> 0) do
    begin
      WndLV := FindWindowEx(Wnd, 0, 'SysListView32', nil);
      Wnd := GetNextWindow(Wnd, GW_CHILD)
    end;
    if WndLV <> 0 then
    begin
      if GetScrollInfo(wnd, SB_VERT, lpsi) then
      begin
        P.Y := lpsi.nPos;
        if GetScrollInfo(WND, SB_HORZ, lpsi) then
        begin
          P.X := lpsi.nPos;
          Result := True;
        end;
      end;
    end;
  end;

  // Scrollbar X,Y Position des HTML Documents ermitteln
  function WB_GetDOCScrollPosition(WB: TWebbrowser; var P: TPoint): Boolean;
  var
    IDoc: IHTMLDocument2;
    IDoc3: IHTMLDocument3;
    IElement: IHTMLElement;
  begin
    P := Point(-1, -1);
    Result := False;
    if Assigned(WB.Document) and
      (Succeeded(WB.Document.QueryInterface(IHTMLDocument2, IDoc))) then
    begin
      IDoc := WB.Document as IHTMLDocument2;
      if Assigned(IDoc) and Assigned((IHTMLDocument2(IDoc).Body)) then
      begin
        if (IDoc.QueryInterface(IHTMLDocument3, IDoc3) = S_OK) then
          if Assigned(IDoc3) then
            IElement := IDoc3.get_documentElement;
        if (Assigned(IElement)) and (Variant(IDoc).DocumentElement.scrollTop = 0) then
          P.Y := IHTMLDocument2(IDoc).Body.getAttribute('ScrollTop', 0)
        else
          P.Y := Variant(IDoc).DocumentElement.scrollTop;
        if Assigned(IElement) and (Variant(IDoc).DocumentElement.scrollLeft = 0) then
          P.X := IHTMLDocument2(IDoc).Body.getAttribute('ScrollLeft', 0)
        else
          P.X := Variant(IDoc).DocumentElement.scrollLeft
      end;
      Result := (P.X <> -1) and (P.Y <> -1)
    end;
  end;

begin
  Result := False;
  if not WB_GetDOCScrollPosition(WB, P) then
    Result := WB_GetLVScrollPosition(WB, P);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // WebBrowser1.Navigate('file://c:\'); // zu einer lokalen Datei navigieren
  WebBrowser1.Navigate('www.google.com');
end;

// test:
procedure TForm1.Button2Click(Sender: TObject);
var
  P: TPoint;
begin
  if WB_GetScrollPosition(Webbrowser1, P) then
    ShowMessage(Format('X: %d, Y: %d', [P.X, P.Y]));
end;

toms 15. Mär 2008 14:23

Re: tWebbrowser scrollposition ermitteln
 
Die Funktion hatte noch einen kleinen Fehler. Update:

Delphi-Quellcode:
// Webbrowser Scrollbar X,Y Position ermitteln
function WB_GetScrollPosition(WB: TWebBrowser; var ScrollPos: TPoint): Boolean;

  // Scrollbar X,Y Position der ListView ermitteln
  function WB_GetLVScrollPosition(WB: TWebBrowser; var ScrollPos: TPoint): Boolean;
  var
    lpsi: TScrollInfo;
    WND, WndLV: HWND;
  begin
    Result := False;
    // SysListView32 Child vom TWebBrowser suchen
    WndLV := 0;
    Wnd := GetNextWindow(WB.Handle, GW_CHILD);
    while (WndLV = 0) and (WND <> 0) do
    begin
      WndLV := FindWindowEx(Wnd, 0, 'SysListView32', nil);
      Wnd := GetNextWindow(Wnd, GW_CHILD)
    end;
    if WndLV <> 0 then // SysListView32 gefunden
    begin
    // TScrollInfo initialisieren
      FillChar(lpsi, SizeOf(lpsi), 0);
      with lpsi do
      begin
        cbSize := SizeOf(lpsi);
        fMask := SIF_POS;
      end;
      // ScrollInfos der vertikalen ScrollBar ermitteln
      if GetScrollInfo(WndLV, SB_VERT, lpsi) then
      begin
        ScrollPos.Y := lpsi.nPos;
        // ScrollInfos der horizontalen ScrollBar ermitteln
        if GetScrollInfo(WndLV, SB_HORZ, lpsi) then
        begin
          ScrollPos.X := lpsi.nPos;
          Result := True;
        end;
      end;
    end;
  end;

  // Scrollbar X,Y Position des HTML Documents ermitteln
  function WB_GetDOCScrollPosition(WB: TWebBrowser; var ScrollPos: TPoint): Boolean;
  var
    IDoc: IHTMLDocument2;
    IDoc3: IHTMLDocument3;
    IElement: IHTMLElement;
  begin
    ScrollPos := Point(-1, -1);
    Result := False;
    if Assigned(WB.Document) and (Succeeded(WB.Document.QueryInterface(IHTMLDocument2, IDoc))) then
    begin
      IDoc := WB.Document as IHTMLDocument2;
      if Assigned(IDoc) and Assigned((IHTMLDocument2(IDoc).Body)) then
      begin
        if (IDoc.QueryInterface(IHTMLDocument3, IDoc3) = S_OK) then
          if Assigned(IDoc3) then
            IElement := IDoc3.get_documentElement;
        if (Assigned(IElement)) and (Variant(IDoc).DocumentElement.scrollTop = 0) then
          ScrollPos.Y := IHTMLDocument2(IDoc).Body.getAttribute('ScrollTop', 0)
        else
          ScrollPos.Y := Variant(IDoc).DocumentElement.scrollTop;
        if Assigned(IElement) and (Variant(IDoc).DocumentElement.scrollLeft = 0) then
          ScrollPos.X := IHTMLDocument2(IDoc).Body.getAttribute('ScrollLeft', 0)
        else
          ScrollPos.X := Variant(IDoc).DocumentElement.scrollLeft
      end;
      Result := (ScrollPos.X <> -1) and (ScrollPos.Y <> -1)
    end;
  end;

begin
  Result := WB_GetDOCScrollPosition(WB, ScrollPos);
  if not Result then
    Result := WB_GetLVScrollPosition(WB, ScrollPos);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:06 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz