0

Graphics Path OnPaintEvent を使用して roundRectangle を描画しましたが、既に mouseevent を追加して、カーソルが gp の上にあるかどうかを確認しました。

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!");
        }

質問: graphicsPath ランタイムのサイズを変更または再描画 (前を非表示にしてから新規描画) する方法はありますか?

4

1 に答える 1

0

Invalidateを再描画するために呼び出すFormので、OnPaint(PaintEventArgs e)が実行されます。

次の例を確認してください。

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

お役に立てば幸いです。

于 2015-03-10T06:25:00.763 に答える