DelphiFAQ Home Search:

Scroll a TWebBrowser document to the top/ bottom/ right

 

comments3 comments. Current rating: 5 stars (2 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;

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'

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

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:
photo Add a picture:

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.