繁体   English   中英

Panel控件中包含的WebBrowser控件的滚动问题

[英]Scrolling problem with a WebBrowser control contained in a Panel control

我有一个.Net Panel控件,其中包含一个子控件,即WebBrowser。 我不会介绍这样做的原因,但这与打印控件有关。 面板控件的AutoScroll属性设置为“ true”,并且我正在调整WebBrowser的大小以适合其自身的内容(在NavigateComplete2事件触发时,使用WebBrowser的.Document.Body.ScrollRectangle.Size属性)。 这样,将出现面板上的滚动条,您可以上下滚动面板以查看WebBrowser的内容。

问题是,当您向下滚动以查看WebBrowser底部的内容然后单击它时(也许您单击html中的链接),面板将跳回到顶部,并且该链接不起作用。

请谁能帮助我了解发生了什么以及如何解决此问题?

我也遇到了同样的问题,面板中也有一个WebBrowser。 这是我正在使用的解决方案(我在stackoverflow上的其他地方找到了):

class AutoScrollPanel : Panel
{
    public AutoScrollPanel()
    {
        Enter += PanelNoScrollOnFocus_Enter;
        Leave += PanelNoScrollOnFocus_Leave;
    }

    private System.Drawing.Point scrollLocation;

    void PanelNoScrollOnFocus_Enter(object sender, System.EventArgs e)
    {
        // Set the scroll location back when the control regains focus.
        HorizontalScroll.Value = scrollLocation.X;
        VerticalScroll.Value = scrollLocation.Y;
    }

    void PanelNoScrollOnFocus_Leave(object sender, System.EventArgs e)
    {
        // Remember the scroll location when the control loses focus.
        scrollLocation.X = HorizontalScroll.Value;
        scrollLocation.Y = VerticalScroll.Value;
    }

    protected override System.Drawing.Point ScrollToControl(Control activeControl)
    {
        // When the user clicks on the webbrowser, .NET tries to scroll to 
        //  the control. Since it's the only control in the panel it will 
        //  scroll up. This little hack prevents that.
        return DisplayRectangle.Location;
    }
}

尝试在包含面板和WebBrowser控件上都将TabStop设置为false。 那对我有用。 起作用的原因是,如果将其设置为希望成为Tabstop的对象,则它将以first click事件表示它正在获得关注。 然后重新设置滚动条的位置...不确定为什么这样做...

但是,在导航到新页面时,您需要手动重置滚动条位置...

这是我用的。 是的,这是骇客。

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        webBrowser1.TabStop = true;
        webBrowser1.Focus();
        webBrowser1.TabStop = false;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM