在剪贴板上使用多种格式

在同一时刻,实际上可以把几块数据放置在 Clipboard 上,只要这几块数据的格式各不相同。这一点是很有用的,因为无法知道什么样的应用程序正在粘贴数据,所以用不同格式提供数据,就能增加为其它应用程序提供可用格式的机会。其它的 Clipboard 方法— GetData、SetData GetFormat— 允许通过提供指定格式的数字,处理文本外的数据格式。这些格式与相应的数字一起,在下表进行了描述。

常数 描述
VbCFLink 动态数据交换链。
VbCFText 文本。本章前面的示例都用这一格式。
VbCFBitmap 位图。
VbCFMetafile 元文件。
VbCFDIB 与设备无关的位图。
VbCFPalette 调色板。

从图片框控件中剪切和粘贴数据时,可使用后四种格式。下列代码为使用任何标准控件,提供了通用的“剪切”、“复制”和“粘贴”命令。

Private Sub mnuCopy_Click ()
   Clipboard.Clear
   If TypeOf Screen.ActiveControl Is TextBox Then
      Clipboard.SetText Screen.ActiveControl.SelText
   ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
      Clipboard.SetText Screen.ActiveControl.Text
   ElseIf TypeOf Screen.ActiveControl Is PictureBox _
         Then
      Clipboard.SetData Screen.ActiveControl.Picture
   ElseIf TypeOf Screen.ActiveControl Is ListBox Then
      Clipboard.SetText Screen.ActiveControl.Text
   Else
      '对其它控件没有意义的动作。
   End If
End Sub

Private Sub mnuCut_Click ()
   '首先要做的与复制相同。
   mnuCopy_Click
   '现在清除活动控件的内容。
   If TypeOf Screen.ActiveControl Is TextBox Then
      Screen.ActiveControl.SelText = ""
   ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
      Screen.ActiveControl.Text = ""
   ElseIf TypeOf Screen.ActiveControl Is PictureBox _
         Then
      Screen.ActiveControl.Picture = LoadPicture()
   ElseIf TypeOf Screen.ActiveControl Is ListBox Then
      Screen.ActiveControl.RemoveItem Screen.ActiveControl.ListIndex
   Else
      '无操作响应其它控件。
   End If
End Sub

Private Sub mnuPaste_Click ()
   If TypeOf Screen.ActiveControl Is TextBox Then
      Screen.ActiveControl.SelText = Clipboard.GetText()
   ElseIf TypeOf Screen.ActiveControl Is ComboBox Then
      Screen.ActiveControl.Text = Clipboard.GetText()
   ElseIf TypeOf Screen.ActiveControl Is PictureBox _
         Then
      Screen.ActiveControl.Picture = _
         Clipboard.GetData()
   ElseIf TypeOf Screen.ActiveControl Is ListBox Then
      Screen.ActiveControl.AddItem Clipboard.GetText()
   Else
      '对其它控件没有意义的动作。
   End If
End Sub