我正在編寫一個WPF應用程序,它顯示一個XAML對象(它基本上是一個在XAML中繪製的地圖)。作爲其功能的一部分,它應該放大/縮小和平移。平移工作正常,縮放放大,但我不明白如何縮放到特定點,例如我的鼠標光標。更改放大/縮小中心點
這是我當前的代碼:
internal void PerformZoom(float ZoomFactor, Point ZoomCenterPoint)
{
m_originalTransform = m_canvas.RenderTransform;
float newZoomFactor = m_oldZoomFactor + ZoomFactor;
float scaleToApply = (newZoomFactor/m_oldZoomFactor);
m_totalZoom = newZoomFactor;
var st = new ScaleTransform(scaleToApply, scaleToApply);
TransformGroup tg = new TransformGroup();
tg.Children.Add(m_originalTransform);
tg.Children.Add(st);
m_canvas.RenderTransform = tg;
m_oldZoomFactor = newZoomFactor;
}
[編輯]找到了解決辦法 - 只是編輯的轉型的centerX/CenterY屬性和它的工作就像一個魅力。 感謝您的幫助!
[EDIT2]這裏是一個可行的解決方案(考慮到鼠標的位置):
public partial class MainWindow
{
private float currentZoom = 1f;
private const float StepSize = .2f;
public MainWindow()
{
InitializeComponent();
}
private void MainGrid_OnMouseWheel(object sender, MouseWheelEventArgs e)
{
var pos = 1;
if (e.Delta < 0)
{
pos = -1;
}
var mousePosition = e.MouseDevice.GetPosition(MainGrid);
currentZoom += StepSize * pos;
var transform = new ScaleTransform(currentZoom, currentZoom, mousePosition.X, mousePosition.Y);
MainGrid.RenderTransform = transform;
}
}
爲什麼不顯示你做別人可能會發現它有幫助。我不知道你提到的轉換的* CenterX/CenterY屬性。 –