当 WebBrowser 缩放时滚动到某个点

发布于 2024-12-12 19:20:48 字数 1720 浏览 1 评论 0原文

我正在尝试放大 .Net WebBrowser 控件中加载的网页上的图片。 例如,(100,100) 处有一张缩放为 100% 的图片。现在我想将缩放设置为 200%,并将图片放在 WebBrowser 控件的左上角。

几乎可以用了。如果该点位于屏幕左侧,则该点有效。但如果该点位于屏幕右侧,则新计算的点不正确。 Y 总是正确的。只有 X 给我带来了问题。

任何人都知道 ScrollTo 与当前缩放系数有何关系。还是还与其他变量有关?我怀疑问题还出在与屏幕尺寸相关的网页适配上。 (缩放时,它会删除不包含任何信息的网页的左右边缘。之后,它会放大内容)。我需要真正的缩放,其中缩放仅影响内容,而不影响布局。缩放时不会引起回流。

public class MyBrowser : WebBrowser

    private int _zoom = 100;
    public int Zoom
    {
        set
        {
            _zoom = value;

            //perform zoom
            object pvaIn = _zoom;
            try
            {
                this._axIWebBrowser2.ExecWB(
                    OLECMDID.OLECMDID_OPTICAL_ZOOM,
                    OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
                    ref pvaIn, 
                    IntPtr.Zero);
            }
            catch (Exception)
            {
                throw;
            }
        }
        get
        {
            return _zoom;
        }
    }

    private Point GetCurrentScroll()
    {
        int scrollX = this.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
        int scrollY = this.Document.GetElementsByTagName("HTML")[0].ScrollTop;

        return new Point(scrollX, scrollY);
    }

    public void ZoomOnPoint(int x, int y, int zoomFactor)
    {
        int currentZoom = this.Zoom;

        Point currentScroll = GetCurrentScroll();

        //Set new values
        this.Zoom = newZoom;
        this.Document.Window.ScrollTo(
            currentScroll.X + ((x * currentZoom) / zoomFactor), //X is sometimes correct, mostly on the rightside of the screen
            currentScroll.Y + y);                               //Y is correct
    }
}

I'm trying to zoom into a picture on a webpage loaded in the .Net WebBrowser control.
For example, there's a picture at (100,100) on 100% zoom. Now I want to set the zoom to 200% and have the picture at the top left in the WebBrowser control.

It almost works. If the point is on the left side of the screen it works. But if the point is more to the right of the screen, new calculated points are not correct.
Y is always correct. Only the X is giving me problems.

Anyone know how the ScrollTo is related to the current zoom factor. Or is it also related to other variables? I suspect the problem is also the adaptation of the webpage related to the size of the screen. (on zooming it removes the left and right edge of a webpage that doesn't hold any information. After that it enlarges the content). I need true zoom where the scaling affects only the content, not the layout. There is no reflow caused when scaling.

public class MyBrowser : WebBrowser

    private int _zoom = 100;
    public int Zoom
    {
        set
        {
            _zoom = value;

            //perform zoom
            object pvaIn = _zoom;
            try
            {
                this._axIWebBrowser2.ExecWB(
                    OLECMDID.OLECMDID_OPTICAL_ZOOM,
                    OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,
                    ref pvaIn, 
                    IntPtr.Zero);
            }
            catch (Exception)
            {
                throw;
            }
        }
        get
        {
            return _zoom;
        }
    }

    private Point GetCurrentScroll()
    {
        int scrollX = this.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
        int scrollY = this.Document.GetElementsByTagName("HTML")[0].ScrollTop;

        return new Point(scrollX, scrollY);
    }

    public void ZoomOnPoint(int x, int y, int zoomFactor)
    {
        int currentZoom = this.Zoom;

        Point currentScroll = GetCurrentScroll();

        //Set new values
        this.Zoom = newZoom;
        this.Document.Window.ScrollTo(
            currentScroll.X + ((x * currentZoom) / zoomFactor), //X is sometimes correct, mostly on the rightside of the screen
            currentScroll.Y + y);                               //Y is correct
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2024-12-19 19:20:48

在 WPF 中,我将 X 和 Y 值与 H 和 V 缩放百分比相乘,它有效:)

In WPF, I multiply the X and Y values with the H and V zoom perc, it works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文