|
ありがとうございます。
後ほど、いろいろと試してみたいと思います。
OnPaintの方でできるだけ高速化する方法を模索しているのですが
以下のコードで
Public Class RotatingLabel
Inherits System.Windows.Forms.Label
Private m_RotateAngle As Integer = 3
Private m_NewText As String = "" 'String.Empty
Public Property RotateAngle As Integer
Get
Return CInt(Math.Min(360, CUInt(m_RotateAngle)))
End Get
Set(ByVal value As Integer)
m_RotateAngle = Math.Min(360, CInt(value))
Invalidate()
End Set
End Property
Public Property NewText As String
Get
Return m_NewText
End Get
Set(ByVal value As String)
m_NewText = value
Invalidate()
End Set
End Property
' Dim InitBool As Boolean = False
Dim horizShift As Integer = 0
Dim vertShift As Integer = 0
Private Sub SetRotation(g As Graphics)
Dim DegToRad As Func(Of Double, Double) = Function(angle) Math.PI * angle / 180.0
Dim size As SizeF = g.MeasureString(Me.NewText, Me.Font, Me.Parent.Width)
Dim normalAngle As Integer = CInt(((RotateAngle Mod 360) + 360) Mod 360)
Dim normaleRads As Double = DegToRad(normalAngle)
Dim hSinTheta As Integer = CInt(Math.Ceiling((size.Height * Math.Sin(normaleRads))))
Dim wCosTheta As Integer = CInt(Math.Ceiling((size.Width * Math.Cos(normaleRads))))
Dim wSinTheta As Integer = CInt(Math.Ceiling((size.Width * Math.Sin(normaleRads))))
Dim hCosTheta As Integer = CInt(Math.Ceiling((size.Height * Math.Cos(normaleRads))))
Dim rotatedWidth As Integer = Math.Abs(hSinTheta) + Math.Abs(wCosTheta)
Dim rotatedHeight As Integer = Math.Abs(wSinTheta) + Math.Abs(hCosTheta)
Me.MinimumSize = New Size(rotatedWidth, 0)
Me.MaximumSize = New Size(rotatedWidth, rotatedHeight)
Me.Size = Me.MaximumSize
Dim numQuadrants As Integer = If((normalAngle >= 0 AndAlso normalAngle < 90), 1,
If((normalAngle >= 90 AndAlso normalAngle < 180), 2,
If((normalAngle >= 180 AndAlso normalAngle < 270), 3,
If((normalAngle >= 270 AndAlso normalAngle < 360), 4, 0))))
If numQuadrants = 1 Then
horizShift = Math.Abs(hSinTheta)
ElseIf numQuadrants = 2 Then
horizShift = rotatedWidth
vertShift = Math.Abs(hCosTheta)
ElseIf numQuadrants = 3 Then
horizShift = Math.Abs(wCosTheta)
vertShift = rotatedHeight
ElseIf numQuadrants = 4 Then
vertShift = Math.Abs(wSinTheta)
End If
' g.Dispose()
End Sub
Dim InitBool As Boolean = False
Public Sub New()
' InitBool = True
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
If InitBool = False Then
Call SetRotation(g)
End If
g.TranslateTransform(horizShift, vertShift)
g.RotateTransform(Me.RotateAngle)
Dim b As Brush = New SolidBrush(Me.ForeColor)
g.DrawString(Me.NewText, Me.Font, b, 0F, 0F)
b.Dispose()
MyBase.OnPaint(e)
End Sub
End Class
|