private void RemoveGradientStop(GradientThumb toRemoveThumb) { if (toRemoveThumb == null || !(toRemoveThumb.Tag is double) || !(toRemoveThumb.Parent is Canvas)) { return; } var offsetTag = (double)toRemoveThumb.Tag; var linebrush = GradientBorder.Background as LinearGradientBrush; var removeTag = linebrush.GradientStops.FirstOrDefault(x => x.Offset == offsetTag); if (removeTag != null) { linebrush.GradientStops.Remove(removeTag); } var canvas = toRemoveThumb.Parent as Canvas; //canvas.Children.Remove(toRemoveThumb); toRemoveThumb.Opacity = 0d; var rtnStyle = new StyleColor(); rtnStyle.FillType = ColorFillType.Gradient; rtnStyle.Frames = new Dictionary <double, int>(); foreach (var gs in linebrush.GradientStops) { rtnStyle.Frames.Add(gs.Offset, gs.Color.ToArgb()); } CurrentStyle = rtnStyle; }
protected virtual void OnInitialColorChanged(StyleColor oldInitialColor, StyleColor newInitialColor) { if (!(GradientBorder.Background is LinearGradientBrush)) { return; } var linebrush = GradientBorder.Background as LinearGradientBrush; linebrush.GradientStops.Clear(); var newFrames = newInitialColor.Frames; if (newFrames == null || newFrames.Count == 0) { newFrames = new Dictionary <double, int>(); newFrames[0] = -1; newFrames[1] = -16777216; } var parentCanvas = this.canvas; parentCanvas.Children.Clear(); var actualWidth = parentCanvas.ActualWidth; foreach (var keypair in newFrames) { var gradientThumb = new GradientThumb { Template = this.Resources["GradientThumbTemplate"] as ControlTemplate, Tag = keypair.Key }; gradientThumb.DragDelta += gradientThumb_DragDelta; gradientThumb.MouseClicked += gradientThumb_MouseDown; gradientThumb.ThumbRemoving += gradientThumb_ThumbRemoving; gradientThumb.ThumbRecovering += gradientThumb_ThumbRecovering; gradientThumb.ThumbRemoved += gradientThumb_ThumbRemoved; Canvas.SetLeft(gradientThumb, keypair.Key * actualWidth); parentCanvas.Children.Add(gradientThumb); var drawingColor = System.Drawing.Color.FromArgb(keypair.Value); gradientThumb.SetColor(Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B)); var gradientStop = new GradientStop( Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B), keypair.Key); linebrush.GradientStops.Add(gradientStop); } this.selectedThumb = parentCanvas.Children[0] as GradientThumb; this.selectedThumb.SetSelected(true); this.SelectedColor = linebrush.GradientStops[0].Color; CurrentStyle = newInitialColor; }
private void SetThumbSeletected(GradientThumb sthumb) { var parentCanvas = this.canvas; foreach (var thumb in parentCanvas.Children.OfType <GradientThumb>()) { if (thumb == sthumb) { thumb.SetSelected(true); this.selectedThumb = thumb; this.SelectedColor = thumb.GetColor(); } else { thumb.SetSelected(false); } } }
void gradientThumb_ThumbRemoved(object sender, EventArgs e) { var thumb = sender as GradientThumb; if (thumb == null || !(thumb.Parent is Canvas)) { return; } var canvas = thumb.Parent as Canvas; canvas.Children.Remove(thumb); var toSelectedThumb = canvas.Children.OfType <GradientThumb>().OrderBy(x => x.Tag is double?(double)x.Tag: double.MaxValue).FirstOrDefault(); if (toSelectedThumb != null) { this.selectedThumb = toSelectedThumb; this.selectedThumb.SetSelected(true); this.SelectedColor = this.selectedThumb.GetColor(); } }
private void CanvasMouseDown(object sender, MouseButtonEventArgs e) { ///暂时屏蔽多点渐变色 if (e.LeftButton == MouseButtonState.Pressed && e.RightButton == MouseButtonState.Released && sender is Canvas) { var canvas = sender as Canvas; var mouseposition = e.GetPosition(canvas); var newOffset = Math.Round(mouseposition.X / canvas.ActualWidth, 2); var linebrush = GradientBorder.Background as LinearGradientBrush; if (linebrush.GradientStops.Any(x => Math.Abs(x.Offset - newOffset) <= 0.05)) { return; } var gradientThumb = new GradientThumb { Template = this.Resources["GradientThumbTemplate"] as ControlTemplate, Tag = newOffset }; gradientThumb.DragDelta += gradientThumb_DragDelta; gradientThumb.MouseClicked += gradientThumb_MouseDown; gradientThumb.ThumbRemoving += gradientThumb_ThumbRemoving; gradientThumb.ThumbRecovering += gradientThumb_ThumbRecovering; gradientThumb.ThumbRemoved += gradientThumb_ThumbRemoved; Canvas.SetLeft(gradientThumb, newOffset * canvas.ActualWidth); canvas.Children.Add(gradientThumb); var relativeposition = new System.Windows.Point( newOffset * GradientBorder.ActualWidth, GradientBorder.ActualHeight / 2); var screenposition = GradientBorder.PointToScreen(relativeposition); var drawingColor = ColorPickerManager.GetColor((int)screenposition.X, (int)screenposition.Y); gradientThumb.SetColor(Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B)); var gradientStop = new GradientStop( Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B), newOffset); linebrush.GradientStops.Add(gradientStop); foreach (var thumb in canvas.Children.OfType <GradientThumb>()) { if (thumb == gradientThumb) { thumb.SetSelected(true); this.selectedThumb = thumb; } else { thumb.SetSelected(false); } } this.selectedThumb.SetSelected(true); this.SelectedColor = drawingColor; var rtnStyle = new StyleColor(); rtnStyle.FillType = ColorFillType.Gradient; rtnStyle.Frames = new Dictionary <double, int>(); foreach (var gs in linebrush.GradientStops) { rtnStyle.Frames.Add(gs.Offset, gs.Color.ToArgb()); } CurrentStyle = rtnStyle; } }