Hi All,
I'm building a C# game that will handle drag and drop of images and labels in a visual studio form, but when I drag around a component, the form takes big performance hit. For example, I have some images moving around on the form (pictureboxes) and these stop moving while I'm draging a component around. This is how I implemented the dragging:

private bool is_dragging = false;
        private int click_offset_y;
        private int click_offset_x;

        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            is_dragging = true;
            click_offset_x = e.X;
            click_offset_y = e.Y;
        }

        private void label1_MouseMove(object sender, MouseEventArgs e)
        {
            if (is_dragging == true)
            {
                label1.Left = e.X + label1.Left - click_offset_x;
                label1.Top = e.Y + label1.Top - click_offset_y;
            }
            Invalidate();
        }

        private void label1_MouseUp(object sender, MouseEventArgs e)
        {
            is_dragging = false;
        }

Any help would be much appreciated.

Recommended Answers

All 4 Replies

One more important thing: the performance is only poor when there is a background image for the form. Does anyone know how I can keep performance from dropping down when moving components around?

Take a look at line 19. Calling Invalidate() will cause the entire form to be redrawn (including the background image) when you're moving the mouse. Comment out that line entirely and let the form paint itself.

Without the invalidate() statement, when I drag around the component, the component image only updates every two or three seconds. As a workaround, I added a picturebox that covers the background. One problem with this is that my picturebox component has a transparent area, and when I set the component background to transparent, the transparent portion cuts a hole through everything, and shows the gray background rather than my picturebox background.

One way to fix the issue with my interface would be to figure out how to make it so that the transparent section of my image would show the picture directly behind my image rather than cutting a whole all the way to the background. Does anyone know how to do this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.