2011-08-23 44 views
1

我想在一個點上居中放置一個矩形,並且無法弄清楚需要做的數學運算。在給定點處居中畫布

這是在被使用的上下文是其描繪了一個位圖,並允許用戶在指定點進行放大,並且平移/滾動,而在放大的形式。

這是我的代碼針對當前工程圍繞該ClientRectangle中間的CanvasBounds:

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。

回答

0

我認爲你只是需要你的畫布大小抵消你的「點」:

private void UpdateCanvas(Point mousePoint) 
{ 
    int canvasWidth = (int)(_bitmap.Width * _zoomRatio); 
    int canvasHeight = (int)(_bitmap.Height * _zoomRatio); 
    int canvasX = (mousePoint.X - (canvasWidth/2)); 
    int canvasY = (mousePoint.Y - (canvasHeight/2)); 
    CanvasBounds = new Rectangle(canvasX, canvasY, canvasWidth, canvasHeight); 
} 

如果這不是你在找什麼,也許用一個簡單的截屏編輯您的帖子。