之前做墨迹复位时总是会偏移,其实还是顺序的问题:下面是代码
前台界面:
<Window x:Class="_1_7Matrix.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="1000">
<Grid Name="grid" Background="White" >
<InkCanvas Name="ink" Background="AliceBlue" Margin="0,60,0,0">InkCanvas>
<Button Content="漫游" Height="23" HorizontalAlignment="Left" Margin="40,20,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="复位" Height="23" HorizontalAlignment="Left" Margin="140,20,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
<Button Content="画图" Height="24" HorizontalAlignment="Left" Margin="249,20,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="button3_Click" />
<Button Content="放大" Height="23" HorizontalAlignment="Left" Margin="347,21,0,0" Name="button4" VerticalAlignment="Top" Width="79" Click="button4_Click" />
<Button Content="缩小" Height="23" HorizontalAlignment="Left" Margin="452,21,0,0" Name="button5" VerticalAlignment="Top" Width="75" Click="button5_Click" />
<Button Content="清空" Height="23" HorizontalAlignment="Left" Margin="550,21,0,0" Name="button6" VerticalAlignment="Top" Width="75" Click="button6_Click" />
Grid>
Window>
后台:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
grid.MouseDown += new MouseButtonEventHandler (grid_PreviewMouseDown);
grid.MouseMove += new MouseEventHandler (grid_PreviewMouseMove);
grid.MouseUp += new MouseButtonEventHandler (grid_PreviewMouseUp);
}
void grid_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
hashcode = 0;
grid.ReleaseMouseCapture();
}
void grid_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (sender.GetHashCode() != hashcode) return;
Point NewPoint = e.GetPosition(grid);
Vector v = NewPoint - oldpoint;
oldpoint = NewPoint;
Matrix m = Matrix .Identity;
m.Translate(v.X, v.Y);
ink.Strokes.Transform(m, false);
matrixlist.Add(m);
m.Invert(); //**所以提前反转**
matrixlist2.Add( new MatrixAndBool (m, false));
}
int hashcode = 0;
Point oldpoint = new Point();
void grid_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (!canMove) return ;
hashcode = sender.GetHashCode();
oldpoint = e.GetPosition(grid);
grid.CaptureMouse();
}
bool canMove = false ;
private void button1_Click(object sender, RoutedEventArgs e)
{
canMove = true;
}
List matrixlist = new List();
List matrixlist2 = new List();
private void button2_Click(object sender, RoutedEventArgs e)
{
//matrixlist.Reverse();
//matrixlist.ForEach(_ => {
// _.Invert();
// ink.Strokes.Transform(_,false);
//});
//matrixlist.Clear();
matrixlist2.Reverse();
matrixlist2.ForEach(_ =>
{
//_.M.Invert(); //**此种集合在这里反转已经没有作用**
ink.Strokes.Transform(_.M, true);
});
matrixlist2.Clear();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
canMove = false;
}
private void button4_Click(object sender, RoutedEventArgs e)
{
Matrix m = Matrix .Identity;
m.ScaleAt(1.1,1.1,ink.Width / 2, ink.Height / 2);
m.OffsetX = 0;
m.OffsetY = 0;
ink.Strokes.Transform(m, true);
//matrixlist.Add(m);
m.Invert();
matrixlist2.Add( new MatrixAndBool (m, true));
}
private void button5_Click(object sender, RoutedEventArgs e)
{
Matrix m = Matrix .Identity;
m.ScaleAt(0.9, 0.9, ink.Width / 2, ink.Height / 2);
m.OffsetX = 0;
m.OffsetY = 0;
ink.Strokes.Transform(m, true);
//matrixlist.Add(m);
m.Invert();
matrixlist2.Add( new MatrixAndBool (m, true));
}
private void button6_Click(object sender, RoutedEventArgs e)
{
ink.Strokes.Clear();
}
}
public class MatrixAndBool
{
public Matrix M { get; set; }
public bool Value { get; set; }
public MatrixAndBool(Matrix matrix,bool b)
{
M = matrix;
Value = b;
}
}