我很难在<多边形>的角上添加装饰物。当然,我想像在每个点的角落。
此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;
}
答案 0 :(得分:0)
在您的代码中,您尝试实现了围绕多边形的自定义装饰器,并将“多边形”添加到“画布”面板。为此,您已经按照Microsoft页面中的说明使用了 SimpleCircleAdorner 类。我认为,在该类中,您已经通过了“画布”面板的默认大小。默认的requiredsize值为0点。
valid_input = False
while not valid_input:
print('Enter difficulty: E - Easy, M - Medium, H - Hard')
difficulty = input().upper()
valid_input = difficulty in ['E', 'M', 'H']
如果要在多边形周围实现自定义装饰,则可以设置画布面板的高度和宽度,或在 OnRender 方法的此 SimpleCircleAdorner 类中正确传递点。我已经在 OnRender 方法的 SimpleCircleAdorner 类中尝试过您的多边形点,
SimpleCircleAdorner代码:
Rect adornedElementRect = new Rect(this.AdornedElement.DesiredSize);
已加载事件:
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);