Note__:这可能是在黑暗中拍摄的,这完全是出于好奇。
当使用来自(mscomctl.ocx)的mscomctl.ocx控件时,我发现VB6生成的FRM代码不解析为真实的属性/方法名称,我很好奇解析是如何产生的。下面给出了一个生成的FRM代码的示例,其中包含了一个包含3个图像的ImageList:
Begin MSComctlLib.ImageList ImageList1
BackColor = -2147483643
ImageWidth = 100
ImageHeight = 45
MaskColor = 12632256
BeginProperty Images {2C247F25-8591-11D1-B16A-00C0F0283628}
NumListImages = 3
BeginProperty ListImage1 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "Form1.frx":0054
Key = ""
EndProperty
BeginProperty ListImage2 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "Form1.frx":3562
Key = ""
EndProperty
BeginProperty ListImage3 {2C247F27-8591-11D1-B16A-00C0F0283628}
Picture = "Form1.frx":6A70
Key = ""
EndProperty
EndProperty
End根据我的经验,BeginProperty标记通常意味着要分配复合属性(对象),例如,大多数控件的Font对象:
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 10950
ClientLeft = 60
ClientTop = 450
ClientWidth = 7215
BeginProperty Font
Name = "MS Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = -1 'True
Strikethrough = 0 'False
EndProperty
End可以很容易地看到解析为VB.Form.Font..。
在ImageList中,没有所谓的图像属性。与属性映像关联的GUID指示实现接口ListImages的类型IImages。这种类型是有意义的,因为ImageList控件有一个名为ListImages的属性,该属性的类型为IImages。其次,属性ListImage1、ListImage2和ListImage3不存在于IImages类型上,但是与这些属性相关联的GUID表示实现接口IImage的ListImage类型。这种类型也有意义,因为IImages实际上是IImage的集合。
对我来说没有意义的是VB6是如何建立这些关联的。VB6如何知道仅仅因为一个关联类型(由GUID提供)而使名称图像-> ListImages之间的关联--也许是因为它是该类型的唯一属性?其次,它如何将ListImage1、ListImage2和ListImage3解析为对集合IImages的添加,以及它是否使用Add方法?或者可能是ControlDefault属性?
也许VB6对此控件有特定的知识,并且不存在逻辑解析?
发布于 2010-03-20 12:41:50
你可以看到这个精心设计的例子是怎么回事。从ActiveX项目开始,添加Class1并将其标记为Persistable = 1
' Class1
Option Explicit
Private m_sText As String
Property Get Text() As String
Text = m_sText
End Property
Property Let Text(sValue As String)
m_sText = sValue
End Property
Private Sub Class_ReadProperties(PropBag As PropertyBag)
With PropBag
m_sText = .ReadProperty("txt", "")
End With
End Sub
Private Sub Class_WriteProperties(PropBag As PropertyBag)
With PropBag
.WriteProperty "txt", m_sText, ""
End With
End Sub添加UserControl1
' UserControl1
Option Explicit
Private m_oData As Class1
Property Get Data() As Class1
Set Data = m_oData
End Property
Private Sub UserControl_Initialize()
Set m_oData = New Class1
m_oData.Text = "this is a test"
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
With PropBag
Set m_oData = .ReadProperty("rs", Nothing)
End With
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
With PropBag
.WriteProperty "rs", m_oData, Nothing
End With
End Sub添加Form1并在其上放置一个UserControl1作为保存。您可以为Sub添加Module1
' Module1
Sub Main()
With New Form1
.Show
End With
End Sub这是我的Form1.frm文件
VERSION 5.00
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 2400
ClientLeft = 48
ClientTop = 432
ClientWidth = 3744
LinkTopic = "Form1"
ScaleHeight = 2400
ScaleWidth = 3744
StartUpPosition = 3 'Windows Default
Begin Project1.UserControl1 UserControl11
Height = 516
Left = 924
TabIndex = 0
Top = 588
Width = 1020
_ExtentX = 1799
_ExtentY = 910
BeginProperty rs {326250A4-CA0D-4F88-8F20-DAA391CF8E79}
txt = "this is a test"
EndProperty
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit因此,UserControl1确定对象m_oData在其WriteProperty重载中作为属性rs持久化。Class1确定它的m_sText成员变量(或Text公共属性)在frm传递的IPropertyBag中作为txt成员持久化。没有任何东西需要公共属性名称来匹配内部属性包名称。我个人使用短if只是为了尽量减少臃肿(如果可能的话,使用VB6 )。
发布于 2010-03-19 19:26:35
我猜想GUID ({2C247F25-8591-11D1-B16A-00C0F0283628})指向相关的ImageList控件和ListImage1、ListImage2等。就是要枚举所有的图像。
这有点像WPF相关控件的早期版本(例如,TextBox可以引用其封闭网格以进行放置)。
https://stackoverflow.com/questions/2478420
复制相似问题