【发布时间】:2011-08-24 05:53:02
【问题描述】:
我试图将一个矩形置于一个点的中心,但无法计算出所需的数学运算。
所使用的上下文是一种绘制位图并允许用户在指定点放大并在放大时平移/滚动的表单。
这是我的代码,目前用于将 CanvasBounds 居中于 ClientRectangle 的中间:
private void UpdateCanvas()
{
int canvasWidth = (int)(_bitmap.Width * _zoomRatio);
int canvasHeight = (int)(_bitmap.Height * _zoomRatio);
Point canvasLocation = new Point((ClientRectangle.Width - canvasWidth) / 2, (ClientRectangle.Height - canvasHeight) / 2);
CanvasBounds = new Rectangle(canvasLocation, new Size(canvasWidth, canvasHeight));
}
_zoomRatio 是调整画布大小的缩放比例。 1.0 是 100%,2.0 是 200%,等等。
基本上,我想为这个函数提供一个鼠标输入点,并将该点用作 canvasBounds 矩形的中心。然后当用户操作水平和垂直滚动条时,它可以改变_centerPoint并更新CanvasBounds。
【问题讨论】: