我正在使用this example (只使用布局转换,并使用附加行为而不是外部nuget包)来放大我的画布。到目前为止,它似乎还行得通,但我也想限制应用的缩放因子,例如,用户不能放大超过10倍。
为此,我需要找出应用于画布的实际缩放因子。不幸的是,上面的代码没有使用实际的因子,只在已经缩放的控件上应用了下一个ScaleAt操作。
问:我如何找出已经应用的比例因子,以便能够决定是否应用下一个比例因子?
Extra:如何将此因子公开为附加属性的依赖属性,以便可以对其应用来自xaml的绑定?
编辑-添加的代码示例:
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,
}
}发布于 2019-10-13 23:26:46
您可以使用Graphics.Transform的m11和m22元素来获得x和y比例因子。你可以在每个维度上进行不同的“缩放”,这就是为什么你会得到两个因子。
但你永远不会知道你的Graphics对象是为了什么而创建的,它们实际上可以堆叠在另一个之上,更好地跟踪你自己在另一个变量上所做的任何缩放操作。
https://stackoverflow.com/questions/58361252
复制相似问题