如何给vb的UserControl添加属性、方法和事件?
来源:互联网 发布:防止自动安装软件 编辑:程序博客网 时间:2024/06/05 17:26
一、属性的派生Public Property Let 属性名(ByVal NewValue As 类型名) PropertyChanged "属性名"End PropertyPublic Property Get 属性名() As 类型名End Property如果是只读的属性,缺省Let过程即可。二、方法的派生Public Sub 方法名(参数表)End SubPublic Function 方法名(参数表) As 类型名End Function三、事件的派生'申明部分Public Event 事件名(参数表)'激活部分RaiseEvent 事件名(参数表)四、UserControl自有事件InitProperties:创建对象的新实例时,发生该事件。Initialize:父对象创建UserControl时,发生该事件。Resize:大小改变时,发生该事件。ReadProperties:当加载具有保存状态的对象的旧实例时,发生该事件。 内部过程:Set 对象名= PropBag.ReadProperty("属性名", 默认值)WriteProperties:当保存对象的实例时(由PropertyChanged引发),发生该事件。 内部过程:Call PropBag.WriteProperty("属性名", 属性值, 默认值)五、UserControl固有过程(1)字体部分 Private WithEvents mFont As StdFont Private Sub UserControl_Initialize() Set mFont = New StdFont Set UserControl.Font = mFont End Sub Public Property Get Font() As StdFont Set Font = UserControl.Font End Property Private Sub mFont_FontChanged(ByVal PropertyName As String) Set UserControl.Font = mFont Refresh End Sub Public Property Set Font(mnewFont As StdFont) With mFont .Bold = mnewFont.Bold .Charset = mnewFont.Charset .Italic = mnewFont.Italic .Name = mnewFont.Name .Size = mnewFont.Size .Strikethrough = mnewFont.Strikethrough .Underline = mnewFont.Underline .Weight = mnewFont.Weight End With PropertyChanged "Font" End Property(2)光标、鼠标、句柄部分'注意!不要删除或修改下列被注释的行!'MappingInfo=UserControl,UserControl,-1,MouseIconPublic Property Get MouseIcon() As Picture Set MouseIcon = UserControl.MouseIconEnd PropertyPublic Property Set MouseIcon(ByVal New_MouseIcon As Picture) Set UserControl.MouseIcon = New_MouseIcon PropertyChanged "MouseIcon"End Property'注意!不要删除或修改下列被注释的行!'MappingInfo=UserControl,UserControl,-1,HasDCPublic Property Get HasDC() As Boolean HasDC = UserControl.HasDCEnd Property'注意!不要删除或修改下列被注释的行!'MappingInfo=UserControl,UserControl,-1,hDCPublic Property Get hDC() As Long hDC = UserControl.hDCEnd Property'注意!不要删除或修改下列被注释的行!'MappingInfo=UserControl,UserControl,-1,hWndPublic Property Get hwnd() As Long hwnd = UserControl.hwndEnd Property六、UserControl对象Public属性:决定是调用形式使用还是以ActiveX方式使用。BackStyle属性:在父对象中是否透明。