@@ -44,6 +44,10 @@ public partial class MainWindow : Window
44
44
int prevX ;
45
45
int prevY ;
46
46
47
+ // undo
48
+ const int maxUndoCount = 100 ;
49
+ int currentUndoIndex = 0 ;
50
+ WriteableBitmap [ ] undoBufferBitmap = new WriteableBitmap [ maxUndoCount ] ;
47
51
48
52
public MainWindow ( )
49
53
{
@@ -68,6 +72,7 @@ void Start()
68
72
drawingImage . MouseRightButtonDown += new MouseButtonEventHandler ( DrawingRightButtonDown ) ;
69
73
drawingImage . MouseDown += new MouseButtonEventHandler ( DrawingMiddleButtonDown ) ;
70
74
w . MouseWheel += new MouseWheelEventHandler ( drawingMouseWheel ) ;
75
+ drawingImage . MouseUp += new MouseButtonEventHandler ( DrawingMouseUp ) ;
71
76
72
77
// build palette
73
78
paletteImage = imgPalette ;
@@ -221,7 +226,6 @@ void DrawPixel(int x, int y)
221
226
222
227
prevX = x ;
223
228
prevY = y ;
224
-
225
229
}
226
230
227
231
void ErasePixel ( MouseEventArgs e )
@@ -306,6 +310,14 @@ void DrawingLeftButtonDown(object sender, MouseButtonEventArgs e)
306
310
DrawPixel ( x , y ) ;
307
311
}
308
312
313
+ void DrawingMouseUp ( object sender , MouseButtonEventArgs e )
314
+ {
315
+ // undo test
316
+ undoBufferBitmap [ ++ currentUndoIndex ] = canvasBitmap . Clone ( ) ;
317
+ Console . WriteLine ( "save undo " + currentUndoIndex ) ;
318
+ }
319
+
320
+
309
321
void DrawingAreaMouseMoved ( object sender , MouseEventArgs e )
310
322
{
311
323
// update mousepos info
@@ -421,5 +433,15 @@ private void OpacitySliderValueChanged(object sender, RoutedPropertyChangedEvent
421
433
// ... Set Window Title.
422
434
//this.Title = "Value: " + value.ToString("0.0") + "/" + slider.Maximum;
423
435
}
436
+
437
+ private void OnUndoButtonDown ( object sender , RoutedEventArgs e )
438
+ {
439
+ if ( currentUndoIndex > 0 )
440
+ {
441
+ canvasBitmap = undoBufferBitmap [ -- currentUndoIndex ] ;
442
+ Console . WriteLine ( "restore undo " + currentUndoIndex ) ;
443
+ imgCanvas . Source = canvasBitmap ;
444
+ }
445
+ }
424
446
} // class
425
447
} // namespace
0 commit comments