说三道四技术文摘-感悟人生的经典句子
说三道四 > 文档快照

VB判断剪贴板的所有可用格式

HTML文档下载 WORD文档下载 PDF文档下载
VB判断剪贴板的所有可用格式
文章作者: 王行骏编译
文章内容: 在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、写字板或Internet Explorer中复制一些东西。


备案号:鲁ICP备13029499号-2 说三道四 www.s3d4.cn 说三道四技术文摘