-1

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

    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();
    }
4

1 回答 1

0

就像@TaW 所说的那样,Invalidate 函数会帮你解决问题。它将在适当的时间触发 Paint 事件,并且您的图形将被更新。找到无效的任何控制元素都有它。因此,您可以使用画布控件下的无效方法。

于 2015-11-28T06:04:47.127 回答