为什么我必须使用 UserControl.MousePointer 而不是 Me.MousePointer?
Why must I use UserControl.MousePointer instead of Me.MousePointer?
在 VB6 中的 UserControl 中,我必须使用 UserControl.MousePointer = vbDefault
而不是 Me.MousePointer = vbDefault
。我可以在表单上使用 Me.MousePointer
(而 Form.MousePointer
不起作用)。
为什么我必须使用 UserControl.MousePointer
而不是 Me.MousePointer
?
我的意思是字面上的文本 "UserControl",而不是 UserControl
作为另一个控件名称的占位符。
Me
并不是您想象的那样。它是对您使用它的模块的当前实例的引用,而不是 "magic."
要获得您想要的内容,您必须将此 属性 添加到 UserControl 的默认界面,例如:
Option Explicit
Public Property Get MousePointer() As MousePointerConstants
MousePointer = UserControl.MousePointer
End Property
Public Sub Test()
MsgBox Me.MousePointer
End Sub
在 VB6 中,表单略有不同,可能是 16 位 VB 的延续,以便更容易移植旧代码。这些似乎总是继承自一个隐藏的接口。这是在您无权访问的类型库中定义的,因为 Microsoft 并未将其作为 VB6 的一部分发布。尝试查询它通常会出现如下错误:
Cannot jump to 'MousePointer' because it is in the library 'Unknown10' which is not currently referenced
仅从这一点来看,似乎使用 Me
总是会带来很小的性能损失。在我看来,您没有直接进入模块的过程,而是通过它的默认 COM 接口。
您必须检查编译后的代码以确定是否存在性能损失,如果有,损失多少。我没有看到这个记录,否则我们只是在猜测它。
在任何情况下都没有理由永远使用Me
除非你必须为了限定某些东西。
糟糕的例子但是:
Option Explicit
Private mCharm As Long
Public Property Get Charm() As Long
Charm = mCharm
End Property
Public Property Let Charm(ByVal RHS As Long)
mCharm = RHS
'Maybe we do more here such as update the user interface or some
'other things.
End Property
Public Sub GetLucky(ByVal Charm As Long)
'Do some stuff.
Charm = Charm + Int(Rnd() * 50000)
'etc.
Me.Charm = Charm 'Here we use Me so we can assign to the property Charm.
End Sub
无论如何,这实际上是 Me
的唯一合法用途:限定所需命名空间的范围。依赖它是因为在输入时它会调出 IntelliSense 只是懒惰。
如果 Forms 是 "broken" 而不是 UserControls。
想通了。事实证明,由于 UserControl 是一个 ActiveX 控件,VB6 为您做了一些神奇的事情。对于 Form 控件,它不是 ActiveX 控件,这就是为什么 MousePointer 属性 可以通过我访问的原因,就像您期望的那样。
对于 UserControl,您在 VB6 中创建的 UserControl 控件位于另一个控件 - ActiveX 控件上。该 ActiveX 控件可通过 UserControl 访问。像这样:
class ActiveXControl
{
int MousePointer;
VBUserControl control;
}
class VBUserControl
{
}
class YourUserControl : VBUserControl
{
ActiveXControl UserControl;
// we must use UserControl.MousePointer
}
但是对于一个Form来说,更像这样:
class Form
{
int MousePointer;
}
class YourForm : Form
{
// we actually inherit from Form so we use Me.MousePointer
}
在 VB6 中的 UserControl 中,我必须使用 UserControl.MousePointer = vbDefault
而不是 Me.MousePointer = vbDefault
。我可以在表单上使用 Me.MousePointer
(而 Form.MousePointer
不起作用)。
为什么我必须使用 UserControl.MousePointer
而不是 Me.MousePointer
?
我的意思是字面上的文本 "UserControl",而不是 UserControl
作为另一个控件名称的占位符。
Me
并不是您想象的那样。它是对您使用它的模块的当前实例的引用,而不是 "magic."
要获得您想要的内容,您必须将此 属性 添加到 UserControl 的默认界面,例如:
Option Explicit
Public Property Get MousePointer() As MousePointerConstants
MousePointer = UserControl.MousePointer
End Property
Public Sub Test()
MsgBox Me.MousePointer
End Sub
在 VB6 中,表单略有不同,可能是 16 位 VB 的延续,以便更容易移植旧代码。这些似乎总是继承自一个隐藏的接口。这是在您无权访问的类型库中定义的,因为 Microsoft 并未将其作为 VB6 的一部分发布。尝试查询它通常会出现如下错误:
Cannot jump to 'MousePointer' because it is in the library 'Unknown10' which is not currently referenced
仅从这一点来看,似乎使用 Me
总是会带来很小的性能损失。在我看来,您没有直接进入模块的过程,而是通过它的默认 COM 接口。
您必须检查编译后的代码以确定是否存在性能损失,如果有,损失多少。我没有看到这个记录,否则我们只是在猜测它。
在任何情况下都没有理由永远使用Me
除非你必须为了限定某些东西。
糟糕的例子但是:
Option Explicit
Private mCharm As Long
Public Property Get Charm() As Long
Charm = mCharm
End Property
Public Property Let Charm(ByVal RHS As Long)
mCharm = RHS
'Maybe we do more here such as update the user interface or some
'other things.
End Property
Public Sub GetLucky(ByVal Charm As Long)
'Do some stuff.
Charm = Charm + Int(Rnd() * 50000)
'etc.
Me.Charm = Charm 'Here we use Me so we can assign to the property Charm.
End Sub
无论如何,这实际上是 Me
的唯一合法用途:限定所需命名空间的范围。依赖它是因为在输入时它会调出 IntelliSense 只是懒惰。
如果 Forms 是 "broken" 而不是 UserControls。
想通了。事实证明,由于 UserControl 是一个 ActiveX 控件,VB6 为您做了一些神奇的事情。对于 Form 控件,它不是 ActiveX 控件,这就是为什么 MousePointer 属性 可以通过我访问的原因,就像您期望的那样。
对于 UserControl,您在 VB6 中创建的 UserControl 控件位于另一个控件 - ActiveX 控件上。该 ActiveX 控件可通过 UserControl 访问。像这样:
class ActiveXControl
{
int MousePointer;
VBUserControl control;
}
class VBUserControl
{
}
class YourUserControl : VBUserControl
{
ActiveXControl UserControl;
// we must use UserControl.MousePointer
}
但是对于一个Form来说,更像这样:
class Form
{
int MousePointer;
}
class YourForm : Form
{
// we actually inherit from Form so we use Me.MousePointer
}