我有一个矩形列表,如果鼠标悬停在矩形所在的区域上,我想更改鼠标坐标所在矩形的颜色。我已经这样做了,但颜色变化不够快。以下方法选择它是哪个矩形。
void OnMouseMoveOnTheRectangles(MouseEventArgs e)
{
RectangleF[] allRectangles = new RectangleF[aListDrawings.Count];
aListDrawings.CopyTo(allRectangles);
if (allRectangles.Length == 0)
return;
RectangleF currentSelected = RectangleF.Empty;
foreach (RectangleF rec in allRectangles)
{
RectangleF current = GetOffsetRectangle(rec);
if (current.Contains(e.Location))
{
_currentActive = current;
break;
}
}
}
这是我的RedDraw函数,你可以称之为
protected virtual void DrawSelection(PaintEventArgs e, RectangleF[] sRegion,
SolidBrush _brush)
{
if (sRegion.Length == 0)
return;
e.Graphics.SetClip(this.GetInsideViewPort(true));
RectangleF[] offsetRectangles = new RectangleF[sRegion.Length];
int x = 0;
foreach (RectangleF r in sRegion)
{
offsetRectangles[x] = this.GetOffsetRectangle(r);
x++;
}
using (Brush brush = _brush)
{
e.Graphics.FillRectangles(brush, offsetRectangles);
}
//This is where i color i tried to change the color for that particular rectangle
if (_currentActive != RectangleF.Empty)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(0x90, Color.Red)),
_currentActive);
}
using (Pen pen = new Pen(this.SelectionColor))
{
e.Graphics.DrawRectangles(pen, offsetRectangles);
}
e.Graphics.ResetClip();
}
就像@TaW说的那样,Invalidate函数会给你带来好处。它将在适当的时间触发"绘制"事件,并且图形将更新。要查找invalidate,任何控件元素都有它。因此,您可以使用在画布控件下找到的invalidate方法。