private void cutCanavas(object sender, RoutedEventArgs e) { InkCanvas.CutSelection(); }
// since inheritance of Window does not work, the eventhandler have to be static methods and added after generation of the Window object // keyboard is pressed private static void InkCanvas_KeyDown(object sender, KeyEventArgs e) { Key Taste = e.Key; // the sender object is the object that generated the event InkCanvas objInkCanvas = (InkCanvas)sender; if (Keyboard.IsKeyDown(Key.RightCtrl) || Keyboard.IsKeyDown(Key.LeftCtrl)) // the following line only works with .Net 4.x // if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) { // if Ctrl is pressed switch (Taste) { case Key.C: // copy marked area objInkCanvas.CopySelection(); break; case Key.O: // open ink drawing Microsoft.Win32.OpenFileDialog objOpenDialog = new Microsoft.Win32.OpenFileDialog(); objOpenDialog.Filter = "isf files (*.isf)|*.isf"; if ((bool)objOpenDialog.ShowDialog()) { FileStream objFileStream = new FileStream(objOpenDialog.FileName, FileMode.Open); objInkCanvas.Strokes.Add(new StrokeCollection(objFileStream)); objFileStream.Dispose(); } break; case Key.P: // save grafic as PNG file Microsoft.Win32.SaveFileDialog objPNGDialog = new Microsoft.Win32.SaveFileDialog(); objPNGDialog.Filter = "png files (*.png)|*.png"; if ((bool)objPNGDialog.ShowDialog()) { FileStream objFileStream = new FileStream(objPNGDialog.FileName, FileMode.Create); System.Windows.Media.Imaging.RenderTargetBitmap objRenderBitmap = new System.Windows.Media.Imaging.RenderTargetBitmap((int)objInkCanvas.ActualWidth, (int)objInkCanvas.ActualHeight, 96.0, 96.0, System.Windows.Media.PixelFormats.Default); objRenderBitmap.Render(objInkCanvas); System.Windows.Media.Imaging.BitmapFrame objBitmapFrame = System.Windows.Media.Imaging.BitmapFrame.Create(objRenderBitmap); System.Windows.Media.Imaging.PngBitmapEncoder objImgEncoder = new System.Windows.Media.Imaging.PngBitmapEncoder(); // alternative for JPG: System.Windows.Media.Imaging.JpegBitmapEncoder objImgEncoder = new System.Windows.Media.Imaging.JpegBitmapEncoder(); objImgEncoder.Frames.Add(objBitmapFrame); objImgEncoder.Save(objFileStream); objFileStream.Dispose(); } break; case Key.S: // save ink drawing Microsoft.Win32.SaveFileDialog objSaveDialog = new Microsoft.Win32.SaveFileDialog(); objSaveDialog.Filter = "isf files (*.isf)|*.isf"; if ((bool)objSaveDialog.ShowDialog()) { FileStream objFileStream = new FileStream(objSaveDialog.FileName, FileMode.Create); objInkCanvas.Strokes.Save(objFileStream); objFileStream.Dispose(); } break; case Key.V: // paste marked area objInkCanvas.Paste(); break; case Key.X: // cut marked area objInkCanvas.CutSelection(); break; } } else { // no Ctrl key is pressed if (Keyboard.Modifiers == ModifierKeys.None) { // only when no other modifier keys are pressed switch (Taste) { case Key.B: // next background color Brush ActualBackColor = (Brush)BrushQueue.Dequeue(); BrushQueue.Enqueue(ActualBackColor); objInkCanvas.Background = ActualBackColor; break; case Key.C: // clear window content objInkCanvas.Strokes.Clear(); break; case Key.D: // switch to draw mode if (objInkCanvas.DefaultDrawingAttributes.IsHighlighter) { StoreHighLightSizeWidth = objInkCanvas.DefaultDrawingAttributes.Width; StoreHighLightSizeHeight = objInkCanvas.DefaultDrawingAttributes.Height; StoreHighLightColor = objInkCanvas.DefaultDrawingAttributes.Color; objInkCanvas.DefaultDrawingAttributes.StylusTip = StylusTip.Ellipse; objInkCanvas.DefaultDrawingAttributes.IsHighlighter = false; objInkCanvas.DefaultDrawingAttributes.Color = StoreInkColor; objInkCanvas.DefaultDrawingAttributes.Height = StoreInkSizeHeight; objInkCanvas.DefaultDrawingAttributes.Width = StoreInkSizeWidth; } objInkCanvas.EditingMode = InkCanvasEditingMode.Ink; break; case Key.E: // // switch to erase mode (and toggle it) switch (objInkCanvas.EditingMode) { case InkCanvasEditingMode.EraseByStroke: objInkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break; case InkCanvasEditingMode.EraseByPoint: objInkCanvas.EditingMode = InkCanvasEditingMode.EraseByStroke; break; case InkCanvasEditingMode.Ink: objInkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; break; } break; case Key.H: // switch to highlight mode if (!objInkCanvas.DefaultDrawingAttributes.IsHighlighter) { StoreInkSizeWidth = objInkCanvas.DefaultDrawingAttributes.Width; StoreInkSizeHeight = objInkCanvas.DefaultDrawingAttributes.Height; StoreInkColor = objInkCanvas.DefaultDrawingAttributes.Color; objInkCanvas.DefaultDrawingAttributes.Color = StoreHighLightColor; } objInkCanvas.EditingMode = InkCanvasEditingMode.Ink; objInkCanvas.DefaultDrawingAttributes.IsHighlighter = true; objInkCanvas.DefaultDrawingAttributes.StylusTip = StylusTip.Rectangle; if (StoreHighLightSizeWidth > 0.0) { objInkCanvas.DefaultDrawingAttributes.Height = StoreHighLightSizeHeight; objInkCanvas.DefaultDrawingAttributes.Width = StoreHighLightSizeWidth; } break; case Key.N: // next foreground color Color ActualFrontColor = (Color)ColorQueue.Dequeue(); ColorQueue.Enqueue(ActualFrontColor); objInkCanvas.DefaultDrawingAttributes.Color = ActualFrontColor; break; case Key.Q: // close window // event is handled now e.Handled = true; // parent object is Window ((Window)(objInkCanvas).Parent).Close(); break; case Key.S: // start marking objInkCanvas.Select(new System.Windows.Ink.StrokeCollection()); break; case Key.OemMinus: // shrink brush switch (objInkCanvas.EditingMode) { case InkCanvasEditingMode.EraseByPoint: if (objInkCanvas.EraserShape.Width > 3.0) { objInkCanvas.EraserShape = new RectangleStylusShape(objInkCanvas.EraserShape.Width - 2.0, objInkCanvas.EraserShape.Height - 2.0); // size change needs refresh to display objInkCanvas.EditingMode = InkCanvasEditingMode.None; objInkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; } break; case InkCanvasEditingMode.Ink: if (objInkCanvas.DefaultDrawingAttributes.Height > 3.0) { objInkCanvas.DefaultDrawingAttributes.Height = objInkCanvas.DefaultDrawingAttributes.Height - 2.0; objInkCanvas.DefaultDrawingAttributes.Width = objInkCanvas.DefaultDrawingAttributes.Width - 2.0; } break; } break; case Key.OemPlus: // enlarge brush switch (objInkCanvas.EditingMode) { case InkCanvasEditingMode.EraseByPoint: if (objInkCanvas.EraserShape.Width < 50.0) { objInkCanvas.EraserShape = new RectangleStylusShape(objInkCanvas.EraserShape.Width + 2.0, objInkCanvas.EraserShape.Height + 2.0); // size change needs refresh to display objInkCanvas.EditingMode = InkCanvasEditingMode.None; objInkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint; } break; case InkCanvasEditingMode.Ink: if (objInkCanvas.DefaultDrawingAttributes.Height < 50.0) { objInkCanvas.DefaultDrawingAttributes.Height = objInkCanvas.DefaultDrawingAttributes.Height + 2.0; objInkCanvas.DefaultDrawingAttributes.Width = objInkCanvas.DefaultDrawingAttributes.Width + 2.0; } break; } break; } } } }
internal void Cut() { _currentInkCanvas.CutSelection(); }