VB.NET设计旋转文本控件

VB.NET设计旋转文本控件

代码如下:

 1 Public Class TextRotator
 2   Inherits System.Windows.Forms.Control
 3 
 4   Private msngRotationAngle As Single = 0
 5 
 6   Public Property RotationAngle() As Single
 7     Get
 8       Return msngRotationAngle
 9     End Get
10     Set(ByVal Value As Single)
11       If (msngRotationAngle >= -180) And (msngRotationAngle <= 180) Then
12         msngRotationAngle = Value
13         Me.Invalidate() ' Forces a repaint of the window
14         ' This is a method of the Control class
15       Else
16         ' Should raise an error here
17       End If
18     End Set
19   End Property
20 
21 
22 
23 #Region " Windows Form Designer generated code "
24 
25   Public Sub New()
26     MyBase.New()
27 
28     'This call is required by the Windows Form Designer.
29     InitializeComponent()
30 
31     'Add any initialization after the InitializeComponent() call
32 
33   End Sub
34 
35   'UserControl1 overrides dispose to clean up the component list.
36   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
37     If disposing Then
38       If Not (components Is Nothing) Then
39         components.Dispose()
40       End If
41     End If
42     MyBase.Dispose(disposing)
43   End Sub
44 
45   'Required by the Windows Form Designer
46   Private components As System.ComponentModel.IContainer
47 
48   'NOTE: The following procedure is required by the Windows Form Designer
49   'It can be modified using the Windows Form Designer.  
50   'Do not modify it using the code editor.
51   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
52     components = New System.ComponentModel.Container()
53   End Sub
54 
55 #End Region
56 
57   Private Sub TextRotator_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
58     ' Declare a brush, a font, and a graphics
59     ' object to use.
60     Dim bshBrush As Brush
61     Dim fntFont As Font
62 
63     ' We can get the graphic object directly from
64     ' the PaintEventArgs parameter of the event.
65     Dim grfGraphics As Graphics = e.Graphics
66 
67     ' Fix up the brush and the font.
68     bshBrush = New SolidBrush(Me.ForeColor)
69     fntFont = Me.Font
70 
71     ' Need to see how far to shift the drawing of the text
72     ' so that it becomes visible. This calculation can be very
73     ' complex, but we will simplify it for the example by making
74     ' the starting point the center of the window for the control. 
75     ' (Code below assumes Option Strict is Off).
76     Dim ShiftHorizontal As Single
77     Dim ShiftVertical As Single
78     ShiftHorizontal = Me.Size.Width * 0.5
79     ShiftVertical = Me.Size.Height * 0.5
80 
81     ' Now draw some rotated text.
82     grfGraphics.RotateTransform(RotationAngle)
83     grfGraphics.DrawString(Me.Text, fntFont, bshBrush, _
84                            ShiftHorizontal, ShiftVertical)
85     grfGraphics.ResetTransform()
86 
87   End Sub
88 
89   Private Sub TextRotator_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.TextChanged
90     Me.Invalidate()
91   End Sub
92 
93 
94 End Class