Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Для новичков > scroll to top and bottom


Автор: muratboy31 10.2.2010, 10:08
i want to autoscroll the web page from top to bottom and then bottom to top, i can do scrolling with timer but i dont know how to do it to scroll top when it is at the bottom ??


Код

procedure TForm1.Timer1Timer(Sender: TObject);
begin
WebBrowser1.OleObject.Document.ParentWindow.ScrollBy(0, 10);
end;

Автор: Mikel 10.2.2010, 11:20
Код

WebBrowser1.OleObject.Document.ParentWindow.ScrollBy(0, -10);
 doesn't work?

Автор: muratboy31 10.2.2010, 12:08
yes yes it works but i want to do it automaticly... when the scroller is at the bottom then it should scroll to top and when it is at the top then scroll to bottom automaticly...
i hope you understand what i mean...

Автор: Mikel 11.2.2010, 15:16
Код

procedure TForm1.btn1Click(Sender: TObject);
begin
  tmr1.Enabled:=true;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  tmr1.Enabled:=false;
  tmr1.Tag:=0;
  wb1.Navigate('http://rol.ru');//some URL
end;

procedure TForm1.tmr1Timer(Sender: TObject);
begin
  if tmr1.tag=0
     then begin
            wb1.OleObject.Document.ParentWindow.ScrollBy(0, 10);
            if WB1.OleObject.Document.Body.ScrollTop+WB1.OleObject.Document.Body.ClientHeight>=wb1.OleObject.Document.Body.ScrollHeight
               then tmr1.tag:=1;
          end
     else begin
            wb1.OleObject.Document.ParentWindow.ScrollBy(0, -10);
            if WB1.OleObject.Document.Body.ScrollTop<=0
               then tmr1.tag:=0;
          end;
end;

Автор: muratboy31 11.2.2010, 15:37
thanks alot Mikel smile 

i used it with spin edit it works perfect apart from 1 thing...

how can i stop user type    .  or   ,  in spin edit becuase it gives error if there is . or , in spin edit...
 
Код

     if Timer4.tag=0
     then begin
            GetActiveEWB.OleObject.Document.ParentWindow.ScrollBy(0, SpinEdit1.Value);
            if GetActiveEWB.OleObject.Document.Body.ScrollTop+GetActiveEWB.OleObject.Document.Body.ClientHeight>=GetActiveEWB.OleObject.Document.Body.ScrollHeight
               then Timer4.tag:=1;
            end
     else begin
            GetActiveEWB.OleObject.Document.ParentWindow.ScrollBy(0, -SpinEdit1.Value);
            if GetActiveEWB.OleObject.Document.Body.ScrollTop<=0
               then Timer4.tag:=0;
      end;

Автор: Mikel 11.2.2010, 15:47
You should convert SpinEdit1.Text to number with val procedure and not use SpinEdit1.Value. Another variant- use try...except...end construction to catch exceptions.

Автор: muratboy31 11.2.2010, 15:51
ok i will do that Mikel...

do you think it will work with pages that has frames ?

because i tried one and it didnt scroll it...

you can also tyr it... here is the web site;

http://www.canliskor.com/

Автор: Mikel 11.2.2010, 16:13
May be, because ScrollTo doesn't influe on WB1.OleObject.Document.Body.ScrollTop, which is always equal to 0. Look for info about TWebbrowser and Document interface smile may be this will be useful: http://www.bsalsa.com/ewb_propert.html

Автор: muratboy31 11.2.2010, 16:16
i cant open the page !!!

http://www.bsalsa.com/ewb_propert.html 

Автор: muratboy31 11.2.2010, 16:41
i tried like that but i got error says;
user posted image

Код

procedure TForm1.Timer4Timer(Sender: TObject);
var
    iall : IHTMLElement;
