none
Drag and drop

    Question

  • Hello, I am interested in making a map creator in C#. I know how to design the whole application the only part I do not understand is how to drag and drop say little tile images onto another section of the form and have it place that image there. Is that more directX or what? If anyone has any suggestions on how to go about doing this please feel free to post. Thank you for your time.
    Tuesday, June 05, 2007 3:19 PM

Answers

  • Google maps aren't hard to do in Windows Forms.  I'll give you an example with just one tile, you can easily extend with multiple tiles to make a map.  Drop a large panel onto the form, then paste this code:

      public partial class Form1 : Form {
        private Point mOffset = Point.Empty;   // Map offset
        private Image mTile;                   // Tile(s)
        private bool mDragging;                // Busy dragging map
        private Point mMousePos;               // Current mouse position
        public Form1() {
          InitializeComponent();
          mTile = Bitmap.FromFile(@"c:\temp\test1.bmp");
          panel1.MouseDown += panel1_MouseDown;
          panel1.MouseMove += panel1_MouseMove;
          panel1.MouseUp += panel1_MouseUp;
          panel1.Paint += panel1_Paint;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e) {
          mDragging = true;
          mMousePos = e.Location;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e) {
          if (mDragging) {
            mOffset.Offset(e.Location.X - mMousePos.X, e.Location.Y - mMousePos.Y);
            mMousePos = e.Location;
            panel1.Invalidate();
          }
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e) {
          mDragging = false; 
        }
        private void panel1_Paint(object sender, PaintEventArgs e) {
          Point pos = new Point((panel1.Width - mTile.Width) / 2 + mOffset.X,
            (panel1.Height - mTile.Height) / 2 + mOffset.Y);
          e.Graphics.DrawImage(mTile, pos);
        }
      }

    Tuesday, June 05, 2007 11:36 PM

All replies

  • what are your "tiles"?

    If they are just a control, you can use drag-drop events to watch for drag-drop and relocate the control that is your tile.

    Otherwise, you'll need to monitor the cursor. On mouse down, record what object is "clicked". Finally on mouse up, move the object from location A to location B.

    Give us some base information about what your system already is and maybe what you've already tried
    Tuesday, June 05, 2007 5:04 PM
  • My tiles are little 35 by 32 bitmaps in image boxes. I want to be able to drag and drop them onto another area and then the image is now there. I have a toolbox on the right which contains picture boxes of things you can use to build a map. Its the actual map part that I am having difficulty figuring out how to do. I do not know if I should just make a huge panel, or several panels or picture boxes and you drag them onto them. I hope this helps. If not I will try again. Thank you for your time.
    Tuesday, June 05, 2007 7:24 PM
  • Making the map area can be done multiple ways. The easiest is to make one object, drawing gridlines, and the current map onto it.

    You're drag and drop will actually should pull the image/filename from the one area and paste it into the "segment" on the map.

    You can set up an MD array that defined what is in the tile on the map, using that to redraw the map would give you refreshing ability. You can also have your map be larger than your display, you'll just need to keep track of where the current view's upper left corner is set to. Using this base, you'll need to get the cursor position on the map and convert it to map position which should be easily converted to the MD array index [x, y] style of indexing
    Tuesday, June 05, 2007 7:58 PM
  • Google maps aren't hard to do in Windows Forms.  I'll give you an example with just one tile, you can easily extend with multiple tiles to make a map.  Drop a large panel onto the form, then paste this code:

      public partial class Form1 : Form {
        private Point mOffset = Point.Empty;   // Map offset
        private Image mTile;                   // Tile(s)
        private bool mDragging;                // Busy dragging map
        private Point mMousePos;               // Current mouse position
        public Form1() {
          InitializeComponent();
          mTile = Bitmap.FromFile(@"c:\temp\test1.bmp");
          panel1.MouseDown += panel1_MouseDown;
          panel1.MouseMove += panel1_MouseMove;
          panel1.MouseUp += panel1_MouseUp;
          panel1.Paint += panel1_Paint;
        }
        private void panel1_MouseDown(object sender, MouseEventArgs e) {
          mDragging = true;
          mMousePos = e.Location;
        }
        private void panel1_MouseMove(object sender, MouseEventArgs e) {
          if (mDragging) {
            mOffset.Offset(e.Location.X - mMousePos.X, e.Location.Y - mMousePos.Y);
            mMousePos = e.Location;
            panel1.Invalidate();
          }
        }
        private void panel1_MouseUp(object sender, MouseEventArgs e) {
          mDragging = false; 
        }
        private void panel1_Paint(object sender, PaintEventArgs e) {
          Point pos = new Point((panel1.Width - mTile.Width) / 2 + mOffset.X,
            (panel1.Height - mTile.Height) / 2 + mOffset.Y);
          e.Graphics.DrawImage(mTile, pos);
        }
      }

    Tuesday, June 05, 2007 11:36 PM
  • Thank you nobugz, I can now render the images on the form load. Is there a way that I can click the images boxes and drop them onto the panel? Either way thank you for your help its another step towards my goal.
    Wednesday, June 06, 2007 1:17 AM