开发者

Weird shape resizing behavior in WPF

开发者 https://www.devze.com 2022-12-30 08:24 出处:网络
I am making a WPF application that will let the user draw and resize shapes. The resizing part is done using adorners, and the shapes are my own classes derived from Shape.

I am making a WPF application that will let the user draw and resize shapes. The resizing part is done using adorners, and the shapes are my own classes derived from Shape. For example I have a Polyline, and for each of it's points I am adorning a Thumb with a handler on it's DragDelta event:

    void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        PolylineEx polyline = this.AdornedElement as PolylineEx;
        ResizingThumb thumb = sender as ResizingThumb;

        int index = (int)thumb.Tag;

        Point newPoint = new Point(
            polyline.Points[index].X + e.HorizontalChange,
            polyline.Points[index].Y + e.VerticalChange);
        Trace.WriteLine(String.Format("Arranging point {0}:{1},{2}", index, newPoint.X, newPoint.Y));
        polyline.Points[index] = newPoint;
    }

Well, the problem is that sometime开发者_开发技巧s (often), dragging a thumb is not smooth at all. First I thought it was a performance issue, because of a new Point being created every time (and other things), but then I noticed that sometimes the point is set up at weird coordinates that have nothing to do with the position of the mouse.

I've also uploaded a sample project HERE, could someone who knows his way around wpf take a look and enlighten me of what is happening?

Also, if anyone thinks of a better way for doing this, any feedback would be highly appreciated.

Thank you.

Edit: HERE is a new link to the project files.


I'm ending up answering my own question :)

I think the problem was here:

protected override System.Windows.Media.Geometry DefiningGeometry
{
    get
    {
        StreamGeometry geometry = new StreamGeometry();
        using (StreamGeometryContext context = geometry.Open())
        {
            context.BeginFigure(Points[0], false, true);
            foreach (Point pt in Points)
            {
                context.LineTo(pt, true, true);
            }
            geometry.Freeze();
            return geometry;
        }
    }
}

I switched to using PathGeometry & LineSegments and it works fine now.

0

精彩评论

暂无评论...
验证码 换一张
取 消