begin
 if Assigned(GetActiveEWB.Document) then
   begin
     iall := (GetActiveEWB.Document AS IHTMLDocument2).body;

     while iall.parentElement <> nil do
     begin
       iall := iall.parentElement;
     end;

  if Timer4.tag=0
     then begin
            GetActiveEWB.OleObject.Document.ParentWindow.ScrollBy(0, SpinEdit1.Value);
            if GetActiveEWB.OleObject.Document.Body.ScrollTop+GetActiveEWB.OleObject.Document.Body.ClientHeight>=iall.outerHTML
               then Timer4.tag:=1;
          end
     else begin
            GetActiveEWB.OleObject.Document.ParentWindow.ScrollBy(0, -SpinEdit1.Value);
            if GetActiveEWB.OleObject.Document.Body.ScrollTop<=0
               then Timer4.tag:=0;
          end;

 end;
end;

Автор: Mikel 11.2.2010, 17:38
It's seems that this will help you http://www.rudins.ru/technoshop/lyceum/polosy_prokrutki/

Автор: muratboy31 11.2.2010, 18:15
thanks for reply Mikel  smile 

but do i have create the scroll bar !!! it is all russian and if i tranlate it to english then it becomes something strange and really difficult to understand...
can i not just use a code from the page taht will take me there ! because i couldnt find this;

Код

BrowserVScrollBar.Position := BrowserVScrollBar.Position + Delta;



Автор: muratboy31 11.2.2010, 19:46
ok i found how to scroll down and up, but i need to combine it so when it is at the bottom scroll top and if it is on top then scroll bottom...


Автор: volvo877 11.2.2010, 23:59
Цитата(muratboy31 @  11.2.2010,  18:46 Найти цитируемый пост)
but i need to combine it so when it is at the bottom scroll top and if it is on top then scroll bottom...
Try this:

Код
function TForm1.TBrowserScrollV(Delta: SmallInt): boolean;
var
  HtmlDoc: IHTMLDocument2;
  CurElement: IHTMLElement;
  Pos: TPoint;
begin
  try
    GetCursorPos(Pos);
    Pos := WebBrowser1.ScreenToClient(Pos);
    HtmlDoc := WebBrowser1.DefaultInterface.Document as IHTMLDocument2;
    CurElement := HtmlDoc.elementFromPoint(Pos.X, Pos.Y) as IHTMLElement;
    if CurElement = nil then
      Exit;
    while ((((CurElement as IHTMLElement2).currentStyle.overflow <> 'scroll')
      and ((CurElement as IHTMLElement2).currentStyle.overflow <> 'auto')))
      or ((((CurElement as IHTMLElement2).scrollTop + (CurElement as IHTMLElement).offsetHeight >=
        (CurElement as IHTMLElement2).scrollHeight) and (Delta > 0))
      or (((CurElement as IHTMLElement2).scrollTop <= 0)) and (Delta < 0))
    do begin
      if (CurElement.parentElement = nil) then
        Break;
      if (HtmlDoc.activeElement.tagName = 'SELECT') then
      begin
        CurElement := HtmlDoc.activeElement;
        if Delta > 0 then
        begin
          if (CurElement as IHTMLSelectElement).selectedIndex + 3 < (CurElement as IHTMLSelectElement).length then
            (CurElement as IHTMLSelectElement).selectedIndex := (CurElement as IHTMLSelectElement).selectedIndex + 3
          else
            (CurElement as IHTMLSelectElement).selectedIndex := (CurElement as IHTMLSelectElement).length - 1;
        end
        else
        begin
          if (CurElement as IHTMLSelectElement).selectedIndex - 3 >= 0 then
            (CurElement as IHTMLSelectElement).selectedIndex := (CurElement as IHTMLSelectElement).selectedIndex - 3
          else
            (CurElement as IHTMLSelectElement).selectedIndex := 0;
        end;
        Break;
      end;
      CurElement := CurElement.parentElement;
    end;
    if (Delta > 0) and ((CurElement as IHTMLElement2).scrollTop + Delta >=
      (CurElement as IHTMLElement2).scrollHeight - (CurElement as IHTMLElement).offsetHeight)
    then
      Delta := (CurElement as IHTMLElement2).scrollHeight - (CurElement as IHTMLElement).offsetHeight - (CurElement as IHTMLElement2).scrollTop
    else if (Delta < 0) and ((CurElement as IHTMLElement2).scrollTop + Delta <= 0) then
      Delta := -(CurElement as IHTMLElement2).scrollTop;

    Result := (Delta = 0); // <--- !!! The only thing was actually changed !!!

    (CurElement as IHTMLElement2).ScrollTop := (CurElement as IHTMLElement2).ScrollTop + Delta;
     //    if (CurElement.tagName = 'BODY') or (CurElement.tagName = 'HTML') then
 except
    Result = false; // Will not change direction ...
 end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
