本示例演示如何覆盖 Panel 的 OnRender 方法以便将自定义图形效果添加到布局元素中。
示例
使用 OnRender 方法以便将图形效果添加到呈现的面板元素中。 例如,可以使用此方法添加自定义边框或背景效果。 DrawingContext 对象作为参数传递,该参数可以为绘制形状、文本、图像或视频提供方法。 因此,此方法对于面板对象的自定义项很有帮助。
' Override the OnRender call to add a Background and Border to the OffSetPanel
Protected Overrides Sub OnRender(ByVal dc As DrawingContext)
Dim mySolidColorBrush As New SolidColorBrush()
mySolidColorBrush.Color = Colors.LimeGreen
Dim myPen As New Pen(Brushes.Blue, 10)
Dim myRect As New Rect(0, 0, 500, 500)
dc.DrawRectangle(mySolidColorBrush, myPen, myRect)
End Sub
// Override the OnRender call to add a Background and Border to the OffSetPanel
protected override void OnRender(DrawingContext dc)
{
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Colors.LimeGreen;
Pen myPen = new Pen(Brushes.Blue, 10);
Rect myRect = new Rect(0, 0, 500, 500);
dc.DrawRectangle(mySolidColorBrush, myPen, myRect);
}