用WebBrowser控件包含在一个Panel控件滚动问题控件、问题、WebBrowser、Panel

2023-09-07 00:45:36 作者:我的青春在学校

我有一个包含一个子控件这是一个web浏览器。净面板控制。我不会去的原因,我这样做,但它关系到打印出的控制。面板控制有其AutoScroll属性设置为真,我上浆web浏览器,以适应自己的内容(通过使用NavigateComplete2事件在web浏览器的.Document.Body.ScrollRectangle.Size属性)。以这种方式,在面板上的滚动条出现并且可以以能够看到的web浏览器的内容上下滚动面板

I have a .Net Panel control that contains a single child control that is a WebBrowser. I won't go into the reasons for me doing that, but it is related to printing out the control. The panel control has its AutoScroll property set to "true" and I am sizing the WebBrowser to fit its own content (by using the .Document.Body.ScrollRectangle.Size property of the WebBrowser when the NavigateComplete2 event fires). In this way, the scrollbar on the panel appears and you can scroll the panel up and down in order to be able to see the content of the WebBrowser.

现在的问题是,当你向下滚动,看看有什么在web浏览器的底部,然后点击它(也许你点击的HTML链接),面板跳回到顶端,链接不得到付诸行动。

The problem is that when you scroll down to see what's at the bottom of the WebBrowser and then click on it (perhaps you click on a link in the html), the panel jumps back to the top and the link doesn't get actioned.

请谁能帮助我明白这是怎么回事,如何解决这个问题?

Please can anyone help me to understand what's going on and how to get around this problem?

推荐答案

我有同样的问题,也有一个小组内的web浏览器。下面是我使用的解决方案,(我发现别的地方上的计算器):

I had the same problem, also with a WebBrowser inside a Panel. Here's the solution I'm using (which I found somewhere else on 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;
	}
}