private Point lastmousepoint;
private void Window_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
{
Point mousepos = e.GetPosition(this);
foreach (UIElement element in canvas1.Children)
{
Canvas.SetLeft(element, Canvas.GetLeft(element) + (mousepos.X - lastmousepoint.X));
Canvas.SetTop(element, Canvas.GetTop(element) + (mousepos.Y - lastmousepoint.Y));
lastmousepoint = mousepos;
}
}
e.Handled = true;
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
lastmousepoint = e.GetPosition(this);
e.Handled = true;
}
private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
但这两个文本中只有一个在移动,而它们都应该移动。实际的运动是好的,并按预期工作。
此代码
private int CurrentScaleLevel = 0;
private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
{
foreach (UIElement element in canvas1.Children)
{
Point p = e.MouseDevice.GetPosition(element);
Matrix m = element.RenderTransform.Value;
if (e.Delta > 0)
{
CurrentScaleLevel++;
m.ScaleAtPrepend(1.1, 1.1, p.X, p.Y);
}
else
{
CurrentScaleLevel--;
m.ScaleAtPrepend(1 / 1.1, 1 / 1.1, p.X, p.Y);
}
Canvas.SetLeft(element, Canvas.GetLeft(element) + m.OffsetX);
Canvas.SetTop(element, Canvas.GetTop(element) + m.OffsetY);
m.Translate(-m.OffsetX, -m.OffsetY);
element.RenderTransform = new MatrixTransform(m);
}
e.Handled = true;
}