Есть нарисованная башня,состоящая из прямоугольников.Как перетаскивать нарисованные прямоугольники с помощью мышки пользователю игры?

         Tower[] c;
         Rectangle rect = new Rectangle(0, 0, 200, 200);
        Point ptDown = new Point();
        Boolean mouseDown = false;




        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {   
        }

        private void Hanoi(int from, int cou)
        {
            if (c != null)  //перед рисованием проверяем, есть ли стержни
            {

                c[0].Draw();  //рисуем 3 стержня                
                c[1].Draw();
                c[2].Draw();
            }
        }

        public class Tower
        { 
          public Stack<int> h;
          public int nomer { get; set; } 
          public void Add(int i)
          {
            h.Push(i);
          }

          Graphics g;

         public Tower(Graphics g, int nomer)
         {
            this.g = g;
            this.nomer = nomer;
            h = new Stack<int>();

         }

         public int X
         {
            get { return 30 + (nomer - 1) * 200; }
         } // свойство для хранения координаты X стержня,

            public int Y
            {
             get { return 70; }
            } // свойство для хранения координаты Y стержня.

            private void Clear()
            {
              g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(X, Y, 200, 200));
            }


             public void Draw()
             {
               Clear();
               for (int i = 0; i < h.Count(); i++)
               {

                int a = h.ElementAt(i);
                Rectangle r = new Rectangle(X + 100 - a * 10, Y + 20 * (10 - h.Count() + i), a * 20, 20);
                g.FillRectangle(new SolidBrush(Color.Gold), r);


               }

              }

        }

        private void button1_Click(object sender, EventArgs e)
        {

            Graphics g;  //Для рисования стержня на форме
            g = this.CreateGraphics(); //Создаем область для рисования на форме
            c = new Tower[3];
            c[0] = new Tower(g, 1);
            c[1] = new Tower(g, 2);
            c[2] = new Tower(g, 3);
            c[0].Add(8); //Добавляем диски на стержни
                         //Число означает номер диска 
            c[0].Add(7);
            c[0].Add(6);
            c[0].Add(5);
            c[0].Add(4);
            c[0].Add(3);
            c[0].Add(2);
            c[0].Add(1);
             Hanoi(1, 8);

        }

        void thisBox_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

         private void Form2_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Gold, rect);
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (Rectangle.Intersect(new Rectangle(e.X, e.Y, 0, 0), rect) != Rectangle.Empty)
            {
                ptDown = Point.Subtract(e.Location, (Size)rect.Location);
                mouseDown = true;
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseDown)
                rect.Location = Point.Subtract(e.Location, (Size)ptDown);

            this.Invalidate();
        }

        private Point GetMousePosition()  {
            return new Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
        }

        private int DeterminePoleFromCursorPosition()
        {
            Point MousePosition = GetMousePosition();
            if (MousePosition.X < c[0].X)
                return 0;
            if (MousePosition.X > c[1].X - 60 && MousePosition.X < c[2].X - 60)
                return 1;
            if (MousePosition.X > c[2].X - 60 && MousePosition.X < c[2].X )
                return 2;
            //Use pole 0 as default
            return 0;
        }

Ответы (0 шт):