Пример #1
0
        //MOUSE HANDLE
        private void pictureBox_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            drawMouse = true;
            x         = e.X;
            y         = e.Y;



            if (currentItem == item.Pen)
            {
                Console.WriteLine("x" + x + "y" + y);
                Console.WriteLine("x1" + x1 + "y1" + y1);
                Pen p = choosePen(e);



                // khởi tạo list tất cả các điểm để vẽ pen
                pointList = new List <Point>();
                pointList.Add(new Point(x, y)); // add điểm đầu tiên
                g.DrawLine(p, x, y, x + 1, y);

                pictureBox.Image = drawing;
            }
            if (currentItem == item.Eraser)
            {
                pointList = new List <Point>();
                SolidBrush brush = new SolidBrush(Color.White);

                rectangleList = new List <Rectangle>();
                rectangleList.Add(new Rectangle(new Point(x, y), new Size(currentWidth, currentWidth)));
                g.FillRectangles(brush, rectangleList.ToArray());

                pictureBox.Image = drawing;
            }
            if (currentItem == item.Select)
            {
                //kích chuột trong vùng đã chọn->kéo thả
                if (x >= selectedArea.X && x <= selectedArea.X + selectedArea.Width &&
                    y >= selectedArea.Y && y <= selectedArea.Y + selectedArea.Height)
                {
                    Console.Write("drag" + selectedArea.X + "   " + selectedArea.Y);
                    dragNdrop = true;
                }
                else //kích chuột ngoài
                {
                    //reset selectedArea-> khi click chuột ngoài rồi click vào vùng chọn không kéo thả được nữa
                    selectedArea.Width  = 0;
                    selectedArea.Height = 0;
                    //xoá vùng chọn và không cho cut copy
                    drawing = new Bitmap(drawingList.ElementAt(drawingList.currentIndex));
                    //pictureBox.Image = drawingList.Last();
                    g = Graphics.FromImage(drawing);
                    pictureBox.Image = drawing;
                    cutItem.Enabled  = false;
                    copyItem.Enabled = false;
                }
            }
        }
Пример #2
0
        private void pictureBox_MouseMove(Object sender, MouseEventArgs e)
        {
            if (dragNdrop)
            {
                this.pictureBox.Cursor = Cursors.SizeAll;
            }
            else
            {
                this.pictureBox.Cursor = Cursors.Cross;
            }

            if (drawMouse == false)
            {
                goto jump;// k phải đang dí chuột thì nhảy tới jump
            }
            x1 = e.X;
            y1 = e.Y;

            //g.Clear(Color.White);
            drawing = new Bitmap(drawingList.ElementAt(drawingList.currentIndex));
            //pictureBox.Image = drawingList.Last();
            g = Graphics.FromImage(drawing);

            //chọn pen
            Pen pen = choosePen(e);

            if (currentFillItem != fillType.NoFill)
            //vẽ phía trong
            {
                Brush brush = chooseBrush(e);
                brushShape(brush, x, y, x1, y1);
            }

            //Vẽ đường ngoài
            switch (currentItem)
            {
            case item.Select:
            {
                if (dragNdrop)
                {
                    Console.WriteLine("Selecting");
                    //this.pictureBox.Cursor = Cursors.SizeAll;


                    //tạo nét đứt xung quanh
                    //dashDraw(new Rectangle(selectedArea.X+ (x1 - x), selectedArea.Y + (y1 - y),selectedArea.Width-1,selectedArea.Height));
                    //xoá hình select trên hình hiện tại
                    Bitmap   nullImage    = new Bitmap(selectImage);
                    Graphics nullGraphics = Graphics.FromImage(nullImage);
                    nullGraphics.Clear(Color.White);
                    g.DrawImageUnscaled(nullImage, selectedArea.X, selectedArea.Y);

                    //vẽ hình select lên vị trí mới
                    g.DrawImageUnscaled(selectImage, selectedArea.X + (x1 - x), selectedArea.Y + (y1 - y));
                    //pictureBox.Image = drawing;
                }
                else
                {
                    dashDraw(Rectangle1(x, y, x1, y1));
                }
                break;
            }

            case item.Pen:
            {
                //Pen pen = pen_properties(currentColor, currentWidth);

                //g.DrawLine(p, x, y, x1, y1);
                //x = x1; y = y1;
                pointList.Add(new Point(x1, y1));

                try
                {
                    g.DrawLines(pen, pointList.ToArray());
                    //g.DrawCurve(pen, pointList.ToArray());
                    ////g.DrawBeziers(pen, pointList.ToArray());
                }
                catch (Exception ex)
                {
                }

                break;
            }

            case item.Eraser:
            {
                SolidBrush brush = new SolidBrush(Color.White);
                rectangleList.Add(new Rectangle(new Point(x1, y1), new Size(currentWidth, currentWidth)));
                g.FillRectangles(brush, rectangleList.ToArray());
                break;
            }

            case item.Line:
            {
                g.DrawLine(pen, new Point(x, y), new Point(x1, y1));
                //pictureBox1.CreateGraphics().DrawImageUnscaled(drawing, 0, 0);
                break;
            }

            case item.Oval:
            {
                g.DrawEllipse(pen, x, y, x1 - x, y1 - y);
                break;
            }

            case item.Rectangle:
            {
                g.DrawRectangle(pen, Rectangle1(x, y, x1, y1));
                break;
            }

            case item.TriCan:
            {
                Point[] pList = new Point[3];
                pList[0] = new Point((x + x1) / 2, y);
                pList[1] = new Point(x, y1);
                pList[2] = new Point(x1, y1);
                g.DrawPolygon(pen, pList);
                break;
            }

            case item.TriVuong:
            {
                Point[] pList = new Point[3];
                pList[0] = new Point(x, y);
                pList[1] = new Point(x, y1);
                pList[2] = new Point(x1, y1);
                g.DrawPolygon(pen, pList);
                break;
            }

            case item.Diamond:
            {
                Point[] pList = new Point[4];
                pList[0] = new Point((x + x1) / 2, y);
                pList[1] = new Point(x, (y + y1) / 2);
                pList[2] = new Point((x + x1) / 2, y1);
                pList[3] = new Point(x1, (y + y1) / 2);
                g.DrawPolygon(pen, pList);
                break;
            }
            }
            pictureBox.Image = drawing; // hiển thị
            jump :;
        }