[非高手勿入] 用VB编写OCX控件,如何解决对象的永久存储问题?

printer 2003-02-26 12:36:54
众所周知,VB中的PropertyBag对象可以提供对用户自定义控件的属性存储功能,并且,帮助文挡也指出,存取值可以为对象,但仅限于方法。

原文如下
The ReadProperty method of the PropertyBag object is used to read a value for a property, while the WriteProperty method of the PropertyBag object is used to write a value of a property. The value of a property can itself be an object; in that case the PropertyBag object will attempt to save it. (PropertyBag Object)

在实际应用中发现,试图存取一个对象时,产生运行时错误:非法调用,控件不支持永久存储。但是,当试图采用Content属性存储二进制数据时,不能适用于UserControl 和 UserDocument 中的 PropertyBag对象。请参阅帮助文档

再看COM原理的时候,COM规范曾提到过永久存储的概念,而且也讲了不少,但是那都是用VC++写的代码。

我主要是想实现图片的存储,自定一个控件,写上一个ProperPage,编辑时通过属性页浏览图片,然后设法保存,待以后程序运行时就不用设置了。以前用过内置ImageList控件,但总觉得不是一个好主意,起码,应用程序必须引用该控件,起码,它不能编译到我的OCX中,它应该是属于COM中的聚合和包容概念。

然而,Visual C++ 中的一些MFC类提供序列化方法,这其实也算是一种永久存储的范畴,但却不能算是COM的办法。

不知道大家是否遇到过如此的困挠,还望大家一起讨论。
...全文
41 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
kensu 2003-02-26
  • 打赏
  • 举报
回复
如果你只是用来存储图片的,你可以放置图片框的数组(维数决定于你需要的
图片属性数量,并且设置他们为不可见的)

假定你有两个需要设置图片的属性如下
'----------------属性1--------------------------
Public Property Get backgroundPicture() As Picture
Set backgroundPicture = Picture1(0).Picture
End Property

Public Property Set backgroundPicture(ByVal New_backgroundPicture As Picture)
Set Picture1(0).Picture = New_backgroundPicture
PropertyChanged "backgroundPicture"
End Property
'----------------属性2--------------------------
Public Property Get ForegroundPicture() As Picture
Set ForegroundPicture = Picture1(1).Picture
End Property

Public Property Set ForegroundPicture(ByVal New_ForegroundPicture As Picture)
Set Picture1(1).Picture = New_ForegroundPicture
PropertyChanged "ForegroundPicture"
End Property


'从存贮器中加载属性值
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set Picture1(0).Picture = PropBag.ReadProperty("backgroundPicture", Nothing)
Set Picture1(1).Picture = PropBag.ReadProperty("ForegroundPicture", Nothing)
End Sub

'将属性值写到存储器
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("backgroundPicture", Picture1(0).Picture, Nothing)
Call PropBag.WriteProperty("ForegroundPicture", Picture1(1).Picture, Nothing)
End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