webbrowser.Navigate('www.ebay.com');
end;

procedure TForm1.WebBrowserDownloadComplete(Sender: TObject);
begin
timer1.Enabled:=true;
end;

var Lines: Integer = 150;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if TBrowserScrollV(Lines) then Lines := - Lines;
end;

Автор: Mikel 12.2.2010, 09:21
There is a loop, where we search element for scrolling. Find it in DownloadComplete event, and then you may scroll it.
volvo877, wrote example for you with test for end of page.

Автор: muratboy31 12.2.2010, 09:54
thanks for helps

it works perfect but i didnt understand wht it doesnt work good if i use the timer with spin value !!!


Код

procedure TForm1.Timer4Timer(Sender: TObject);
var
Lines:integer;
begin
Lines:=SpinEdit1.value;
if TBrowserScrollV(Lines) then Lines := - Lines;
end;

Автор: volvo877 12.2.2010, 10:15
Цитата(muratboy31 @  12.2.2010,  08:54 Найти цитируемый пост)
i use the timer with spin value
In this case you should change the code:

Код
  TForm1 = class(TForm)
     // ... 
  public
    { Public declarations }
    Lines: Integer; // <--- Lines declared here !!!

    function TBrowserScrollV(Delta: SmallInt): boolean;
    // ...
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  webbrowser.Navigate('www.ebay.com');
  SpinEdit1.Value := 50;
end;
procedure TForm1.WebBrowserDownloadComplete(Sender: TObject);
begin
  timer1.Enabled:=true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if TBrowserScrollV(Lines) then Lines := - Lines;
end;

// Add SpinEdit OnChange event:
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
  Lines := (Sender as TSpinEdit).Value * IfThen(Lines < 0, -1, 1);
end;
It's working now smile

Автор: muratboy31 12.2.2010, 10:25
thanks volvo but what does it mean ;

expression expected but then found 

Код

  private
    { Private declarations }
function TBrowserScrollV(Delta: SmallInt): boolean;
  public
    { Public declarations }
     Lines: Integer;
end;

var
    Form1: TForm1;

implementation

procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
Lines := (Sender as TSpinEdit).Value * If Then (Lines < 0, -1, 1);
end;





Автор: volvo877 12.2.2010, 10:34
Цитата(muratboy31 @  12.2.2010,  09:25 Найти цитируемый пост)
expression expected but then found 
No-no-no... Do you see the space between If and Then in my post? smile I wrote IfThen in ONE word...

Автор: muratboy31 12.2.2010, 10:36
yes yes i also tried it but it said undeclared identifier 'ifthen'  ...

Автор: volvo877 12.2.2010, 10:43
Код
Uses ..., Math;
should help.

Update:
Код
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
  // Lines := (Sender as TSpinEdit).Value * IfThen(Lines < 0, -1, 1);

  // can be changed

  if Lines < 0 then Lines := - (Sender as TSpinEdit).Value
  else Lines := (Sender as TSpinEdit).Value;
end;

Автор: muratboy31 12.2.2010, 10:58
but if i type a number to spinedit then it crashes and also it doesnt start scrooling if i dont spin a number up or down... i used checkedbox to start scrolling but it doesnt start scrolling when i check it, it starts only if i spin a number...



Автор: volvo877 12.2.2010, 11:04
Цитата(muratboy31 @  12.2.2010,  09:58 Найти цитируемый пост)
but if i type a number to spinedit then it crashes and also it doesnt start scrooling if i dont spin a number up or down...
D2009 on WinXP. My code works perfectly (no crash, and scroll starts as I type in spinbox). I think something wrong in OTHER parts of your project.

Автор: muratboy31 12.2.2010, 11:33
i use delphi 7 on win xp and it just doesnt work on design time, if i compile it it works fine...
thanks volvo  smile 
i could never do it on my own...

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)