【问题标题】:Implementing Custom Adorners around Polygon围绕多边形实现自定义装饰器
【发布时间】:2019-05-19 00:39:11
【问题描述】:

我很难将装饰器添加到多边形的角落。当然,我想在每个点位置都有角落。

这个 Microsoft 页面描述了如何使用他们的完整示例,但我很难理解如何使用他们的完整示例。谁能告诉我如何适应?

非常感谢。

https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/adorners-overview

// Adorners must subclass the abstract base class Adorner.
public class SimpleCircleAdorner : Adorner
{
  // Be sure to call the base class constructor.
  public SimpleCircleAdorner(UIElement adornedElement)
    : base(adornedElement) 
  { 
  }

  // A common way to implement an adorner's rendering behavior is to override the OnRender
  // method, which is called by the layout system as part of a rendering pass.
  protected override void OnRender(DrawingContext drawingContext)
  {
    Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);

    // Some arbitrary drawing implements.
    SolidColorBrush renderBrush = new SolidColorBrush(Colors.Green);
    renderBrush.Opacity = 0.2;
    Pen renderPen = new Pen(new SolidColorBrush(Colors.Navy), 1.5);
    double renderRadius = 5.0;

    // Draw a circle at each corner.
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.TopRight, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomLeft, renderRadius, renderRadius);
    drawingContext.DrawEllipse(renderBrush, renderPen, adornedElementRect.BottomRight, renderRadius, renderRadius);
  }
}

这是我将多边形添加到画布的代码。

Polygon myPolygon;
private Polygon drawPolygon()
{

    //Add the Polygon Element
    myPolygon = new Polygon();

    // Appearance
    myPolygon.Stroke = System.Windows.Media.Brushes.LightCoral;
    // Disabled for hit testing on border only.
    //myPolygon.Fill = System.Windows.Media.Brushes.Transparent;
    myPolygon.StrokeThickness = 6;

    // Alignment
    myPolygon.HorizontalAlignment = HorizontalAlignment.Left;
    myPolygon.VerticalAlignment = VerticalAlignment.Center;

    // Point Array for polyline.
    // This will come from an XML file.
    Point[] polylinePoints = {

        // START: @ TOP LEFT
        new Point(200.0F, 200.0F),

        // 1ST LINE: TOP RIGHT
        new Point(400.0F, 200.0F),
        // 2ND LINE: BOMTTOM RIGHT
        new Point(400.0F, 400.0F),
        // 3RD LINE: BOTTOM LEFT
        new Point(200.0F, 400.0F),

        // END: @ TOP LEFT
        //Not Required to close the Polygon...
        //new Point(200.0F, 200.0F),

    };

    // Load from Point[Array] polylinePoints.
    PointCollection myPointCollection = new PointCollection(polylinePoints);

    //myPointCollection.Add(Point1);

    // Add from PointCollection myPointCollection
    myPolygon.Points = myPointCollection;

    return myPolygon;

}

【问题讨论】:

    标签: c# wpf c#-4.0


    【解决方案1】:

    在您的代码中,您尝试在多边形周围实现自定义装饰器并将多边形添加到 Canvas 面板。为此,您已按照 Microsoft 页面中的说明使用了这个 SimpleCircleAdorner 类。我认为,在这堂课中,您已经通过了 Canvas 面板的默认大小。默认的期望大小值为 0 点。

     Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);
    

    如果你想在多边形周围实现自定义装饰器,你可以设置画布面板的高度和宽度,或者在 OnRender 方法的这个 SimpleCircleAdorner 类中正确传递点。我已经在 SimpleCircleAdorner 类的 OnRender 方法中尝试了您的多边形点,

    SimpleCircleAdorner 代码:

            Point[] polylinePoints = {
                // START: @ TOP LEFT
                new Point(200.0F, 200.0F),
                // 1ST LINE: TOP RIGHT
                new Point(400.0F, 200.0F),
                // 2ND LINE: BOMTTOM RIGHT
                new Point(400.0F, 400.0F),
                // 3RD LINE: BOTTOM LEFT
                new Point(200.0F, 400.0F),
            };
            // Load from Point[Array] polylinePoints.
            PointCollection myPointCollection = new PointCollection(polylinePoints);
    
            // Draw a circle at each corner.
            drawingContext.DrawEllipse(renderBrush, renderPen, myPointCollection[0], renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, myPointCollection[1], renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, myPointCollection[2], renderRadius, renderRadius);
            drawingContext.DrawEllipse(renderBrush, renderPen, myPointCollection[3], renderRadius, renderRadius);
    

    加载事件:

            private void MyGrid_Loaded(object sender, RoutedEventArgs e)
            {
                var polygonColl = drawPolygon();
                polygonTL = polygonColl;
                canvasTL.Children.Add(polygonTL);
                var myAdornerLayer = AdornerLayer.GetAdornerLayer(canvasTL);
                myAdornerLayer.Add(new SimpleCircleAdorner(canvasTL));
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-08
      • 2013-02-06
      • 1970-01-01
      • 2019-08-30
      • 2020-02-04
      • 2011-08-11
      • 1970-01-01
      相关资源
      最近更新 更多