Silverlight撤消重做功能的实现。
生活随笔
收集整理的這篇文章主要介紹了
Silverlight撤消重做功能的实现。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
想實現類似畫圖工具那樣,重做撤消可以恢復或擦除最后一次的繪圖。
不知道通用的方法是什么,自己琢磨了這樣的方法,請高手指教。
????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(255,?255,?255,?255));//動態設置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(255,?255,?255,?255));
????????????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?==?0)?return;
????????????if?(arrLine[iCount?-?1]?!=?null)
????????????{
????????????????Board.Children.Remove(arrLine[iCount?-?1]);
????????????????iCount?-=?1;
????????????}
????????}
????}
}
?
源碼:http://files.cnblogs.com/greatverve/SilverlightUndo.rar
總結
以上是生活随笔為你收集整理的Silverlight撤消重做功能的实现。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: QuickFlow之任务代理-TaskD
- 下一篇: 《互联网运营智慧》十一月进展