DelphiFAQ Home Search:

Scroll a TWebBrowser document to the top/ bottom/ right

 

comments5 comments. Current rating: 5 stars (4 votes). Leave comments and/ or rate it.

Question:

My application uses TWebBrowser and I need to load a document and then have TWebBrowser scroll to the bottom of the document. How can I achieve this?

Answer:

Create an event on OnDocumentComplete event TForm1.WebBrowser1DocumentComplete() and put this code in there:

WB_ScrollTo(WebBrowser1, wbPosBottom);

// Scroll to the Bottom, Top, Right of the Document:
type
  TWBPosition = (wbPosBottom, wbPosTop, wbPosRight);

function WB_ScrollTo(WB: TWebBrowser; Position: TWBPosition): Boolean;
var
  ParentW: OLEVariant;
begin
  Result := WB.Document <> nil;
  if Result then
  begin
    ParentW := WB.OleObject.Document.ParentWindow;
    case Position of
      wbPosBottom: ParentW.ScrollTo(0, ParentW.Screen.Height);
      wbPosTop: ParentW.ScrollTo(0, 0);
      wbPosRight: ParentW.ScrollTo(ParentW.Screen.Width, 0);
    end;
  end;
end;


procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  WB_ScrollTo(WebBrowser1, wbPosBottom);
end;

Content-type: text/html

Comments:

2006-03-26, 03:55:38
anonymous from Poland  
rating
2006-04-08, 13:39:43
anonymous from United States  
rating
You, sir, are a godsend.
2007-05-27, 04:54:23
anonymous from United Kingdom  
The scroll to bottom doesn't work perfectly - I have a case here when the page is very long, and the WB_ScrollTo(WebBrowser1, wbPosBottom); doesn't scroll all the way down, just down by the height of the screen. i.e. by 'ParentW.Screen.Height'
2009-08-28, 03:18:52
from Hungary  
rating
That's true, Bottom has some problems.
Not a beauty solution, but if you use a big value (e.g. 10000) instead of ParentW.Screen.Height. It works:).
2015-06-21, 05:03:06
anonymous from United Kingdom  
rating
Thank you for this! It works a treat!

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: