예제 #1
0
파일: Form1.cs 프로젝트: Nguyen-Tai/Paint
        private void FindGroupRegion(Group group)
        {
            int minX = int.MaxValue, minY = int.MaxValue, maxX = int.MinValue, maxY = int.MinValue;

            for (int i = 0; i < group.Shapes.Count; i++)
            {
                ShapeObject shape = group.Shapes[i];
                if (shape is Polygon polygon)
                {
                    FindPolygonRegion(polygon);
                }


                if (shape.p1.X < minX)
                {
                    minX = shape.p1.X;
                }
                if (shape.p2.X < minX)
                {
                    minX = shape.p2.X;
                }

                if (shape.p1.Y < minY)
                {
                    minY = shape.p1.Y;
                }
                if (shape.p2.Y < minY)
                {
                    minY = shape.p2.Y;
                }

                if (shape.p1.X > maxX)
                {
                    maxX = shape.p1.X;
                }
                if (shape.p2.X > maxX)
                {
                    maxX = shape.p2.X;
                }

                if (shape.p1.Y > maxY)
                {
                    maxY = shape.p1.Y;
                }
                if (shape.p2.Y > maxY)
                {
                    maxY = shape.p2.Y;
                }
            }
            group.p1 = new Point(minX, minY);
            group.p2 = new Point(maxX, maxY);
        }
예제 #2
0
파일: Form1.cs 프로젝트: Nguyen-Tai/Paint
        private void PanelPaint_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                isMouseDown = false;
                if (isMovingShape)
                {
                    isMovingShape = false;
                    selectedShape = null;
                }
                else if (isResizeShape)
                {
                    isResizeShape = false;
                    resizeShape   = null;
                }
                else if (isMouseSelect)
                {
                    isMouseSelect = false;  // select
                    for (int i = 0; i < lstObject.Count; i++)
                    {
                        lstObject[i].IsSelected = false;

                        if (lstObject[i].p1.X >= selectedRegion.X && lstObject[i].p2.X <= selectedRegion.X + selectedRegion.Width &&
                            lstObject[i].p1.Y >= selectedRegion.Y && lstObject[i].p2.Y <= selectedRegion.Y + selectedRegion.Height)
                        {
                            lstObject[i].IsSelected = true;
                        }
                    }
                    PanelPaint.Invalidate();
                }
                if (currentShape != CurrentShape.Polygon && currentShape != CurrentShape.NoDrawing)
                {
                    lstObject[lstObject.Count - 1].p2 = e.Location;
                    PanelPaint.Invalidate();
                }
                ShapeObject shape = lstObject[lstObject.Count - 1];

                if (shape is Circle circle)
                {
                    if (circle.p2.X < circle.p1.X && circle.p2.Y < circle.p1.Y) // lên trái
                    {
                        circle.p2 = new Point(circle.p1.X - Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)), circle.p1.Y
                                              - Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)));
                    }
                    if (circle.p2.X > circle.p1.X && circle.p2.Y > circle.p1.Y) // xuống phải
                    {
                        circle.p2 = new Point(circle.p1.X + Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)), circle.p1.Y
                                              + Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)));
                    }
                    if (circle.p2.X > circle.p1.X && circle.p2.Y < circle.p1.Y) // lên phải
                    {
                        circle.p2 = new Point(circle.p1.X + Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)), circle.p1.Y
                                              - Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)));
                    }
                    if (circle.p2.X < circle.p1.X && circle.p2.Y > circle.p1.Y) // xuống trái
                    {
                        circle.p2 = new Point(circle.p1.X - Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)), circle.p1.Y
                                              + Math.Min(Math.Abs(circle.p2.X - circle.p1.X), Math.Abs(circle.p2.Y - circle.p1.Y)));
                    }
                }
            }
            catch
            {
            }
        }
