CText类从CGElement类继承,用于定义文本的基本属性和与交互绘制有关的方法。文本的属性包括字体名称、插入点、内容、大小、旋转角度、颜色、字体样式等。CText类提供了多个构造函数,可以根据给定的属性值和给定的文本构造CText对象。采用Graphics对象的DrawString方法绘制文本。
code.vb.net
'文本类
Public Class CText
Inherits CGElement
Private m Font As Font
Private m Pos As PointF
Private m_Content As String
Private m_Size,m_Angle As Single
Private m Color As Color
Private m_Style As FontStyle
'字体属性
Public Property Font() As Font
Get
Return m_Font
End Get
Set(ByVal Value As Font)
m_Font = Value
End Set
End Property
'位置属性
Public Property Pos() As PointF
Get
Return m_Pos"
End Get
Set(By Val Value As PointF)
m_Pos = Value
End Set
End Property
'大小属性
Public Property Size() As Single
Get
Retum m_Size
End Get
Set(By Val Value As Single)
m_Size = Value
End Set
End Property
'内容属性
Public Property Content() As String
Get
Return m_Content
End Get
Set(ByVal Value As String)
m_Content = Value
End Set
End Property
'旋转角度属性
Public Property Angle() As Single
Get
Return m_Angle
End Get
Set(ByVal Value As Single)
m_Angle = Value
End Set
End Property
'颜色属性
Public Shadows Property Color() As Color
Get
Return m_Color
End Get"
Set(ByVal Value As Color)
m_Color = Value
End Set
End Property
'字体模式
Public Shadows Property Style() As FontStyle
Get
Return m_Style
End Get
Set(By Val Value As FontStyle)
m_Style = Value
End Set
End Property
'无参构造函数
Public Sub New()
Init()
End Sub
'构造函数,用已知的字符串进行构造
Public Sub New(ByVal str As String, ByVal aPos As PointF)
Init()
m_Content = str
m_Pos = aPos
End Sub
'构造函数
Public Sub New(By Val str As String, ByVal f As Font,
ByVal size As Single,
By Val aPos As PointF, ByVal ang As Single,
ByVal c As Color, By Val sty As FontStyle)
m_Content = str
m_Font =f
m_Size = size
m_Pos = aPos
m_Angle = ang
m_Color=c
m_Style = sty
End Sub
'构造函数,用已知的文本进行构造
Public Sub New(ByVal text As CText)
With text
m_Content =.Content
m_Font =.Font
m_Pos=.Pos
m_Size =.Size
m_Angle =.Angle
m_Color =.Color
m_Style =.Style
End With
End Sub
'初始化文本
Private Shadows Sub Init(
m_Content=""
m_Size = 20
m_Font= New Font\"宋体\",m_Size,m_Style,GraphicsUnit.Pixel)
Dim format As New StringFormat0
format.Alignment = String Alignment.Near
m_Pos = New PointF(0,0)
m_Angle=0
m_Color = Color.Black
m_Style = FontStyle.Regular
End Sub
'绘文本
Public Overrides Sub Draw(ByVal g As Graphics, ByVal aDrawMode As geDrawMode)
Select Case aDrawMode
Case CGElement.geDrawMode.Normal
m_Color = Color.Black
Case CGElement.geDrawMode.Selec
m_Color = Color.Red
Case CGElement.geDrawMode.Delete
m_Color = Color. White
End Select
Dim Pos As PointF = WorldtoPage(m_Pos)
Dim sf As New StringFormat(StringFormatFlags.NoWrap)
g.DrawString(m_Content, m_Font, New SolidBrush(m_Color), Pos.X, Pos.Y, sf)
End Sub
End Class