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!
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.
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.
精彩评论