예제 #3
0
파일: Form1.cs 프로젝트: Nguyen-Tai/Paint
        private void PanelPaint_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (currentShape == CurrentShape.NoDrawing) // select
                {
                    if (isControlKeyPress)                  // nhấn phím controls
                    {
                        for (int i = 0; i < lstObject.Count; i++)
                        {
                            if (lstObject[i].IsHit(e.Location))
                            {
                                lstObject[i].IsSelected = !lstObject[i].IsSelected;
                                PanelPaint.Invalidate();
                                break;
                            }
                        }
                    }
                    else
                    {
                        lstObject.ForEach(shape => shape.IsSelected = false);
                        lstObject.ForEach(shape => shape.IsResize   = false);
                        PanelPaint.Invalidate();
                        for (int i = 0; i < lstObject.Count; i++)
                        {
                            if (lstObject[i].IsHit(e.Location) && PanelPaint.Cursor == Cursors.Hand)
                            {
                                selectedShape           = lstObject[i];
                                lstObject[i].IsSelected = true;
                                lstObject[i].IsResize   = false;
                                PanelPaint.Invalidate();
                                break;
                            }
                        }
                        for (int j = 0; j < lstObject.Count; j++)
                        {
                            if (lstObject[j].IsHitPoint(e.Location) && PanelPaint.Cursor == Cursors.SizeAll)
                            {
                                resizeShape             = lstObject[j];
                                lstObject[j].IsResize   = true;
                                lstObject[j].IsSelected = true;
                                PanelPaint.Invalidate();
                                break;
                            }
                        }
                        if (selectedShape != null || resizeShape != null)
                        {
                            if (selectedShape != null)
                            {
                                isMovingShape = true;
                                previousPoint = e.Location;
                            }
                            if (resizeShape != null)
                            {
                                isResizeShape = true;
                            }
                        }
                        else
                        {
                            isMouseSelect  = true;
                            selectedRegion = new System.Drawing.Rectangle(e.Location, new Size(0, 0));
                        }
                    }
                }
                else
                {
                    isMouseDown = true;
                    lstObject.ForEach(shape => shape.IsSelected = false);
                    if (currentShape == CurrentShape.Line)
                    {
                        Line myObj = new Line();
                        myObj.p1            = e.Location;
                        myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                        myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                        lstObject.Add(myObj);
                    }
                    else if (currentShape == CurrentShape.Circle)
                    {
                        Circle myObj = new Circle();
                        myObj.p1            = e.Location;
                        myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                        myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                        if (mode == ShapeMode.Fill)
                        {
                            myObj.Fill = true;
                            HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle),
                                                                   comboBox1.SelectedItem.ToString(), true);
                            myObj.hatch = new HatchBrush(hs, forecolor.BackColor, bc.BackColor);
                        }
                        lstObject.Add(myObj);
                    }
                    else if (currentShape == CurrentShape.Ellipse)
                    {
                        Ellipse myObj = new Ellipse();
                        myObj.p1            = e.Location;
                        myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                        myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                        myObj.hatch         = new HatchBrush(HatchStyle.BackwardDiagonal, myColor);
                        if (mode == ShapeMode.Fill)
                        {
                            myObj.Fill = true;

                            HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle),
                                                                   comboBox1.SelectedItem.ToString(), true);
                            myObj.hatch = new HatchBrush(hs, forecolor.BackColor, bc.BackColor);
                        }
                        lstObject.Add(myObj);
                    }
                    else if (currentShape == CurrentShape.Rectangle)
                    {
                        Rect myObj = new Rect();
                        myObj.p1            = e.Location;
                        myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                        myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                        if (mode == ShapeMode.Fill)
                        {
                            myObj.Fill = true;

                            {
                                HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle),
                                                                       comboBox1.SelectedItem.ToString(), true);
                                myObj.hatch = new HatchBrush(hs, forecolor.BackColor, bc.BackColor);
                            }
                        }
                        lstObject.Add(myObj);
                    }
                    else if (currentShape == CurrentShape.Arc)
                    {
                        Arc myObj = new Arc();
                        myObj.p1            = e.Location;
                        myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                        myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                        myObj.start         = float.Parse(start.Text.Trim());
                        myObj.sweep         = float.Parse(sweep.Text.Trim());
                        lstObject.Add(myObj);
                    }
                    else if (currentShape == CurrentShape.Polygon)
                    {
                        if (!isDrawPolygon)  //  vẽ mới polygon
                        {
                            Polygon myObj = new Polygon();
                            myObj.pen           = new Pen(myColor, Convert.ToInt32(cblineWidth.SelectedItem));
                            myObj.pen.DashStyle = (DashStyle)cbDashMode.SelectedIndex;
                            if (mode == ShapeMode.Fill)
                            {
                                myObj.Fill = true;

                                HatchStyle hs = (HatchStyle)Enum.Parse(typeof(HatchStyle),
                                                                       comboBox1.SelectedItem.ToString(), true);
                                myObj.hatch = new HatchBrush(hs, forecolor.BackColor, bc.BackColor);
                            }
                            myObj.Points.Add(e.Location); // add 2 tọa độ để vẽ
                            myObj.Points.Add(e.Location);
                            lstObject.Add(myObj);
                            isDrawPolygon = true;
                        }
                        else
                        {
                            Polygon polygon = lstObject[lstObject.Count - 1] as Polygon;
                            polygon.Points[polygon.Points.Count - 1] = e.Location;
                            polygon.Points.Add(e.Location);
                        }
                        isMouseDown = false;
                    }
                }
            }
            catch
            {
            }
        }