0

I would like to have drag handles for my dynamic pictureBoxes, here is the code of how it was created if it helps:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        PictureBox flower1 = new PictureBox();
        flower1.Image = Properties.Resources.Flower2;
        flower1.Location = new Point(panel1.Location.X + 10, panel1.Location.Y + 10);
        flower1.Size = new System.Drawing.Size(pictureBox1.Size.Width, pictureBox1.Size.Height);
        flower1.Parent = panel1;
        panel1.Controls.Add(flower1);

        flower1.BringToFront();
        flower1.MouseMove += new MouseEventHandler(flower1_MouseMove);
        flower1.MouseDown += new MouseEventHandler(flower1_MouseDown);
    }

    private void flower1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            MouseDownLocation = e.Location;
        }
    }

    private void flower1_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            PictureBox flower1 = (PictureBox)sender;
            flower1.Left = e.X + flower1.Left - MouseDownLocation.X;
            flower1.Top = e.Y + flower1.Top - MouseDownLocation.Y;
        }
    }

I have no clue how to start. I need to be able to resize these dynamically created images using drag handles. I Googled it and couldn't find anything to do with DYNAMIC controls.

Pierre-Luc Pineault
  • 8,993
  • 6
  • 40
  • 55
  • There's a lot more to it, using drag handles effectively requires a transparent window on top of the control so you can paint the handles. Consider a much simpler alternative, make the control resizable. Code [is here](http://stackoverflow.com/a/17264543/17034) – Hans Passant Aug 29 '13 at 18:34
  • Sorry, that's no good. It doesn't allow me to resize the image, it just makes the pictureBox smaller, but the image doesn't scale with it. – user2730088 Aug 29 '13 at 18:54
  • Well, just set the SizeMode property to "Zoom". – Hans Passant Aug 29 '13 at 19:02
  • How would I do that in code? I am doing this dynamically. So, I need to do something like: flower1.SizeMode = SizeMode.Zoom; – user2730088 Aug 29 '13 at 19:07
  • In fact got it now, thanks for the answer! I put flower1.SizeMode - PictureBoxSizeMode.Zoom; – user2730088 Aug 29 '13 at 19:11

0 Answers0