简体   繁体   English

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

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

I have list of rectangles if the mouse hover on the area of the rectangles located i want to change the color for the rectangle my mouse coordinate is on.如果鼠标悬停在我想更改鼠标坐标所在矩形的颜色的矩形区域上,我有矩形列表。 I have done this but the color is not fast enough to change.我已经这样做了,但颜色不够快,无法改变。 The following method selects which rectangle it is.以下方法选择它是哪个矩形。

    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;
            }

        }

    }

This is my RedDraw Function you can call it这是我的 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();
    }

Just like @TaW says it is the Invalidate function that is going to do you the trick.就像@TaW 所说的那样,Invalidate 函数可以帮您解决问题。 It will fire the Paint event at an appropriate time and your graphics will be updated.它将在适当的时间触发 Paint 事件,您的图形将被更新。 To find invalidate any control element has it.找到无效的任何控制元素都有它。 So you can use the invalidate method found under your canvas control.因此,您可以使用画布控件下的 invalidate 方法。

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

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