Vb6 为什么我必须使用UserControl.MousePointer而不是Me.MousePointer?

Vb6 为什么我必须使用UserControl.MousePointer而不是Me.MousePointer?,vb6,Vb6,在用户控件的VB6中,我必须使用UserControl.MousePointer=vbDefault而不是Me.MousePointer=vbDefault。我可以在表单上使用Me.MousePointer(并且Form.MousePointer不起作用) 为什么我必须使用UserControl.MousePointer而不是Me.MousePointer 我的意思是字面上的文本“UserControl”,而不是UserControl作为另一个控件名称的占位符。Me并不是你想象的那样。它是对使用

在用户控件的VB6中,我必须使用
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的一部分发布。尝试查询时通常会出现如下错误:

无法跳转到“鼠标指针”,因为它位于当前未引用的库“Unknown10”中

仅从这一点来看,似乎使用
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
的唯一合法用途:作用域到所需的名称空间。依靠它,因为在打字时它会带来智能感知,这是懒惰的


如果有什么表单被“破坏”了,不是用户控件。

解决了它。事实证明,由于UserControl是ActiveX控件,VB6为您带来了一些魔力。对于表单控件,它不是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
}
class Form
{
    int MousePointer;
}
class YourForm : Form
{
    // we actually inherit from Form so we use Me.MousePointer
}
但对于一种形式,它更像这样:

class ActiveXControl
{
    int MousePointer;
    VBUserControl control;
}
class VBUserControl
{
}
class YourUserControl : VBUserControl
{
    ActiveXControl UserControl;
    // we must use UserControl.MousePointer
}
class Form
{
    int MousePointer;
}
class YourForm : Form
{
    // we actually inherit from Form so we use Me.MousePointer
}

是否在UserControl中定义了
Me
文本?Me具体指的是当前表单。我相信这里也有一个屏幕。鼠标指针,但我可能错了。每一个都在相应的级别上专门控制鼠标指针。您的control.MousePointer将覆盖me(form)MousePointer,这将覆盖屏幕MousePointer。如果您的用户控件是文本框,您可能希望光标在其上移动时变为文本指针,但在离开时变回表单的默认值,等等。这就是您要问的吗?不完全是。我正在创建一个UserControl ocx,但要设置鼠标指针(以及其他一些属性),而不是执行Me.SomeProperty,对于某些属性(如鼠标指针),我必须执行UserControl.SomeProperty。我的意思是字面上的“UserControl”,不是控件的名称,而是实际的文本“UserControl”。但是如果我创建一个表单,那么我不做Form.MousePointer,我做Me.MousePointer(我是表单对象)。在我正在创建的UserControl对象上,Me是UserControl,因此我希望能够执行Me.MousePointer,但我不能,也不知道为什么。