适用于 Visual Basic 6.0 用户的 Screen 对象

更新:2007 年 11 月

Visual Basic 6.0 的 Screen 对象在 Visual Basic 2008 中没有直接等效项,但是可以使用 .NET Framework 重现它的大部分功能。

概念差异

在 Visual Basic 6.0 中,Screen 对象提供对应用程序中的活动窗体和控件的访问,提供有关正在显示应用程序的屏幕的信息,并且允许控制光标的外观。

在 Visual Basic 2008 中,没有对应于 Screen 对象的直接等效项,但是可以使用 .NET Framework 重现它的大部分功能。

说明:

Visual Basic 2008 具有一个 Screen 属性 — My.Computer.Screen。与 Visual Basic 6.0 Screen 对象不同,My.Computer.Screen 仅返回有关屏幕的只读信息,如它的设备名称、工作区和颜色深度。有关更多信息,请参见 My.Computer.Screen 属性

ActiveControl 属性

在 Visual Basic 6.0 中,Screen 对象的 ActiveControl 属性用于确定拥有焦点的控件。ActiveControl 属性可用于全局功能中,例如,用于 Screen.ActiveControl 中,此时将返回当前选定窗体上的活动控件。如果引用了特定窗体(例如,Form2.ActiveControl),则 ActiveControl 在引用窗体为活动时指定将拥有焦点的控件。

在 Visual Basic 2008 中,不再有全局 ActiveControl 属性;窗体的每一个实例都具有它自己的 ActiveControl 属性。当引用特定的窗体时,该属性的工作方式与它在 Visual Basic 6.0 中的完全相同。若要确定当前选定窗体上的活动控件,必须先循环访问 OpenForms 集合并检查 ContainsFocus 属性,确定哪一个窗体是活动的。

ActiveForm 属性

在 Visual Basic 6.0 中,Screen 对象的 ActiveForm 属性用于确定当前哪一个窗体拥有焦点。如果 MDI 父窗体拥有焦点,则 ActiveForm 返回最近一次拥有焦点的 MDI 子窗体。

在 Visual Basic 2008 中,不再有全局 ActiveForm 属性。若要确定活动窗体,必须循环访问 OpenForms 集合并查找其 ContainsFocus 属性设置为 True 的窗体。

Visual Basic 2008 MDI 父窗体(IsMDIContainer 设置为 True 的任何窗体)具有一个 ActiveMDIChild 属性,它可用于返回活动子窗体而不必使用 OpenForms 集合。

MousePointer 属性

在 Visual Basic 6.0 中,Screen 对象的 MousePointer 属性用于更改光标的外观;设置之后它将应用于应用程序中的所有窗体。

在 Visual Basic 2008 中,不再有全局 MousePointer 属性;每个窗体都具有一个 Cursor 属性,它可用于仅更改该窗体的光标外观。

TwipsPerPixel 属性

在 Visual Basic 6.0 中,Screen 对象的 TwipsPerPixelX 和 TwipsPerPixelY 属性用于将屏幕度量从逻辑缇(Visual Basic 6.0 中的标准度量单位)转换为像素。

在 Visual Basic 2008 中,像素是标准度量单位;不再需要任何转换。

用于 Screen 对象的代码更改

下面的示例演示 Visual Basic 6.0 与 Visual Basic 2008 在编码方法方面的差异。

用于确定活动控件的代码更改

下面的代码演示如何从当前选定窗体上的当前选定控件将文本复制到剪贴板。

' Visual Basic 6.0
If TypeOf Screen.ActiveControl Is TextBox Then
    Clipboard.SetText Screen.ActiveControl.Text
End If
' Visual Basic
Dim i As Integer
For i = 0 To My.Application.OpenForms.Count - 1
  If My.Application.OpenForms.Item(i).ContainsFocus Then
    If TypeOf (My.Application.OpenForms.Item(i).ActiveControl) _
      Is TextBox Then
        My.Computer.Clipboard.SetText(My.Application.OpenForms. _
          Item(i).ActiveControl.Text)
    End If
  End If
Next

用于确定活动窗体的代码更改

下面的代码演示如何更改当前选中窗体的标题。

' Visual Basic 6.0
Screen.ActiveForm.Caption = "This is the selected form"
' Visual Basic
Dim i As Integer
For i = 0 To My.Application.OpenForms.Count - 1
  If My.Application.OpenForms.Item(i).ContainsFocus Then
    My.Application.OpenForms.Item(i).Text = _
      "This is the selected form"
  End If
Next

用于确定 MDI 应用程序中活动窗体的代码更改

下面的代码演示如何更改当前选中的 MDI 子窗体的标题。

' Visual Basic 6.0
Screen.ActiveForm.Caption = "This is the selected child form"
' Visual Basic
Me.ActiveMdiChild.Text = "This is the selected child form"

Screen 对象属性的等效项

下表列出了 Visual Basic 6.0 的属性及其在 Visual Basic 2008 中的等效项。根据需要提供了解释行为差异的主题链接。如果 Visual Basic 2008 中没有直接等效项,则提供指向介绍其他替代项的主题的链接。

属性

Visual Basic 6.0

Visual Basic 2008等效项

ActiveControl

My.Application.OpenForms(0).ActiveControl

ActiveForm

My.Application.OpenForms(0).ContainsFocus 或

ActiveMdiChild(MDI 应用程序)

FontCount

Fonts

新的实现。枚举字体的行为有所不同。有关更多信息,请参见 字体处理(针对 Visual Basic 6.0 用户)

Height

My.Computer.Screen.Bounds.Height

MouseIcon

新的实现。有关更多信息,请参见无法设置自定义 MousePointer

MousePointer

System.Windows.Forms.Cursor

TwipsPerPixelX

TwipsPerPixelY

新的实现。在 Visual Basic 2008 中,坐标以像素为单位;缇不用作度量单位。

Width

My.Computer.Screen.Bounds.Width

升级说明

当 Visual Basic 6.0 应用程序升级到 Visual Basic 2008 时,Screen 对象的任何属性都会升级到各自的 Visual Basic 2008 等效项。在可能存在行为差异的情况下,向代码中插入升级注释。

请参见

概念

App 对象(针对 Visual Basic 6.0 用户)

字体处理(针对 Visual Basic 6.0 用户)

参考

My.Application.OpenForms 属性

My.Computer.Screen 属性