Private Type RECT ' 定义矩形区域结构
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextW" (ByVal hDC As Long, ByVal lpStr As Long, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Const DT_CENTER As Long = &H1
Private Const DT_SINGLELINE As Long = &H2
Private Const DT_RIGHT As Long = &H6
Private Const DT_LEFT As Long = &H0
Private m_Text As String, m_Align As Integer
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
With PropBag
Caption = .ReadProperty("Caption", m_Text)
BackColor = .ReadProperty("BackColor", UserControl.BackColor)
ForeColor = .ReadProperty("ForeColor", UserControl.ForeColor)
Set Font = .ReadProperty("Font", UserControl.Font)
Visible = .ReadProperty("Visible", UserControl.BackStyle)
TextAlign = .ReadProperty("TextAlign", m_Align)
End With
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
With PropBag
.WriteProperty "Caption", m_Text
.WriteProperty "BackColor", UserControl.BackColor
.WriteProperty "ForeColor", UserControl.ForeColor
.WriteProperty "Font", UserControl.Font
.WriteProperty "Visible", UserControl.BackStyle
.WriteProperty "TextAlign", m_Align
End With
End Sub
Public Property Get Caption() As String
Caption = m_Text
End Property
Public Property Let Caption(ByVal NewStr As String)
m_Text = NewStr
UserControl.Cls
Dim lRect As RECT
With lRect
.Left = 0
.Top = 0
.Right = UserControl.ScaleWidth
.Bottom = UserControl.ScaleHeight
End With
Select Case m_Align
Case 0
DrawText UserControl.hDC, StrPtr(m_Text), Len(m_Text), lRect, DT_LEFT ' DT_CENTER Or DT_SINGLELINE
Case 1
DrawText UserControl.hDC, StrPtr(m_Text), Len(m_Text), lRect, DT_CENTER
Case 2
DrawText UserControl.hDC, StrPtr(m_Text), Len(m_Text), lRect, DT_RIGHT
End Select
PropertyChanged "Caption"
End Property
Public Property Let BackColor(ByVal newColor As OLE_COLOR)
UserControl.BackColor = newColor
PropertyChanged "BackColor"
End Property
Public Property Get BackColor() As OLE_COLOR
BackColor = UserControl.BackColor
End Property
Public Property Let ForeColor(ByVal newColor As OLE_COLOR)
' SendMessageW hDC, &H6, newColor, 0
UserControl.ForeColor = newColor
PropertyChanged "ForeColor"
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End Property
Public Property Get Visible() As Integer
Visible = UserControl.BackStyle
End Property
Public Property Let Visible(ByVal NewStyle As Integer)
UserControl.BackStyle = NewStyle
PropertyChanged "BackStyle"
End Property
Public Property Get Font() As Font
Set Font = UserControl.Font
End Property
Public Property Set Font(ByVal NewFont As StdFont)
Set UserControl.Font = NewFont
PropertyChanged "Font"
End Property
Public Property Let TextAlign(ByVal NewStyle As Integer)
m_Align = NewStyle
PropertyChanged "TextAlign"
End Property
Public Property Get TextAlign() As Integer
TextAlign = m_Align
End Property