更改放大/缩小中心点

发布于 2024-12-04 18:05:17 字数 1444 浏览 2 评论 0原文

我正在编写一个显示 XAML 对象的 WPF 应用程序(它基本上是用 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;
    }
}

I'm writing a WPF application that displays a XAML object (it's basically a map drawn in XAML). As part of its features, it should zoom in/out and pan. The panning works fine, and the zoom zooms, but I can't quite understand how to zoom to a specific point, like my mouse cursor, for example.

This is my current code:

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;
}

[edit] Found the solution - Just edited the CenterX / CenterY properties of the transformation and it worked like a charm.
Thanks for all your help!

[edit2] Here's a viable solution (considering the mouse position):

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2024-12-11 18:05:17

您必须使用 TranslateTransform 来组合您的 ScaleTransform,该 TranslateTransform 会在缩放时平移您的组件。

TranslateTransform 给出的偏移量取决于您想要的行为(即以鼠标为中心,以屏幕中心为中心...)

我过去写过一个可以附加到组件的行为:它使其可缩放(以鼠标为中心) ,对鼠标滚轮做出反应)
它很脏,而且不确定是否有效(我不再使用它)...并且注释是法语:-/

查看来源

[编辑] 事实上,我记得它是滚动和缩放面板背景。但将其应用于任何对象应该不会很难修改,因为图像和元素的转换是相同的。

You will have to compose your ScaleTransform with a TranslateTransform which translates your component while zooming.

The offset given by the TranslateTransform depends of the behavior you wanna have (i.e. center on mouse, center on screens center...)

I wrote in the past a behavior which you can attach to a component : It makes it zoomable (centered on mouse, reacting to mousewheel)
It's pretty dirty and not sure to be efficient (i no longer use it)... and comments are in french :-/

see the source

[edit] In fact, I remember it was to scroll and scale a Panels background. But it shouldnt be so hard to modify for applying it to any object as the transformations are the same for images and elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文