自动滚动和x,y之间的关系 [英] relation between autoscrollposition and x,y

查看:182
本文介绍了自动滚动和x,y之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 如果我们有一个带有图片框的面板(panel.size>picture Box.size)
可以在面板的autoScrollPosition和图片框中的点的x,y之间找到任何关系吗?

Hi if we have a panel with a picture Box in it(panel.size>picture Box.size)
can we find any relation between autoScrollPosition of the panel and x,y of a point in the picture Box?

best regards.

推荐答案

两者之间的关系是AutoScrollPosition相对于Panel.Location偏移了PictureBox.Location,而PictureBox中的点是与PistureBox.Location的偏移量.


要更改AutoScrollPosition,需要为其分配一个Point对象,该对象代表您希望其移动到的偏移量.换句话说:
The relationship between the two is that the AutoScrollPosition is offsetting the PictureBox.Location relative to the Panel.Location, and the point in the PictureBox is an offset from the PistureBox.Location.


To change the AutoScrollPosition, you need to assign it a Point object that represents the offset that you want it to move to. In other words:
panel1.AutoScrollPosition = new Point(50,100);


会导致水平滚动条向右移动50个点,垂直滚动条将向下移动100个点.听起来您正在尝试显示地图,并在用户单击地图时将其自动居中,对吗?如果是这样,您的Mouseclick事件将执行以下操作:


would cause the horizontal scrollbar to move 50 points to the right and the vertical scrollbar would move down 100 points. It sounds like you are trying to display a map and have it automatically center when the user clicks on it, is that right? If so, your Mouseclick event would do something like this:

private void pictureBox1_MouseClick ( object sender, MouseEventArgs e )
{
	Point panelcenter = new Point ( ( this.panel1.Width / 2 ), ( this.panel1.Height / 2 ) ); // find the centerpoint of the panel
	Point offsetinpicturebox = new Point ( ( this.pictureBox1.Location.X + e.Location.X ), ( this.pictureBox1.Location.Y + e.Location.Y ) ); // find the offset of the mouse click
	Point offsetfromcenter = new Point ( ( panelcenter.X - offsetinpicturebox.X ), ( panelcenter.Y - offsetinpicturebox.Y ) ); // find the difference between the mouse click and the center

	this.panel1.AutoScrollPosition = new Point ( 
		( Math.Abs ( this.panel1.AutoScrollPosition.X ) + ( -1 * offsetfromcenter.X ) ), 
		( Math.Abs ( this.panel1.AutoScrollPosition.Y ) + ( -1 * offsetfromcenter.Y ) ) 
	);
}



Note:重要的是要认识到,当您检索AutoScrollPosition时,它将以负值返回.但是,在分配它时,需要分配正值.



Note: It is important to realize that when you retrieve the AutoScrollPosition, it will return with negative values. However, when you assign it, you need to assign positive values.


这篇关于自动滚动和x,y之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