ITPub博客

首页 > IT职业 > IT生活 > 判别剪贴板上所有可用的格式 (转)

判别剪贴板上所有可用的格式 (转)

原创 IT生活 作者:worldblog 时间:2007-12-03 11:11:17 0 删除 编辑
判别剪贴板上所有可用的格式 (转)[@more@]判别剪贴板上所有可用的格式,包括自定义格式

  在VB允许你判别标准的、认可的格式是否在剪贴板上的同时,你无法判别是否有其它格式出现。所以,举例来说,如果你想知道剪贴板上是否包含来自excel的数据(which always pastes at least its own customised format called 'Wk1'),或者你想判别Rich Text格式是否可用以激活粘贴菜单时,你无法办到。
  这则技巧告诉你如何判别当前剪贴板上所有可用的格式,包括自定义格式。
  开始一个新项目,在窗体上放置一个Command和一个ListBox,然后加入下列代码:

Private Declare Function CountClipboardFormats Lib "USER32" () As Long
Private Declare Function EnumClipboardFormats Lib "USER32" (ByVal wFormat As Long) As Long
Private Declare Function OpenClipboard Lib "USER32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "USER32" () As Long
Private Declare Function GetClipboardFormatName Lib "USER32" Alias "GetClipboardFormatNameA" (ByVal wFormat As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Long
Private Enum EPredefinedClipboardFormatConstants
  [_First] = 1
  CF_TEXT = 1
  CF_BITMAP = 2
  CF_METAFILEPICT = 3
  CF_SYLK = 4
  CF_DIF = 5
  CF_TIFF = 6
  CF_OEMTEXT = 7
  CF_DIB = 8
  CF_PALETTE = 9
  CF_PENDATA = 10
  CF_RIFF = 11
  CF_WAVE = 12
  CF_UNICODETEXT = 13
  CF_ENHMETAFILE = 14
  CF_HDROP = 15
  CF_LOCALE = 16
  CF_MAX = 17
  [_Last] = 17
End Enum

Private Property Get FormatName(ByVal lFormatId As Long) As String
'返回一个见剪贴板格式id的格式名:
Dim lSize As Long
Dim sBuf As String
Dim lR As Long
  
  If (lFormatId >= EPredefinedClipboardFormatConstants.[_First] And _
 lFormatId <= EPredefinedClipboardFormatConstants.[_Last]) Then
    '对于预先定义的已知格式,我们自己给出格式名:
    select Case lFormatId
    Case CF_TEXT
      FormatName = "Text"
    Case CF_BITMAP
      FormatName = "Bitmap Picture"
    Case CF_METAFILEPICT
      FormatName = "Meta-File Picture"
    Case CF_SYLK
      FormatName = "microsoft Symbolic Link (SYLK) data."
    Case CF_DIF
      FormatName = "Software Arts' Data Interchange information."
    Case CF_TIFF = 6
      FormatName = "Tagged Image File Format (TIFF) Picture"
    Case CF_OEMTEXT
      FormatName = "Text (OEM)"
    Case CF_DIB
      FormatName = "DIB Bitmap Picture"
    Case CF_PALETTE
      FormatName = "Colour Palette"
    Case CF_PENDATA
      FormatName = "Pen Data"
    Case CF_RIFF
      FormatName = "RIFF Audio data"
    Case CF_WAVE
      FormatName = "Wave File"
    Case CF_UNICODETEXT
      FormatName = "Text (Unicode)"
    Case CF_ENHMETAFILE
      FormatName = "Enhanced Meta-File Picture"
    Case CF_HDROP
      FormatName = "File List"
    Case CF_LOCALE
      FormatName = "Text Locale identifier"
    End Select
  Else
    '对于自定义格式,我们可以向剪贴板要注册过的名字:
    lSize = 255
    sBuf = String$(lSize, 0)
    lR = GetClipboardFormatName(lFormatId, sBuf, lSize)
    If (lR <> 0) Then
      FormatName = Left$(sBuf, lR)
    End If
  End If
End Property

Private Sub Command1_Click()
Dim lR As Long
Dim iCount As Long
  
  List1.Clear
  If (OpenClipboard(Me.hWnd)) Then
    lR = EnumClipboardFormats(0)
    If (lR <> 0) Then
      Do
        iCount = iCount + 1
        List1.AddItem FormatName(lR)
        List1.ItemData(List1.NewIndex) = lR
        lR = EnumClipboardFormats(lR)
      Loop While lR <> 0
    End If
  End If
  CloseClipboard
End Sub

  当你点击Command后,ListBox将被可用的数据格式列表填充。列表区被设为格式名,每一格的数据被设为剪贴板格式的ID。要在列表中看到自定义格式,试试在点击Command之前,从word、写字板或Inte.NET Explorer中复制一些东西。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-987610/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6241
  • 访问量
    2410784