【发布时间】:2023-03-12 20:25:02
【问题描述】:
我需要构建一个像这样的自定义 WPF 控件
由于我是 WPF 新手,所以我使用了以下代码(对于 VB.NET 感到抱歉)
Public Class TextPlaceholder
Inherits System.Windows.Controls.Label
Const CustomBorderWidth As Integer = 2
Public Sub New()
MyBase.New()
Me.BorderBrush = SystemColors.ActiveBorderBrush
End Sub
Protected Overrides Sub OnRender(drawingContext As System.Windows.Media.DrawingContext)
MyBase.OnRender(drawingContext)
Dim pointTopLeft As New Point(-1, -1)
Dim pointTopRight As New Point(Me.ActualWidth, -1)
Dim pointBottomLeft As New Point(-1, Me.ActualHeight)
Dim pointBottomRight As New Point(Me.ActualWidth, Me.ActualHeight)
Dim myPen As New Pen(Me.BorderBrush, CustomBorderWidth)
drawingContext.DrawLine(myPen, pointTopLeft, New Point(pointTopLeft.X + 5, pointTopLeft.Y))
drawingContext.DrawLine(myPen, pointTopLeft, New Point(pointTopLeft.X, pointTopLeft.Y + 5))
drawingContext.DrawLine(myPen, pointTopRight, New Point(pointTopRight.X - 5, pointTopRight.Y))
drawingContext.DrawLine(myPen, pointTopRight, New Point(pointTopRight.X, pointTopRight.Y + 5))
drawingContext.DrawLine(myPen, pointBottomLeft, New Point(pointBottomLeft.X + 5, pointBottomLeft.Y))
drawingContext.DrawLine(myPen, pointBottomLeft, New Point(pointBottomLeft.X, pointBottomLeft.Y - 5))
drawingContext.DrawLine(myPen, pointBottomRight, New Point(pointBottomRight.X - 5, pointBottomRight.Y))
drawingContext.DrawLine(myPen, pointBottomRight, New Point(pointBottomRight.X, pointBottomRight.Y - 5))
End Sub
End Class
现在
1) 考虑到我将继承该控件并且需要在继承的控件上具有相同的边框,这是最好的方法吗
2) 是否可以像我一样为 BorderBrush 指定默认值(不透明)?
3) 为什么我的角移动了一个像素(不是真正正确链接)?
【问题讨论】:
标签: .net wpf vb.net custom-controls paintcomponent