C#如何在面板上绘制橡皮筋选择矩形,就像在Windows资源管理器中使用的一样?

我有一个带有一些用户控件的Flow Layout面板。 我想使用鼠标选择这些控件,就像在Windows文件浏览器中使用的一样。 我试过这些: https : //support.microsoft.com/en-us/kb/314945但是它非常闪烁而没用(我可能错了,请指正)。 任何好的例子请。

如果问题只是闪烁。 您可能希望将Forms双缓冲区属性设置为true。

绘制橡皮筋矩形是这样的:

private void panel1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == System.Windows.Forms.MouseButtons.Left) { panel1.Refresh(); using (Graphics g = panel4.CreateGraphics()) { Rectangle rect = GetRectangle(mdown, e.Location); g.DrawRectangle(Pens.Red, rect); } } } private void panel1_MouseDown(object sender, MouseEventArgs e) { mdown = e.Location; } 

它使用辅助函数:

 static public Rectangle GetRectangle(Point p1, Point p2) { return new Rectangle(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y), Math.Abs(p1.X - p2.X), Math.Abs(p1.Y - p2.Y)); } 

我没有得到任何闪烁。 如果你这样做,也许你已经编写了Paint evetn,你可能想要使用双缓冲Panel

 class DrawPanel : Panel { public DrawPanel() { DoubleBuffered = true; } }