Wpf中畫板墨跡的移動與復位
阿新 • • 發佈:2019-01-08
之前做墨跡復位時總是會偏移,其實還是順序的問題:下面是程式碼
前臺介面:
<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<Matrix > matrixlist = new List<Matrix >();
List<MatrixAndBool > matrixlist2 = new List<MatrixAndBool >();
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;
}
}
效果圖: