The new vbAccelerator Site - more VB and .NET Code and Controls

Determine every format available on the clipboard, including custom formats

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Clipboard,Interprocess Comms

Updated:

01/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


Whilst VB allows you to determine if the standard, recognised clipboard formats are on the clipboard, you can't determine if any other formats are present. So, for example, if you want to know if the clipboard contains data from Excel (which always pastes at least its own customised format called 'Wk1'), or if you want to determine whether Rich Text format is available to enable a Paste menu, you can't do it.

This tip shows how to determine all the formats currently available on the clipboard, including the customised formats.

Start a new project in VB. Add a Command button and a ListBox to the project's form, then add the following code:

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
&nbsp &nbsp [_First] = 1
&nbsp &nbsp CF_TEXT = 1
&nbsp &nbsp CF_BITMAP = 2
&nbsp &nbsp CF_METAFILEPICT = 3
&nbsp &nbsp CF_SYLK = 4
&nbsp &nbsp CF_DIF = 5
&nbsp &nbsp CF_TIFF = 6
&nbsp &nbsp CF_OEMTEXT = 7
&nbsp &nbsp CF_DIB = 8
&nbsp &nbsp CF_PALETTE = 9
&nbsp &nbsp CF_PENDATA = 10
&nbsp &nbsp CF_RIFF = 11
&nbsp &nbsp CF_WAVE = 12
&nbsp &nbsp CF_UNICODETEXT = 13
&nbsp &nbsp CF_ENHMETAFILE = 14
&nbsp &nbsp CF_HDROP = 15
&nbsp &nbsp CF_LOCALE = 16
&nbsp &nbsp CF_MAX = 17
&nbsp &nbsp [_Last] = 17
End Enum


Private Property Get FormatName( _
&nbsp &nbsp &nbsp &nbsp ByVal lFormatId As Long _
&nbsp &nbsp ) As String
' Returns the format name for a clipboard format id:
Dim lSize As Long
Dim sBuf As String
Dim lR As Long
&nbsp &nbsp
&nbsp &nbsp If (lFormatId >= EPredefinedClipboardFormatConstants.[_First] And lFormatId &nbsp &nbsp &nbsp &nbsp
' For pre-defined formats, we have to make the text
&nbsp &nbsp &nbsp &nbsp ' up ourselves:
&nbsp &nbsp &nbsp &nbsp Select Case lFormatId
&nbsp &nbsp &nbsp &nbsp Case CF_TEXT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Text"
&nbsp &nbsp &nbsp &nbsp Case CF_BITMAP
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Bitmap Picture"
&nbsp &nbsp &nbsp &nbsp Case CF_METAFILEPICT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Meta-File Picture"
&nbsp &nbsp &nbsp &nbsp Case CF_SYLK
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Microsoft Symbolic Link (SYLK) data."
&nbsp &nbsp &nbsp &nbsp Case CF_DIF
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp
FormatName = "Software Arts' Data Interchange information."
&nbsp &nbsp &nbsp &nbsp Case CF_TIFF = 6
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Tagged Image File Format (TIFF) Picture"
&nbsp &nbsp &nbsp &nbsp Case CF_OEMTEXT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Text (OEM)"
&nbsp &nbsp &nbsp &nbsp Case CF_DIB
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "DIB Bitmap Picture"
&nbsp &nbsp &nbsp &nbsp Case CF_PALETTE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Colour Palette"
&nbsp &nbsp &nbsp &nbsp Case CF_PENDATA
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Pen Data"
&nbsp &nbsp &nbsp &nbsp Case CF_RIFF
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "RIFF Audio data"
&nbsp &nbsp &nbsp &nbsp Case CF_WAVE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Wave File"
&nbsp &nbsp &nbsp &nbsp Case CF_UNICODETEXT
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Text (Unicode)"
&nbsp &nbsp &nbsp &nbsp Case CF_ENHMETAFILE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Enhanced Meta-File Picture"
&nbsp &nbsp &nbsp &nbsp Case CF_HDROP
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "File List"
&nbsp &nbsp &nbsp &nbsp Case CF_LOCALE
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = "Text Locale Identifier"
&nbsp &nbsp &nbsp &nbsp End Select
&nbsp &nbsp Else
&nbsp &nbsp &nbsp &nbsp
' For custom formats, we can ask the Clipboard for
&nbsp &nbsp &nbsp &nbsp ' the registered name:
&nbsp &nbsp &nbsp &nbsp lSize = 255
&nbsp &nbsp &nbsp &nbsp sBuf = String$(lSize, 0)
&nbsp &nbsp &nbsp &nbsp lR = GetClipboardFormatName(lFormatId, sBuf, lSize)
&nbsp &nbsp &nbsp &nbsp If (lR 0) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp FormatName = Left$(sBuf, lR)
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End If
End Property

Private Sub Command1_Click()
Dim lR As Long
Dim iCount As Long
&nbsp &nbsp
&nbsp &nbsp List1.Clear

&nbsp &nbsp If (OpenClipboard(Me.hWnd)) Then
&nbsp &nbsp &nbsp &nbsp lR = EnumClipboardFormats(0)
&nbsp &nbsp &nbsp &nbsp If (lR 0) Then
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Do
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp iCount = iCount + 1
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp List1.AddItem FormatName(lR)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp List1.ItemData(List1.NewIndex) = lR
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp lR = EnumClipboardFormats(lR)
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp Loop While lR 0
&nbsp &nbsp &nbsp &nbsp End If
&nbsp &nbsp End If
&nbsp &nbsp CloseClipboard
&nbsp &nbsp
End Sub

When you click the command button, the list box will be filled with a list of available data formats. The List field is set to the format name, and the item data of each item is set to the clipboard format's ID. To see custom formats in the list, try copying something from Word, Write or Internet Explorer before clicking the button.


&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

Copyright 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98