![](/img/trans.png)
[英]How to Draw a Rubber Band Selection Rectangle accurately on a Rotated Canvas?
[英]C# How to Draw a Rubber Band Selection Rectangle on Panel, like one used in Windows Explorer?
我有一個流布局面板,其中包含一些用戶控件。 我想使用鼠標使用矩形選擇來選擇這些控件,就像在 windows 文件資源管理器中使用的一樣。 我已經嘗試過這些:https: //support.microsoft.com/en-us/kb/314945但它非常閃爍並且沒有用(我可能錯了,請糾正我)。 請有任何好的例子。
繪制橡皮筋矩形是這樣完成的:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
panel1.Refresh();
using (Graphics g = panel1.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
事件,你可能想要使用雙緩沖Panel
:
class DrawPanel : Panel
{
public DrawPanel()
{
DoubleBuffered = true;
}
}
更新:您可以使用Picturebox
或Label
(使用Autosize=false
),而不是Panel
,它是一個Container
控件,並不是真正要在上面繪制; 兩者都具有開箱即用的DoubleBuffered
屬性,並且比Panels
更好地支持繪圖。
如果問題只是閃爍。 您可能希望將 Forms 雙緩沖區屬性設置為 true。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.