C# 当鼠标悬停在某个特定区域时,如何为列表中的某个矩形填充不同的颜色?

C# 当鼠标悬停在某个特定区域时,如何为列表中的某个矩形填充不同的颜色?,c#,winforms,graphics,c#-2.0,drawrectangle,C#,Winforms,Graphics,C# 2.0,Drawrectangle,我有一个矩形列表,如果鼠标悬停在所定位的矩形区域上,我想更改鼠标坐标所在矩形的颜色。我已经这样做了,但是颜色变化不够快。下面的方法选择它是哪个矩形 void OnMouseMoveOnTheRectangles(MouseEventArgs e) { RectangleF[] allRectangles = new RectangleF[aListDrawings.Count]; aListDrawings.CopyTo(allRectangles

我有一个矩形列表,如果鼠标悬停在所定位的矩形区域上,我想更改鼠标坐标所在矩形的颜色。我已经这样做了,但是颜色变化不够快。下面的方法选择它是哪个矩形

    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调用绘画中的重画吗?您是否检查是否有必要(即当前矩形是否已更改)?旁注:
\u currentActive
从不重置为空矩形。我在此处未看到任何会在移动鼠标时启动重画的代码。请提供可靠的重现问题的方法。@t速度不可预测。它不会闪烁。我可能有多个,因为它是动态的。控件是基于控件类的自定义控件。我不使用双重缓冲。是的,我从油漆中调用重画,但不是通过mousmove上的Invalidate。如果我单击或移动过多,或者我最小化并最大化了当前矩形,则该矩形会发生更改,因此重新绘制会滞后于鼠标移动?-是的,我从画图中调用重画,但不是通过mousmove上的Invalidate,那么何时以及如何触发画图?