0

I' ve drawn roundRectangle using Graphics Path OnPaintEvent and I already added mouseevent to know if cursor was over the g.p .

void Round_MouseMove(object sender, MouseEventArgs e)
        {
          Point mousePt = new Point(e.X, e.Y);
          if (_path != null)
             if (_path.IsVisible(e.Location))
                MessageBox.Show("GraphicsPath has been hovered!");
        }

Question: is there a way to resize or redraw(hide previous then draw new) a graphicsPath runtime?

Elegiac
  • 361
  • 2
  • 15
  • General rule for Winforms rendering: only draw in `Paint` event or `OnPaint()` override; keep track somewhere what you want to draw (e.g. bitmap, list of points, objects, etc.); call `Invalidate()` when you want the screen to be redrawn (i.e. when the things you keep track of to draw have changed). From your question, I'm guessing you've violated all of these rules (a common mistake for new programmers). So step #1 is to understand those rules, and study tutorials and documentation that explain why those rules exist and how to work within them. – Peter Duniho Mar 10 '15 at 05:51

1 Answers1

0

Invoke Invalidate for redrawn the Form, so the OnPaint(PaintEventArgs e) will be executed.

Check the following example:

public sealed partial class GraphicsPathForm : Form
{
    private bool _graphicsPathIsVisible;

    private readonly Pen _pen = new Pen(Color.Red, 2);
    private readonly Brush _brush = new SolidBrush(Color.FromArgb(249, 214, 214));
    private readonly GraphicsPath _graphicsPath = new GraphicsPath();
    private Rectangle _rectangle = new Rectangle(10, 30, 100, 100);

    public GraphicsPathForm()
    {
        InitializeComponent();

        _graphicsPath.AddRectangle(_rectangle);
    }


    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.InterpolationMode = InterpolationMode.Bilinear;
        g.SmoothingMode = SmoothingMode.AntiAlias;

        g.DrawPath(_pen, _graphicsPath);

        if (_graphicsPathIsVisible)
            g.FillPath(_brush, _graphicsPath);


        base.OnPaint(e);
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        var isVisible = _graphicsPath.IsVisible(e.Location);

        if (isVisible == _graphicsPathIsVisible)
            return;

        const int zoom = 5;

        if (isVisible)
        {
            if (!_graphicsPathIsVisible)
            {
                _rectangle.Inflate(zoom, zoom);
                _graphicsPath.Reset();
                _graphicsPath.AddRectangle(_rectangle);
            }
        }
        else
        {
            if (_graphicsPathIsVisible)
            {
                _rectangle.Inflate(-zoom, -zoom);
                _graphicsPath.Reset();
                _graphicsPath.AddRectangle(_rectangle);
            }
        }

        _graphicsPathIsVisible = isVisible;
        Invalidate();

        base.OnMouseMove(e);
    }
}

I hope it helps.

denys-vega
  • 3,522
  • 1
  • 19
  • 24
  • i saw it and it works! ty ... it fill color every mousemove ... but it is possible to make rectangle change size if hovered? – Elegiac Mar 10 '15 at 06:41
  • @Elegiac yes sure that is possible. You need to store the rectangle in a var, change the size and add again to `_graphicsPath`. I will try to modify my example. – denys-vega Mar 10 '15 at 06:47