AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi tWebbrowser scrollposition ermitteln
Thema durchsuchen
Ansicht
Themen-Optionen

tWebbrowser scrollposition ermitteln

Ein Thema von FLINKER_FINGER · begonnen am 12. Mär 2008 · letzter Beitrag vom 15. Mär 2008
Antwort Antwort
FLINKER_FINGER

Registriert seit: 29. Apr 2004
Ort: Berlin
131 Beiträge
 
#1

tWebbrowser scrollposition ermitteln

  Alt 12. Mär 2008, 18:54
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.???
  Mit Zitat antworten Zitat
Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#2

Re: tWebbrowser scrollposition ermitteln

  Alt 13. Mär 2008, 09:25
Hallo,

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

IHTMLDocument2(IDoc).Body.getAttribute('ScrollLeft', 0) Respektive:
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;
Thomas
  Mit Zitat antworten Zitat
Benutzerbild von toms
toms
(CodeLib-Manager)

Registriert seit: 10. Jun 2002
4.648 Beiträge
 
Delphi XE Professional
 
#3

Re: tWebbrowser scrollposition ermitteln

  Alt 15. Mär 2008, 14:23
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;
Thomas
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 05:00 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz