简体   繁体   English

在ZoomableCanvas中使用Viewbox时围绕点进行缩放吗?

[英]Zooming about point when using a Viewbox inside a ZoomableCanvas?

Thanks to Kael Rowan, you can easily create a zoomable canvas in WPF . 感谢Kael Rowan,您可以轻松地在WPF创建可缩放的画布。 Sample project can be downloaded at the bottom of this blog entry . 可以在此博客条目的底部下载示例项目

Now, I need to modify this sample project to use its Viewbox feature. 现在,我需要修改此示例项目以使用其Viewbox功能。 In MainWindow.xaml set the Viewbox , Stretch and ApplyTransform properties as follows: MainWindow.xaml ,如下设置ViewboxStretchApplyTransform属性:

<ZoomableCanvas ... Viewbox="0 0 400 400" Stretch="Fill" ApplyTransform="True" />

The problem 问题

With this feature enabled, the zooming using the mouse wheel does not zoom about the mouse position. 启用此功能后,使用鼠标滚轮进行缩放不会围绕鼠标位置进行缩放。 Instead, it seems to zoom around (0,0). 相反,它似乎会缩放(0,0)。 This piece of code (in the mouse wheel handler) works without Viewbox : 这段代码(在鼠标滚轮处理程序中) 无需 Viewbox

// Adjust the offset to make the point under the mouse stay still.
var position = (Vector)e.GetPosition(MyListBox);
MyCanvas.Offset = (Point)((Vector)(MyCanvas.Offset + position) * x - position);

How can I get it working with a Viewbox ? 如何使用 Viewbox This is driving me nuts! 这真让我发疯! Math/WPF gurus out there, please help! 数学/ WPF专家在那里,请帮助!

Try this code 试试这个代码

protected override void OnMouseWheel(MouseWheelEventArgs e)
{ 
    var position = e.GetPosition(this.MyCanvas);

    var pre = this.MyCanvas.TransformToAncestor(this).Transform(position);

    var x = Math.Pow(2, e.Delta / 3.0 / Mouse.MouseWheelDeltaForOneLine);
    MyCanvas.Scale *= x;

    var cur = this.MyCanvas.TransformToAncestor(this).Transform(position);

    var offset = (cur - pre);

    this.MyCanvas.Offset += offset;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM