Dan7
Dan7

Reputation: 1727

WinForm: Selection box flickers when dragging the mouse

I made a click-drag selection box in a picture box. In the picturebox Paint event hander I use

e.Graphics.DrawRectangle(pen, rectangle);

and update the rectangle and refreshe the picturebox in the mouse move event handler.

The selection box looks smooth as long as the mouse remains at the bottom-right corner (i.e. drag to right/bottom). However if I want to drag the mouse to the left or up, rectangle.X/rectangle.Y has to be re-set constantly and the box flickers very noticeably.

Is there a better/more efficient way to do the drawing? Much appreciated!

Upvotes: 0

Views: 1889

Answers (2)

Dan7
Dan7

Reputation: 1727

I just found the solution: Replacing pictureBox.Refresh() with pictureBox.Invalidate() would make the redrawing smooth at all times. It seems Refresh() adds huge overhead in this case, that even setting the main Form or the PictureBox's DoubleBuffered property to true will not help.

Upvotes: 1

Alastair Pitts
Alastair Pitts

Reputation: 19601

another thing to take into account is DoubleBuffering

How do I enable double-buffering of a control using C# (Windows forms)?

have a look at the excepted answer here for the correct double buffering code.

Upvotes: 1

Related Questions