Silverlight撤消重做功能的实现。

想实现类似画图工具那样,重做撤消可以恢复或擦除最后一次的绘图。

不知道通用的方法是什么,自己琢磨了这样的方法,请高手指教。

大气象
<UserControl x:Class="SilverlightUndo.MainPage"
    xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d
="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable
="d" d:DesignWidth="640" d:DesignHeight="480">
  
<Grid x:Name="LayoutRoot">
        
<Grid.RowDefinitions>
            
<RowDefinition Height="50"></RowDefinition>
            
<RowDefinition Height="*"></RowDefinition>
        
</Grid.RowDefinitions>
        
<Grid.ColumnDefinitions>
            
<ColumnDefinition Width="50"></ColumnDefinition>
            
<ColumnDefinition Width="50"></ColumnDefinition>
            
<ColumnDefinition Width="*"></ColumnDefinition>
        
</Grid.ColumnDefinitions>
        
<Button Grid.Row="0" Grid.Column="0" Content="重做" Name="btnRedo" Click="btnRedo_Click"></Button>
        
<Button Grid.Row="0" Grid.Column="1" Content="撤消" Name="btnUndo" Click="btnUndo_Click"></Button>
        
<Canvas Name="Board" MouseLeftButtonDown="Canvas_MouseLeftButtonDown" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Background="Black"></Canvas>
    
</Grid>
</UserControl>
大气象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightUndo
{
    
public partial class MainPage : UserControl
    {
        
private bool IsStart = true;//是否是起点。
        private double pX1 = 0, pY1 = 0;//起点坐标
        Line[] arrLine = new Line[100];//silverlight不支持ArrayList之类。
        int iCount = 0;//计数器

        
public MainPage()
        {
            InitializeComponent();
        }

        
private void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Point p 
= e.GetPosition(sender as FrameworkElement);//取得鼠标点下的位置
            Canvas pnl = sender as Canvas;
            
if (IsStart)
            {
                pX1 
= p.X;//设置起点坐标
                pY1 = p.Y;
            }
            
if (IsStart)//如果是起点,则画起点。
            {
                DrawPoint(pnl, p.X, p.Y);
            }
            
else
            {
                DrawOneLine(pnl, pX1, pY1, p.X, p.Y);
            }
            IsStart 
= !IsStart;
        }
        
//画点
        private void DrawPoint(Canvas pnl, double pX1, double pY1)
        {
            Ellipse ellipse 
= new Ellipse();//画点
            ellipse.Stroke = new SolidColorBrush(Color.FromArgb(255255255255));//动态设置Stroke属性的方法。
            ellipse.StrokeThickness = 2;
            ellipse.Width 
= 4;
            ellipse.Height 
= 4;
            Canvas.SetLeft(ellipse, pX1);
//动态设置Ellipse的Canvas.Top与Canvas.Left
            Canvas.SetTop(ellipse, pY1);

            pnl.Children.Add(ellipse);
        }
        
//画直线
        private void DrawOneLine(Canvas pnl, double pX1, double pY1, double pX2, double pY2)
        {
            Line line 
= new Line();
            line.X1 
= pX1;
            line.Y1 
= pY1;
            line.X2 
= pX2;
            line.Y2 
= pY2;

            line.Stroke 
= new SolidColorBrush(Color.FromArgb(255255255255));
            line.StrokeThickness 
= 4;

            pnl.Children.Add(line);

            
//计数器
            arrLine[iCount] = line;
            iCount 
+= 1;
        }

        
private void btnRedo_Click(object sender, RoutedEventArgs e)
        {
            
if (arrLine[iCount] != null)
            {
                Board.Children.Add(arrLine[iCount]);
                iCount 
+= 1;
            }
        }

        
private void btnUndo_Click(object sender, RoutedEventArgs e)
        {
            
if (iCount == 0return;
            
if (arrLine[iCount - 1!= null)
            {
                Board.Children.Remove(arrLine[iCount 
- 1]);
                iCount 
-= 1;
            }
        }
    }
}

 

源码:https://files.cnblogs.com/greatverve/SilverlightUndo.rar

posted @ 2010-11-02 09:55  大气象  阅读(972)  评论(3编辑  收藏  举报
http://www.tianqiweiqi.com