'我们就以font为例,看看怎么让用户修改用户控件的属性吧
我们现在来讨论一下,怎么让用户修改font属性
请加入以下代码:
Public Property Get Font() As Font
Set Font = Label1.Font'将用户控件的font属性设置为控件上label1控件的font属性
End Property
Public Property Set Font(ByVal New_Font As Font)
Set Label1.Font = New_Font'将用户修改后的font属性赋给label1.font
PropertyChanged "Font"'修改用户控件的font属性为最新的属性
End Property
ok了!用户可以在属性窗口和代码修改font了!~再添加进修改forecolor,text的属性代码,就可以完成这个用户控件了!
5. 代码全貌
现在把所有原代码都贴进来,这是一个有图片背景,支持更改text的label~
当然之前请做好控件的属性设定:
在用户控件的界面上添加一个label一个image,同时将label1置为顶层(bring to front 反正让label1压着image1就行了)
然后将label1的backstyle设为透明,usercontrol的backstyle也设为透明,image1的strech属性设置为true,然后随便为image1添加背景图片。
接着添加如下代码:
Private m_backstyle As Integer
Event click()
Private Sub Label1_Click()
RaiseEvent click
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
m_backstyle = 0
Set Label1.Font = PropBag.ReadProperty("Font", Ambient.Font)
Label1.ForeColor = PropBag.ReadProperty("ForeColor", &H80000008)
Label1.Caption = PropBag.ReadProperty("Text", "")
End Sub
Private Sub UserControl_Resize()
With Image1
.Left = 0
.Top = 0
.Height = UserControl.Height
.Width = UserControl.Width
End With
With Label1
.Left = 0
.Top = 0
.Height = UserControl.Height
.Width = UserControl.Width
.BackStyle = 0
End With
UserControl.BackStyle = 0
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("backstyle", 0, 0)
Call PropBag.WriteProperty("Font", Label1.Font, Ambient.Font)
Call PropBag.WriteProperty("ForeColor", Label1.ForeColor, &H80000008)
Call PropBag.WriteProperty("Text", Label1.Caption, "CMX")
End Sub
Public Property Get BackStyle() As Integer
BackStyle = Label1.BackStyle
End Property
Public Property Get Font() As Font
Set Font = Label1.Font
End Property
Public Property Set Font(ByVal New_Font As Font)
Set Label1.Font = New_Font
PropertyChanged "Font"
End Property
'WARNING! DO NOT REMOVE OR MODIFY THE FOLLOWING COMMENTED LINES!
'MappingInfo=TextBoxEx,TextBoxEx,-1,Text
Public Property Get Text() As String
Text = Label1.Caption
End Property
Public Property Let Text(ByVal New_Text As String)
Label1.Caption() = New_Text
PropertyChanged "Text"
End Property
Public Property Get ForeColor() As OLE_COLOR
ForeColor = Label1.ForeColor
End Property
Public Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
Label1.ForeColor() = New_ForeColor
PropertyChanged "ForeColor"
End Property