首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Canvas LayoutTransform获取实际比例

Canvas LayoutTransform获取实际比例
EN

Stack Overflow用户
提问于 2019-10-13 14:55:15
回答 1查看 111关注 0票数 1

我正在使用this example (只使用布局转换,并使用附加行为而不是外部nuget包)来放大我的画布。到目前为止,它似乎还行得通,但我也想限制应用的缩放因子,例如,用户不能放大超过10倍。

为此,我需要找出应用于画布的实际缩放因子。不幸的是,上面的代码没有使用实际的因子,只在已经缩放的控件上应用了下一个ScaleAt操作。

问:我如何找出已经应用的比例因子,以便能够决定是否应用下一个比例因子?

Extra:如何将此因子公开为附加属性的依赖属性,以便可以对其应用来自xaml的绑定?

编辑-添加的代码示例:

代码语言:javascript
复制
public static class ZoomBehavior
{
    //example from https://stackoverflow.com/questions/46424149/wpf-zoom-canvas-center-on-mouse-position

    #region ZoomFactor
    public static double GetZoomFactor(DependencyObject obj)
    {
        return (double)obj.GetValue(ZoomFactorProperty);
    }
    public static void SetZoomFactor(DependencyObject obj, double value)
    {
        obj.SetValue(ZoomFactorProperty, value);
    }
    // Using a DependencyProperty as the backing store for ZoomFactor.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ZoomFactorProperty =
        DependencyProperty.RegisterAttached("ZoomFactor", typeof(double), typeof(ZoomBehavior), new PropertyMetadata(1.05));
    #endregion

    #region ModifierKey       
    public static ModifierKeys? GetModifierKey(DependencyObject obj)
    {
        return (ModifierKeys?)obj.GetValue(ModifierKeyProperty);
    }
    public static void SetModifierKey(DependencyObject obj, ModifierKeys? value)
    {
        obj.SetValue(ModifierKeyProperty, value);
    }
    // Using a DependencyProperty as the backing store for ModifierKey.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ModifierKeyProperty =
        DependencyProperty.RegisterAttached("ModifierKey", typeof(ModifierKeys?), typeof(ZoomBehavior), new PropertyMetadata(null));
    #endregion
    public static TransformMode TranformMode { get; set; } = TransformMode.Layout;
    private static Transform _transform;
    private static DesignerCanvas _canvas;

    #region IsZoomable        
    public static bool GetIsZoomable(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsZoomableProperty);
    }
    public static void SetIsZoomable(DependencyObject obj, bool value)
    {
        obj.SetValue(IsZoomableProperty, value);
    }
    // Using a DependencyProperty as the backing store for IsZoomable.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty IsZoomableProperty =
        DependencyProperty.RegisterAttached(
            "IsZoomable",
            typeof(bool),
            typeof(ZoomBehavior),
            new UIPropertyMetadata(false, OnIsZoomableChanged));
    #endregion

    //#region ScaleMax
    //#region ScaleMin

    private static void OnIsZoomableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        _canvas = d as DesignerCanvas;
        if (null == _canvas)
        {
            System.Diagnostics.Debug.Assert(false, "Wrong dependency object type");
            return;
        }
        if ((e.NewValue is bool) == false)
        {
            System.Diagnostics.Debug.Assert(false, "Wrong value type assigned to dependency object");
            return;
        }

        if (true == (bool)e.NewValue)
        {
            _canvas.MouseWheel += Canvas_MouseWheel;
            if (TranformMode == TransformMode.Render)
            {
                _transform = _canvas.RenderTransform = new MatrixTransform();
            }
            else
            {
                _transform = _canvas.LayoutTransform = new MatrixTransform();
            }
        }
        else
        {
            _canvas.MouseWheel -= Canvas_MouseWheel;
        }
    }

    private static void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        ModifierKeys? modifierkey = GetModifierKey(sender as DependencyObject);

        if (!modifierkey.HasValue)
        {
            return;
        }

        if((Keyboard.Modifiers & (modifierkey.Value)) == ModifierKeys.None)
        {
            return;
        }

        if (!(_transform is MatrixTransform transform))
        {
            return;
        }

        var pos1 = e.GetPosition(_canvas);
        //double max = GetScaleMax(sender as DependencyObject);
        //double min = GetScaleMin(sender as DependencyObject);

        double zoomfactor = GetZoomFactor(sender as DependencyObject);            
        double scale = (e.Delta < 0) ? zoomfactor : (1 / zoomfactor);
        //scale = (scale > max) ? max : scale;
        //scale = (scale < min) ? min : scale;

        var mat = transform.Matrix;            
        mat.ScaleAt(scale, scale, pos1.X, pos1.Y);
        transform.Matrix = mat;
        e.Handled = true;
    }

    public enum TransformMode
    {
        Layout,
        Render,
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-13 23:26:46

您可以使用Graphics.Transform的m11和m22元素来获得x和y比例因子。你可以在每个维度上进行不同的“缩放”,这就是为什么你会得到两个因子。

但你永远不会知道你的Graphics对象是为了什么而创建的,它们实际上可以堆叠在另一个之上,更好地跟踪你自己在另一个变量上所做的任何缩放操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58361252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档