public void Paste(InkCanvas inkCanvas, InkCanvas clipboardCanvas) { if (!ContainsImage()) { EventBroker.Instance.ActionResult(Resources.Title_Paste, Resources.Message_Clipboard_does_not_contain_any_image_as_expected); return; } if (ClipboardWasChangedOutside()) { var image = ImageHelper.AddImageTo(inkCanvas, new Bitmap(GetImage())); ActivateSelectionMode(inkCanvas); inkCanvas.Select(new List <UIElement> { image }); return; } _currentPasteOffset += PasteOffset; ClearSelection(inkCanvas); var elements = new List <UIElement>(); var strokes = new StrokeCollection(); WPFHelper.CloneElementsTo(elements, clipboardCanvas.Children.GetEnumerator(), inkCanvas, _currentPasteOffset); WPFHelper.CloneStrokesTo(strokes, clipboardCanvas.Strokes, _currentPasteOffset); elements.ForEach(element => inkCanvas.Children.Add(element)); inkCanvas.Strokes.Add(strokes); ActivateSelectionMode(inkCanvas); inkCanvas.Select(strokes, elements); }
/// <summary> /// Removes the old collection, inserts the newer one. /// </summary> /// <param name="old"></param> /// <param name="newer"></param> /// <returns></returns> private void replace(StrokeCollection old, StrokeCollection newer) { if (old != null) { inkSketch.DeleteStrokes(old); } if (newer != null) { inkSketch.AddStrokes(newer); inkCanvas.Select(newer); } }
public void CopyInkCanvasObjects(InkCanvas inkCanvas, InkCanvas clipboardCanvas) { var selectionBounds = inkCanvas.GetSelectionBounds(); if (selectionBounds.IsEmpty) { return; } Clear(clipboardCanvas); var selectedElements = inkCanvas.GetSelectedElements().ToList(); var selectedStrokes = new StrokeCollection(inkCanvas.GetSelectedStrokes()); ClearSelection(inkCanvas); var elements = new List <UIElement>(); WPFHelper.CloneElementsTo(elements, selectedElements.GetEnumerator(), clipboardCanvas); WPFHelper.CloneStrokesTo(clipboardCanvas.Strokes, selectedStrokes); elements.ForEach(element => clipboardCanvas.Children.Add(element)); inkCanvas.Select(selectedStrokes, selectedElements); var dataObject = new DataObject(); dataObject.SetData(ClipboardIdFormat, _clipboardStateId = Guid.NewGuid()); dataObject.SetData(DataFormats.Bitmap, ImageHelper.GetCroppedImageFromInkCanvas(selectionBounds, inkCanvas)); Clipboard.SetDataObject(dataObject); }
/// <summary> /// Sélectionne la Textbox dans l'InkCanvas /// </summary> /// <param name="window"></param> /// <param name="ink"></param> public override void selectInCanvas(MainWindow window, InkCanvas ink) { List <UIElement> uIElements = new List <UIElement> { this.BoxT }; ink.Select(null, uIElements); }
/// <summary> /// Sélectionne la Stroke dans l'InkCanvas /// </summary> /// <param name="window"></param> /// <param name="ink"></param> public override void selectInCanvas(MainWindow window, InkCanvas ink) { StrokeCollection strokeCollection = new StrokeCollection(new List <Stroke> { this.Strokeat }); ink.Select(strokeCollection, null); }
/// <summary> /// Unmagnifies the sketch by a preset amount and notifies listerners. /// </summary> public void ZoomOut(int pixels) { StrokeCollection oldSelection = InkCanvas.GetSelectedStrokes(); SelectAllStrokes(); System.Windows.Rect oldBounds = InkCanvas.GetSelectionBounds(); System.Windows.Rect smallerBounds = new System.Windows.Rect(oldBounds.TopLeft, new System.Windows.Size(oldBounds.Width * SketchPanelConstants.ZoomOutFactor, oldBounds.Height * SketchPanelConstants.ZoomOutFactor)); CommandList.MoveResizeCmd resize = new CommandList.MoveResizeCmd(ref inkSketch, oldBounds, smallerBounds); CM.ExecuteCommand(resize); InkCanvas.Select(oldSelection); InkChanged(false); }
public void SelectCanvas(int canvasIndex) { InkCanvas oldInkCanvas = _currentInkCanvas; _currentInkCanvas = (InkCanvas)_canvasList.Children[canvasIndex]; //copy images & strokes if (oldInkCanvas != null) { foreach (Image uIElement in oldInkCanvas.GetSelectedElements().OfType <Image>()) { oldInkCanvas.Children.Remove(uIElement); _currentInkCanvas.Children.Add(uIElement); } foreach (Stroke stroke in oldInkCanvas.GetSelectedStrokes()) { if (Keyboard.IsKeyDown(Key.LeftShift)) { _currentInkCanvas.Strokes.Add(stroke.Clone()); } if (Keyboard.IsKeyDown(Key.LeftAlt)) { oldInkCanvas.Strokes.Remove(stroke); _currentInkCanvas.Strokes.Add(stroke); } } } //hide all canvases foreach (InkCanvas inkCanvas in _canvasList.Children) { inkCanvas.Opacity = .2; inkCanvas.IsHitTestVisible = false; } _currentInkCanvas.IsHitTestVisible = true; _currentInkCanvas.Opacity = 1; //SetMode(InkCanvasEditingMode.Select, CustomMode.select); _currentInkCanvas.Select(new StrokeCollection()); if (null != oldInkCanvas) { oldInkCanvas.Select(new StrokeCollection()); } }
private void Stop(InkCanvas inkCanvas) { if (_overlayStroke != null && inkCanvas.Strokes.Contains(_overlayStroke)) { inkCanvas.Strokes.Remove(_overlayStroke); } foreach (var repetition in _repetitions) { _selectedElements.AddRange(repetition.Obj1); _selectedStrokes.Add(repetition.Obj2); } inkCanvas.Select(_selectedStrokes, _selectedElements); if (_repetitions.Count > 0) { ModelStorage.WorkAreaModel.GroupSelectedDrawings(); } ProcessRepeateSelectionState = RepeateSelectionState.Inactive; ClearRepetitionCollections(); }
//</Snippet3> void InkCanvas_Drop(object sender, DragEventArgs e) { // Get the strokes that were moved. InkCanvas ic = (InkCanvas)sender; MemoryStream ms = (MemoryStream)e.Data.GetData( StrokeCollection.InkSerializedFormat); ms.Position = 0; StrokeCollection strokes = new StrokeCollection(ms); // Translate the strokes to the position at which // they were dropped. Point pt = e.GetPosition(ic); TranslateStrokes(strokes, pt.X, pt.Y); // Add the strokes to the InkCanvas and keep them selected. ic.Strokes.Add(strokes); ic.Select(strokes); }
void InkCanvas1SelectionChanged(object sender, EventArgs e) { InkCanvas _InkCanvas = (InkCanvas)sender; if (Keyboard.IsKeyDown(Key.LeftCtrl)) { if (_oldStrokeCollection != null) { foreach (Stroke _Stroke in _InkCanvas.GetSelectedStrokes()) { if (!_oldStrokeCollection.Contains(_Stroke)) { _oldStrokeCollection.Add(_Stroke); } } _InkCanvas.Select(_oldStrokeCollection); } } _oldStrokeCollection = _InkCanvas.GetSelectedStrokes(); }
public void SelectCanvas(int i) { InkCanvas _oldInkCanvas = _InkCanvas; _InkCanvas = (InkCanvas)_CanvasList.Children[i]; if (_oldInkCanvas != null) { foreach (Image _UIElement in _oldInkCanvas.GetSelectedElements().OfType <Image>().ToList()) { _oldInkCanvas.Children.Remove(_UIElement); _InkCanvas.Children.Add(_UIElement); } foreach (Stroke _Stroke in _oldInkCanvas.GetSelectedStrokes()) { if (Keyboard.IsKeyDown(Key.LeftShift)) { _InkCanvas.Strokes.Add(_Stroke.Clone()); } if (Keyboard.IsKeyDown(Key.LeftAlt)) { _oldInkCanvas.Strokes.Remove(_Stroke); _InkCanvas.Strokes.Add(_Stroke); } } } foreach (InkCanvas _InkCanvas1 in _CanvasList.Children) { _InkCanvas1.Opacity = .2; _InkCanvas1.IsHitTestVisible = false; } _InkCanvas.IsHitTestVisible = true; _InkCanvas.Opacity = 1; //SetMode(InkCanvasEditingMode.Select, CustomMode.select); _InkCanvas.Select(new StrokeCollection()); if (null != _oldInkCanvas) { _oldInkCanvas.Select(new StrokeCollection()); } }
/// <summary> /// Update Selected Strokes /// </summary> private void UpdateSelection() { Rect rubberBand = new Rect(startPoint.Value, endPoint.Value); StrokeCollection lstSelectedStrokes = new StrokeCollection(); foreach (var item in inkCanvas.Strokes) { Rect itemRect = item.GetBounds(); if (rubberBand.Contains(itemRect)) { lstSelectedStrokes.Add(item); } } inkCanvas.Select(lstSelectedStrokes); if (lstSelectedStrokes.Count == 0) { inkCanvas.EditingMode = InkCanvasEditingMode.None; } }
public static void SelectionChanged(InkCanvas inkCanvas, ICollection <StrokeCollection> groupedStrokesCollections) { lock (SelectionChangedInProgress) { if (SelectionChangedInProgress.Contains(inkCanvas)) { return; } SelectionChangedInProgress.Add(inkCanvas); } try { var selectedStrokes = inkCanvas.GetSelectedStrokes(); var uniqueStrokesCollection = new HashSet <Stroke>(selectedStrokes); foreach (var groupedStrokesCollection in groupedStrokesCollections) { if (!groupedStrokesCollection.Any(uniqueStrokesCollection.Contains)) { continue; } foreach (var stroke in groupedStrokesCollection) { uniqueStrokesCollection.Add(stroke); } } inkCanvas.Select(new StrokeCollection(uniqueStrokesCollection), inkCanvas.GetSelectedElements()); } finally { lock (SelectionChangedInProgress) { if (SelectionChangedInProgress.Contains(inkCanvas)) { SelectionChangedInProgress.Remove(inkCanvas); } } } }
// 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; } } } }
public override void DrawCommentConnectorLine() { InkCanvas parentCanvas = this.Parent as InkCanvas; if (this._commentNote == null) { return; } else if (this._commentNote.Visibility == Visibility.Collapsed) { if (parentCanvas.Strokes.Contains(this.CommentConnectorStroke)) { parentCanvas.Strokes.Remove(this.CommentConnectorStroke); } parentCanvas.Select((IEnumerable <UIElement>)null); parentCanvas.Children.Remove(this._commentNote); //InkCanvasEditingMode ie = parentCanvas.EditingMode; //parentCanvas.EditingMode = InkCanvasEditingMode.None; //parentCanvas.EditingMode = ie; return; } if (this.CommentConnectorStroke != null) { if (parentCanvas.Strokes.Contains(this.CommentConnectorStroke)) { parentCanvas.Strokes.Remove(this.CommentConnectorStroke); } } if (this.CommentNote.Visibility == Visibility.Visible) { StylusPointCollection spCollection = new StylusPointCollection(); //Start center-below entity spCollection.Add(new StylusPoint( this.GetBounds(0).Left + (this.ActualWidth * 0.5), this.GetBounds(2).Bottom + 2)); //Go to 10p below start spCollection.Add(new StylusPoint( this.GetBounds(0).Left + (this.ActualWidth * 0.5), this.GetBounds(2).Bottom + 10)); //Go left/right to the center of dist. between comment and entity if ((double)this.CommentNote.GetValue(InkCanvas.LeftProperty) > spCollection[0].X) { double xDiff = ((double)this.CommentNote.GetValue(InkCanvas.LeftProperty) - this.GetBounds(0).Right) * 0.5; spCollection.Add(new StylusPoint( this.GetBounds(0).Right + xDiff, this.GetBounds(2).Bottom + 10)); } else { double xDiff = (this.GetBounds(0).Left - ((double)this.CommentNote.GetValue(InkCanvas.LeftProperty) + this.CommentNote.ActualWidth)) * 0.5; spCollection.Add(new StylusPoint( this.GetBounds(0).Left - xDiff, this.GetBounds(2).Bottom + 10)); } //Go up/down to the center of diff between comment and entity spCollection.Add(new StylusPoint( spCollection[2].X, (double)this.CommentNote.GetValue(InkCanvas.TopProperty) - 10)); //Go to the top of the comment note spCollection.Add(new StylusPoint( (double)this.CommentNote.GetValue(InkCanvas.LeftProperty) + (this.CommentNote.ActualWidth * 0.5), (double)this.CommentNote.GetValue(InkCanvas.TopProperty) - 10)); //End center-on top of comment. spCollection.Add(new StylusPoint( (double)this.CommentNote.GetValue(InkCanvas.LeftProperty) + (this.CommentNote.ActualWidth * 0.5), (double)this.CommentNote.GetValue(InkCanvas.TopProperty) - 2)); this.CommentConnectorStroke = new Stroke(spCollection); parentCanvas.Strokes.Add(this.CommentConnectorStroke); } else { AdornerLayer al = AdornerLayer.GetAdornerLayer(parentCanvas); al.Visibility = Visibility.Collapsed; } }
public static void ClearSelection(InkCanvas inkCanvas) { inkCanvas.Select(new StrokeCollection()); }
/// <summary> /// Selects all strokes in the sketch /// </summary> public void SelectAllStrokes() { inkCanvas.Select(inkCanvas.Strokes); }
/// <summary> /// OnDrop - called when drag and drop contents are dropped on an InkCanvas /// </summary> private void OnDrop(object sender, DragEventArgs e) { InkCanvas inkCanvas = (InkCanvas)sender; //see if ISF is present in the drag and drop IDataObject if (e.Data.GetDataPresent(StrokeCollection.InkSerializedFormat) || e.Data.GetDataPresent(DataFormats.Xaml)) { SetDragDropEffects(e); if (e.Effects == DragDropEffects.Move || e.Effects == DragDropEffects.Copy) { Point dropPosition = e.GetPosition(inkCanvas); //after we drop, we need to select the elements and strokes that were //copied into the inkCanvas List <UIElement> elementsToSelect = new List <UIElement>(); if (e.Data.GetDataPresent(DataFormats.Xaml)) { //paste Xaml string xamlData = (string)e.Data.GetData(DataFormats.Xaml); if (!String.IsNullOrEmpty(xamlData)) { UIElement element = XamlReader.Load(new XmlTextReader(new StringReader(xamlData))) as UIElement; if (element != null) { //check to see if this is an InkCanvas InkCanvas inkCanvasFromDragDrop = element as InkCanvas; if (inkCanvasFromDragDrop != null) { //we assume this was put on the data object by us. //remove the children and add them to the drop inkCanvas. while (inkCanvasFromDragDrop.Children.Count > 0) { UIElement childElement = inkCanvasFromDragDrop.Children[0]; double childLeft = InkCanvas.GetLeft(childElement); InkCanvas.SetLeft(childElement, childLeft + dropPosition.X); double childTop = InkCanvas.GetTop(childElement); InkCanvas.SetTop(childElement, childTop + dropPosition.Y); inkCanvasFromDragDrop.Children.Remove(childElement); inkCanvas.Children.Add(childElement); elementsToSelect.Add(childElement); } } else { //just add the element, it wasn't another InkCanvas that we //added to the dataobject as a container inkCanvas.Children.Add(element); elementsToSelect.Add(element); } } } } //now check to see if we have ISF as well StrokeCollection strokesToSelect = new StrokeCollection(); if (e.Data.GetDataPresent(StrokeCollection.InkSerializedFormat)) { //only ISF was present MemoryStream ms = (MemoryStream)e.Data.GetData(StrokeCollection.InkSerializedFormat); StrokeCollection sc = new StrokeCollection(ms); ms.Close(); //translate the strokes back from the origin to the current position Matrix translation = new Matrix(); translation.Translate(dropPosition.X, dropPosition.Y); sc.Transform(translation, false); //add the strokes from the IDataObject to the inkCanvas and select them. inkCanvas.Strokes.Add(sc); strokesToSelect.Add(sc); } //now that we're done, we select if (elementsToSelect.Count > 0 || strokesToSelect.Count > 0) { inkCanvas.Select(strokesToSelect, elementsToSelect); } e.Handled = true; } } }