How to get and set TChromium scrollbar positions?
Asked Answered
A

3

9

How to get and set TChromium scrollbar positions in Delphi ?

Arvo answered 17/12, 2012 at 12:20 Comment(2)
Voted to reopen. This is in my view a valid and useful question! There's not much to research (so not much to show as well), since it's probably impossible to get or set scrollbar positions in CEF.Paterfamilias
Thank you for answer, I will try to another solution.Arvo
C
4

It's possible to work with javascript objects directly. Just use CefV8Context of the frame.

Here is an example:

var
    val: ICefV8Value;
    context: ICefv8Context;
    excp: ICefV8Exception;
    scroll: TPoint;
begin
    if (Chromium1.Browser.MainFrame = nil) then
      exit;

    //this will work only with exact frame
    context := Chromium1.Browser.MainFrame.GetV8Context;

    if (context <> nil) then
    begin
        context.Eval('window.pageXOffset', val, excp);
        scroll.x := val.GetIntValue;
        context.Eval('window.pageYOffset', val, excp);
        scroll.y := val.GetIntValue;
    end
    else
      exit;

    //todo: do something with scroll here
end;
Charley answered 18/11, 2013 at 16:18 Comment(0)
F
1

Currently playing with CefSharp, I do think that this is similar than in Delphi. Here is my solution:

public int GetVerticalScrollPosition()
{
    var r = _webView.EvaluateScript(@"document.body.scrollTop");
    return Convert.ToInt32(r);
}

public void SetVerticalScrollPosition(int pos)
{
    _webView.ExecuteScript(
        string.Format(@"document.body.scrollTop = {0}", pos));
}

I'm not that Delphi expert anymore, hope you can understand my code; basically I use JavaScript to read/write the scroll positions and execute these small JavaScript snippets through the EvaluateScript and ExecuteScript methods.

Furfur answered 29/5, 2013 at 19:3 Comment(0)
M
0

You need to use JavaScript in TCromium.Browser. That's the easiest way:

Chromium1.Browser.MainFrame.ExecuteJavaScript('window.scrollBy(0,50)', 'about:blank', 0);

Good luck!

Maintain answered 11/4, 2017 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.