MatrixTransform结合画布的宽度/高度 [英] MatrixTransform in combination with Width/Height of a Canvas
问题描述
在画布上放大图形时,我需要显示滚动条。
画布在ScrollViewer中,并且我增加了画布的宽度/高度,以便出现滚动条(
When zooming in on a drawing on a Canvas I have the requirement to show scrollbars.
The Canvas is in a ScrollViewer and I increase the Width/Height of the Canvas so that that the scollbars appear (otherwise they don't).
要放大1.1倍,请使用以下代码:
To zoom in with a factor of 1.1 I use this code:
Matrix m = this.LayoutTransform.Value;
if (e.Delta > 0) f = 1.1;
else f = 1.0 / 1.1;
m.Scale(f, f);
this.LayoutTransform = new MatrixTransform(m);
this.Height = this.ActualHeight * f;
this.Width = this.ActualWidth * f;
事实证明Canvas变得太大了。工程图放大10%,但宽度似乎变得像1.1的平方一样多20%。因此,我使用 Math.Sqrt(f);
而不是 f
。
It turns out that the Canvas becomes much too large. The drawing zooms in 10% but the width seems to become 20% more like the square of 1.1. So I use Math.Sqrt(f);
instead of f
.
有人可以解释为什么这样做吗?
Can anybody explain why it behaves this way?
推荐答案
您应该只更改Canvas的LayoutTransform,例如在此简化示例中:
You should only change the LayoutTransform of the Canvas, like in this simplified example:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Canvas Width="1000" Height="1000" Background="Transparent"
MouseWheel="Canvas_MouseWheel">
<Canvas.LayoutTransform>
<MatrixTransform/>
</Canvas.LayoutTransform>
</Canvas>
</ScrollViewer>
MouseWheel事件处理程序:
The MouseWheel event handler:
private void Canvas_MouseWheel(object sender, MouseWheelEventArgs e)
{
var element = (FrameworkElement)sender;
var transform = (MatrixTransform)element.LayoutTransform;
var matrix = transform.Matrix;
var scale = e.Delta >= 0d ? 1.1 : (1d / 1.1);
matrix.Scale(scale, scale);
transform.Matrix = matrix;
e.Handled = true;
}
这篇关于MatrixTransform结合画布的宽度/高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!