Beispiel #1
0
        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;
        }
Beispiel #2
0
        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;
            }
        }