Member 13594414 Ответов: 1

Перемещение и изменение размеров фигур


Эй,
В моем настольном приложении я сделал прямоугольники на растровых изображениях в соответствии с пользовательским вводом ,строкой и столбцом wise.it работает очень хорошо,но я также хочу переместить и изменить размер прямоугольника.Всякий раз, когда я нажимаю кнопку Переместить основной прямоугольник, и его суб-прямоугольник также перемещаются вместе. Как я могу решить эту проблему?

Что я уже пробовал:

 int arg1;
        int arg2;
       
        public List<rectangle> listRec = new List<rectangle>();
        Graphics g;
        //private Graphics g;
        Point startPos;
        Point currentPos;
        bool drawing;
        Rectangle r1;
        private Rectangle getRectangle()
        {
            r1 = new Rectangle(
                Math.Min(startPos.X, currentPos.X),
                Math.Min(startPos.Y, currentPos.Y),
                Math.Abs(startPos.X - currentPos.X),
                Math.Abs(startPos.Y - currentPos.Y));
            return r1;
        }
        private void Form7_Load(object sender, EventArgs e)
        {
            if(File.Exists("E:\\B1Pockets.txt"))
            {
                File.Delete("E:\\B1Pockets.txt");
            }
            if (File.Exists("E:\\B2Pockets.txt"))
            {
                File.Delete("E:\\B2Pockets.txt");
            }
            if (File.Exists("E:\\Blisters.txt"))
            {
                File.Delete("E:\\Blisters.txt");
            }
            if (File.Exists("E:\\AOI.txt"))
            {
                File.Delete("E:\\AOI.txt");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String data;
            
            Font font = new Font("Arial", 14);
            arg1 = Convert.ToInt32(textBox1.Text);
            arg2 = Convert.ToInt32(textBox2.Text);
            Rectangle rect = new Rectangle();
            rect.Size = new Size(40, 65);
            for (int x = 0; x < arg1; x++)
            {
               // rect.X = x * rect.Width;
                rect.X = x * (rect.Width +30) + 73;
                for (int y = 0; y <arg2 ; y++)
                {
                    rect.Y = y * (rect.Height +35)+38;
                    listRec.Add(rect);
                    data = rect.ToString();
                    TextWriter txt = new StreamWriter("E:\\B1Pockets.txt",true );
                    txt.WriteLine(data);
                    txt.Close();
                 }
            }

          
            foreach (Rectangle rec in listRec)
            {
                g = pictureBox1.CreateGraphics();
                Pen p = new Pen(Color.Red,3);
                g.DrawRectangle(p, rec);
                g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 30) ,35 );
                g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 35);
                g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, 35);
                g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 30), (rect.Height + 30) + 40);
                g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, (rect.Height + 30) + 40);
                g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 130, (rect.Height + 30) + 40);
            }
           

            
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (drawing)
            {
                drawing = false;
                var rc = getRectangle();
                if (rc.Width > 0 && rc.Height > 0) listRec.Add(rc);
                pictureBox1.Invalidate();
            }
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            currentPos = startPos = e.Location;
            drawing = true;
        }

        public void ChangeColor(Rectangle target, Color targetColor)
        {
            Pen p = new Pen(targetColor,3);
            g.DrawRectangle(p, target.X, target.Y, target.Width, target.Height);
        }

        private void Form7_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.D0:
                    ChangeColor(listRec[0], Color.Red);
                    break;
                case Keys.D1:
                    ChangeColor(listRec[1], Color.Red);
                    break;
                    //..more code to handle all keys..
            }
        }


        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Arial", 14);
            Pen p1 = new Pen(Color.Red, 3);
            Pen p2 = new Pen(Color.Red, 3);
            if (listRec.Count > 0)
            {
                e.Graphics.DrawRectangles(p1, listRec.ToArray());
              
            }
            if (drawing)
            {
                e.Graphics.DrawRectangle(p2, getRectangle());
               
            }
           
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            currentPos = e.Location;
            if (drawing) pictureBox1.Invalidate();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String data = "";
            Font font = new Font("Arial", 14);
            arg1 = Convert.ToInt32(textBox4.Text);
            arg2 = Convert.ToInt32(textBox3.Text);
            Rectangle rect = new Rectangle();
            rect.Size = new Size(280,190);
            for (int x = 0; x < arg1; x++)
            {
               
                rect.X = x * (rect.Width + 45) + 55;
                for (int y = 0; y < arg2; y++)
                {
                    rect.Y = y * (rect.Height + 35) + 25;
                    listRec.Add(rect);
                    data = rect.ToString();
                    TextWriter txt = new StreamWriter("E:\\Blisters.txt", true);
                    txt.WriteLine(data);
                    txt.Close();
                }
            }

            foreach (Rectangle rec in listRec)
            {
                g = pictureBox1.CreateGraphics();
                Pen p = new Pen(Color.Red, 3);
                g.DrawRectangle(p, rec);
                g.DrawString("B1", font, new SolidBrush(Color.Yellow), 55, 25);
                g.DrawString("B2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 60, 25);

            }
        }

       
        private void button3_Click(object sender, EventArgs e)
        {
            String data;
            Font font = new Font("Arial", 14);
            Rectangle rect = new Rectangle();
            rect.Size = new Size(40, 65);
            for (int x = 0; x < 3; x++)
            {
                // rect.X = x * rect.Width;
                rect.X = x * (rect.Width + 30) +463 ;
                for (int y = 0; y < 2; y++)
                {
                    rect.Y = y * (rect.Height + 35) + 38;
                    listRec.Add(rect);
                    data = rect.ToString();
                    TextWriter txt = new StreamWriter("E:\\B2Pockets.txt", true);
                    txt.WriteLine(data);
                    txt.Close();
                }
            }


            foreach (Rectangle rec in listRec)
            {
                g = pictureBox1.CreateGraphics();
                Pen p = new Pen(Color.Red, 3);
                g.DrawRectangle(p, rec);
                g.DrawString("p1", font, new SolidBrush(Color.Yellow), (rect.Width + 420), 35);
                g.DrawString("p2", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 450, 35);
                g.DrawString("p3", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 520, 35);
                g.DrawString("p4", font, new SolidBrush(Color.Yellow), (rect.Width + 420), (rect.Height + 30) + 40);
                g.DrawString("p5", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 450, (rect.Height + 30) + 40);
                g.DrawString("p6", font, new SolidBrush(Color.Yellow), (rect.Width + 40) + 520, (rect.Height + 30) + 40);
            }
        }
     
    }



I've tried the above code.My rectangles and it's sub rectangles are drawn successfully,but i want to copy whole the rectangle with sub rectangle and move it into different place.Can anyone help me out from this??

1 Ответов

Рейтинг:
0

P Conny M Westh

I would change the ´class you are creating from picturebox to a Panel and I would put each rectangle in its own Panel.

The Panels are much more easy to control in a modeling view that I believe you are creating.

You can also use the FlowLayoutPanel komponent to have som automatic "flow" when positioning the objects in the form.

Panels are very much better to use when you try to move objects around in the form.