14 Replies - 412 Views - Last Post: 07 September 2012 - 03:19 PM Rate Topic: -----

#1 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 01:31 PM

Ok 1st I took a screenshot about what I'm talking about. It's a MS Paint type application which is what I'm working on. When I click pictureBox1 I can click and drag it across panel1 which has pictureBox2 painted on it. Problem is when I pictureBox1 it's background image trails across the panel :(!):
Attached Image
Here is the code I am using:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{           
    x = e.X;
    y = e.Y;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        pictureBox1.Left += (e.X - x);
        pictureBox1.Top += (e.Y - y);
    }
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
      e.Graphics.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));
}


Here is the code for the double buffered panel:
public DoubleBufferPanel()
        {

            // Set the value of the double-buffering style bits to true.
            this.DoubleBuffered = true;



            this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
             ControlStyles.AllPaintingInWmPaint, true);

            this.UpdateStyles();

        }


This application contains 1 doublebuffered Panel and 2 pictureBoxes each 700px in size.

I was hoping someone could help solve the cause of this horrible flicker which is occuring in this small size application.

This post has been edited by aquahoya: 07 September 2012 - 01:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Trailing/flicker effect when dragging picBox across dblebuffered panel

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 01:36 PM

Did you set the style for the app?

Typically I would throw this in the 'new' of a form... (yes I know it's vb.net but the translations pretty easy).

Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)


http://www.dreaminco...-basics-part-1/
Was This Post Helpful? 0
  • +
  • -

#3 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 01:39 PM

View Postmodi123_1, on 07 September 2012 - 01:36 PM, said:

Did you set the style for the app?

Typically I would throw this in the 'new' of a form... (yes I know it's vb.net but the translations pretty easy).

Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)


http://www.dreaminco...-basics-part-1/

I had that code in my double buffered panel modi123, if that's what you mean. I didn't realize I forgot to add my bufferedpanel class to my original post so I just added that.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 01:41 PM

Try promoting that from just the panel and do the whole form.
Was This Post Helpful? 0
  • +
  • -

#5 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 01:55 PM

View Postmodi123_1, on 07 September 2012 - 01:36 PM, said:

Did you set the style for the app?

Typically I would throw this in the 'new' of a form... (yes I know it's vb.net but the translations pretty easy).

Me.SetStyle(ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)


http://www.dreaminco...-basics-part-1/

modi123 I added it to the app using this:
public Form1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }


but that still did not get rid of the trailling of pictureBox1 across panel1 :helpsmilie:
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:06 PM

What does your paint event look like?
Was This Post Helpful? 0
  • +
  • -

#7 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:08 PM

View Postmodi123_1, on 07 September 2012 - 02:06 PM, said:

What does your paint event look like?

void panel1_Paint(object sender, PaintEventArgs e)
{
 e.Graphics.DrawImage(pictureBox2.BackgroundImage, new Rectangle(pictureBox2.Location, pictureBox2.Size));
}



Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:10 PM

okay.. let me take another step back - why are you using picture boxes and not say.. bitmap objects?
Was This Post Helpful? 0
  • +
  • -

#9 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:25 PM

View Postmodi123_1, on 07 September 2012 - 02:10 PM, said:

okay.. let me take another step back - why are you using picture boxes and not say.. bitmap objects?

well I have been using the pictureBoxes for quite sometime and thought that using them would be similar or better than using bitmaps?

I've never used a bitmap object but if I changed the panel1 paint event to something like this:
void panel1_Paint(object sender, PaintEventArgs e)
	{
	 e.Graphics.DrawImage(Bitmap.BackgroundImage, new Rectangle(Bitmap.Location, Bitamp.Size)); //just an example
	}


then that would get rid of the trailing problem I've been having modi123?
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:33 PM

Check out that link I sent above.. part of it deals with moving and displaying bitmaps on a form... the reason bitmaps are superior to pictureboxes (in this instance) is they shed all the bulk and barnacles of the picturebox object. So if you are doing a videogame or paint it's best to keep in mind three things:

1. the three style lines
2. bitmap > picture box
3. keep everything being painted in the form's paint event.


Eyeball that code and you can fiddle around with how things are drawn and how to trigger a redraw.
Was This Post Helpful? 0
  • +
  • -

#11 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:52 PM

thanks modi123 for the link. I'll read it

do you think it would be a good idea to continue using pictureBox1 for the Move & MouseDown event; and use the bitmap object for containing the image painted on the panel?
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 6610
  • View blog
  • Posts: 23,941
  • Joined: 12-June 08

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 02:55 PM

Naw.. not really. You'll need something lithe and light and pictureboxes just have a fat ol' booty.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 4963
  • View blog
  • Posts: 10,558
  • Joined: 02-June 10

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 03:01 PM

Just a thought for an alternative direction... Drag n drop.
The OS already supports it by showing a ghost of the object being dragged. So if you were to make the picturebox a DnD object and the panel or form receiving the Drop event... You would basically drag the picturebox from the panel, to the panel. You don't really have to fully impliment the DnD process. Just use it for the drag ghost, but still use the mousemove tracking as you go to get your new coordinates when you let go of the mouse.

This way you aren't actually painting the dragged item every pixel of the move... you eliminate the whole double-buffer part and let the OS do the fancy painting on its level.

Just a thought for some experimentation.
Was This Post Helpful? 0
  • +
  • -

#14 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 03:05 PM

View Postmodi123_1, on 07 September 2012 - 02:55 PM, said:

Naw.. not really. You'll need something lithe and light and pictureboxes just have a fat ol' booty.


lol thanks for that image of a pictureBox which I'll never get outta my head.

modi123 but for images moved to the outskirts of the panel, the image would be displayed like so
Attached Image
but I don't see this as a possibility if I use a bitmap

This post has been edited by aquahoya: 07 September 2012 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#15 aquahoya  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 08-January 12

Re: Trailing/flicker effect when dragging picBox across dblebuffered panel

Posted 07 September 2012 - 03:19 PM

View PosttlhIn`toq, on 07 September 2012 - 03:01 PM, said:

Just a thought for an alternative direction... Drag n drop.
The OS already supports it by showing a ghost of the object being dragged. So if you were to make the picturebox a DnD object and the panel or form receiving the Drop event... You would basically drag the picturebox from the panel, to the panel. You don't really have to fully impliment the DnD process. Just use it for the drag ghost, but still use the mousemove tracking as you go to get your new coordinates when you let go of the mouse.

This way you aren't actually painting the dragged item every pixel of the move... you eliminate the whole double-buffer part and let the OS do the fancy painting on its level.

Just a thought for some experimentation.

tlhIn`toq are you saying that instead of using pictureBox1_mousedown & _mousemove for moving the picturebox Image, I coould just keep it hidden and inside the panel1_paint event thats where I handle the painting of the Moving panel?(similar to a DnD implementation)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1