简体   繁体   中英

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

    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. It will fire the Paint event at an appropriate time and your graphics will be updated. To find invalidate any control element has it. So you can use the invalidate method found under your canvas control.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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