0
我需要放大和缩小画布。但变焦应始终以画面中心(不是画布)为中心。这不符合我目前的代码!它始终放大到画布的中心,而不是当前屏幕。 画布也应该是可移动的,但不应该被允许远离它的边界。 这是我到目前为止有:放大和缩小到画布中的视口中心
XAML
<Grid>
<Canvas HorizontalAlignment="Left" Name="canvas1" VerticalAlignment="Top">
</Canvas>
</Grid>
C#
// x & y arent the position, it's how many pixel the object should move to the left/right etc
public void setPosition(double x, double y)
{
Thickness t = new Thickness(canvas1.Margin.Left + x, canvas1.Margin.Top + y, 0, 0);
if (t.Left > 0)
{
t.Left = 0;
}
else if(t.Left < -(canvas1.Width - System.Windows.SystemParameters.PrimaryScreenWidth))
{
t.Left = -(canvas1.Width - System.Windows.SystemParameters.PrimaryScreenWidth);
}
if (t.Top > 0)
{
t.Top = 0;
}
else if (t.Top < -(canvas1.Height - System.Windows.SystemParameters.PrimaryScreenHeight))
{
t.Top = -(canvas1.Height - System.Windows.SystemParameters.PrimaryScreenHeight);
}
canvas1.Margin = t;
}
public void setZoom(double zoom)
{
double tempW = canvas1.Width;
double tempH = canvas1.Height;
canvas1.Width *= (1 + zoom);
canvas1.Height *= (1 + zoom);
Debug.WriteLine("tempW: " + tempW + " tempH: " + tempH + " canvas1.Width: " + canvas1.Width + " canvas1.Height: " + canvas1.Height);
setPosition((tempW - canvas1.Width)/2, ((tempH - canvas1.Height)/2));
}
是的,我知道有这样的东西ScaleTransform但出于某种原因,这是更好的性能..不要问我为什么!
有人可以帮我吗?
这种性能更高的原因是您实际上并没有缩放。你改变画布的大小是不同的,因为如果我没有弄错的话,这将不会影响画布的孩子的大小。 – 2011-04-02 01:52:39
我假设是正确的,你有这个Grid + Canvas里面的ViewBox?该视框本身正在使用ScaleTransform。您可能想尝试跳过视图框并直接使用ScaleTransform – 2011-04-05 20:09:32