tags:

views:

875

answers:

1

Hello,

Is there a way to get/set the location of the scrollbar in Internet Explorer/Firefox? I am not looking to do that from inside the HTML/ASP/Javascript code, but from an application outside the browser (using WinAPI for example), and without using BHO.

From the search I've done right now it seems impossible, so I'm dropping a question here as a last try.

Thanks,

Oren.

A: 

For Internet Explorer, you can use COM automation to enumerate all active Internet Explorer windows/tabs and then access the DOM tree of the document displayed in the window/tab to access and read the scroll position.

The following sample code uses Delphi as a programming language. The mechanism would be similar in C++, VB or C#

var
   ShWindows:  ShellWindows;
   InetExplorer: InternetExplorer;
   Count: Integer;
   I: Integer;
   HTMLDocument: IHTMLDocument2;
   Elem: IHTMLElement2;
   ScrollPosY: Integer;
begin
   // Create ShellWindows Object
   SHWindows:= CoShellWindows.Create;

   // Number of explorer windows/tabs (win explorer and ie)
   Count:= ShWindows.Count;
   ShowMessage(Format('There are %d explorer windows open.', [Count]));

   // For all windows/tabs
   for I:= 0 to (Count - 1) do
   begin
     // Get as InetExplorer interface
     InetExplorer:= SHWindows.item(I) as InternetExplorer;

     // Check to see if this explorer window contains a web document
     if Supports(InetExplorer.Document, IHTMLDocument2, HTMLDocument) then
     begin
       // Get body Element
       Elem:= HTMLDocument.body as IHTMLElement2;
       // Read vertical scroll position
       ScrollPosY:= Elem.scrollTop;

       // If this is 0 so far, maybe there is a scroll position in root element
       if ScrollPosY = 0 then
       begin
         Elem:= HTMLDocument.body.parentElement as IHTMLElement2;
         ScrollPosY:= Elem.scrollTop;
       end;

       // Display
       ShowMessage(IntToStr(Elem.scrollTop));
     end;
   end;
end;

For documentation, start here: http://msdn.microsoft.com/en-us/library/bb773974(VS.85).aspx

NineBerry