Пример #1
0
        private void MainCanvas_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                return;
            }
            if (!(e.Source != MainCanvas && e.Source.GetType() == typeof(ItemLayer) && controlSelector.itemChoose == (ItemLayer)e.Source))
            {
                for (int i = 0; i < MainCanvas.Children.Count; ++i)
                {
                    if (!(MainCanvas.Children[i] is ItemLayer))
                    {
                        continue;
                    }
                    ItemLayer item = (ItemLayer)MainCanvas.Children[i];
                    if (item.Selected == true)
                    {
                        item.Selected = false;
                        SaveHistory();
                    }
                    //Canvas.SetZIndex(item, 0);
                }
                controlSelector.itemChoose = null;
                //SaveCanvas();
                for (int i = 0; i < MainCanvas.Children.Count; ++i)
                {
                    if (!(MainCanvas.Children[i] is ItemLayer))
                    {
                        continue;
                    }
                    ItemLayer item = (ItemLayer)MainCanvas.Children[i];
                    MainCanvas.Children.Remove((ItemLayer)MainCanvas.Children[i]);
                }
            }
            if (TypeDraw == TypeShape.Eraser)
            {
                isEraser = true;
                Point        p   = Mouse.GetPosition(MainCanvas);
                BitmapSource bmp = (BitmapSource)MainCanvasImage.Source;

                if (bmp == null)
                {
                    return;
                }
                MyBitmap = new WriteableBitmap(bmp);
                MainCanvasImage.Source = MyBitmap;
                ImageProcess.Rubber((int)p.X, (int)p.Y, penWidth, MyBitmap);
            }
            if (TypeDraw == TypeShape.Fill)
            {
                System.Drawing.Color color = System.Drawing.Color.FromArgb(fillColor.A, fillColor.R, fillColor.G, fillColor.B);
                Point p = Mouse.GetPosition(MainCanvas);
                ImageProcess.FloodFill(MainCanvas, MainCanvasImage, p, color);
                SaveHistory();
                return;
            }
        }
Пример #2
0
        private void MainCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
            {
                if (!IsMouseDown)
                {
                    return;
                }
                ///MainCanvainitial = Mouse.GetPosition(this);
                //xOffset = //Mouse.GetPosition(this).X - initial.X;
                //yOffset = //Mouse.GetPosition(this).Y - initial.Y;
                //MainCanvas.RenderTransform =
                Translate = new TranslateTransform
                {
                    X = xOffset + Mouse.GetPosition(this).X - initial.X,
                    Y = yOffset + Mouse.GetPosition(this).Y - initial.Y
                };
                TransformGroup group = new TransformGroup();
                group.Children.Add(Translate);
                group.Children.Add(Transform);
                MainCanvas.RenderTransform = group;
                return;
            }
            if (isEraser == true && TypeDraw == TypeShape.Eraser)
            {
                Point        point = Mouse.GetPosition(MainCanvas);
                BitmapSource bmp   = (BitmapSource)MainCanvasImage.Source;

                if (bmp == null)
                {
                    return;
                }

                MyBitmap = new WriteableBitmap(bmp);
                MainCanvasImage.Source = MyBitmap;
                ImageProcess.Rubber((int)point.X, (int)point.Y, penWidth, MyBitmap);
            }
            if (TypeDraw == TypeShape.PolyLine)
            {
                if (controlSelector.itemChoose != null)
                {
                    // Mouse.GetPosition(MainCanvas);
                    Point CurPoint = Mouse.GetPosition(MainCanvas);
                    CurPoint = MainCanvas.PointToScreen(CurPoint);
                    CurPoint = ((PolyLineObject)controlSelector.itemChoose.objectBase).rect.PointFromScreen(CurPoint);
                    ((PolyLineObject)controlSelector.itemChoose.objectBase).rect.Points.Add(CurPoint);
                }
                return;
            }
            if (TypeDraw == TypeShape.PolyGon)
            {
                if (controlSelector.itemChoose != null)
                {
                    // Mouse.GetPosition(MainCanvas);
                    Point CurPoint = Mouse.GetPosition(MainCanvas);
                    CurPoint = MainCanvas.PointToScreen(CurPoint);
                    CurPoint = ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.PointFromScreen(CurPoint);
                    PointCollection list = ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.Points;
                    ((PolyGonObject)controlSelector.itemChoose.objectBase).rect.Points[list.Count - 1] = CurPoint;
                }
            }
            controlSelector.OnMouse_Move();
            //Set thông tin statusbar
            Point p = Mouse.GetPosition(MainCanvas);

            lblCursorPosition.Text = "X: " + ((int)p.X).ToString() + ", Y: " + ((int)p.Y).ToString();
        }