0

hello all im creating a game where user can drag and drop pictureboxes.I managed to do it but i dont like how it behaves when you drag n drop a picturebox. With this code

private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
        currentX = e.X;
        currentY = e.Y;
    }

    private  void myPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        Control control = (Control)sender;
        if (isDragging)
        {
           control.Top = control.Top + (e.Y - currentY);
           control.Left = control.Left +(e.X - currentX); 
        }
    }

    private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

it behaves like this (video 1) So i put a .update() to it

private void myPictureBox_MouseDown(object sender, MouseEventArgs e)
    {
        isDragging = true;
        currentX = e.X;
        currentY = e.Y;
    }

    private  void myPictureBox_MouseMove(object sender, MouseEventArgs e)
    {
        Control control = (Control)sender;
        if (isDragging)
        {
           control.Top = control.Top + (e.Y - currentY);
           control.Left = control.Left +(e.X - currentX);
           control.update();
        }
    }

    private void myPictureBox_MouseUp(object sender, MouseEventArgs e)
    {
        isDragging = false;
    }

But because its in the if loop its behave like this (video 2) .Do you have any suggestion? thanks in advance.

maam27
  • 444
  • 3
  • 21
valentinosael
  • 25
  • 1
  • 1
  • 6

2 Answers2

0

Try setting double buffering for all pictureboxes and the control they are on

SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

If that doesnt work you need to do the drawing yourself (isnt hard) to make use of the double buffer setting.

Vajura
  • 1,112
  • 7
  • 16
0

I tried to reproduce the stuff that you're experiencing.

When the picture box have a form with a background, the same effect as your video occurs. When the picuter box have a picture box as the background, it runs a little bit better. However, I can't make the control background transparent.

The solution to draw your own control could work.

My suggestion is, if you were really into creating a game. Using already successfull game engine such as Unity, XNA could increase your productivity.

Aidity
  • 41
  • 10