繁体   English   中英

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

[英]How to fill different color for one of my rectangle from my list when the mouse is hover on that particular area?

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

    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 函数可以帮您解决问题。 它将在适当的时间触发 Paint 事件,您的图形将被更新。 找到无效的任何控制元素都有它。 因此,您可以使用画布控件下的 invalidate 方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM