Visual Basic System Services
EnumClipboardFormats: Identify Clipboard Data Available
     
Posted:   Sunday January 15, 2006
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB6, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

DragQueryFile: Identify Files Copied to the Clipboard
     
 Prerequisites
None.

Although VB provides a number of very flexible routines for identifying data on the clipboard, the APIs provide a more extensive means to interrogate the clipboard for the data types currently available.

EnumClipboardFormats can be used when identification of the current clipboard data is required. Current clipboard data formats are stored in an ordered list. To perform an enumeration of clipboard data formats, a series of calls to EnumClipboardFormats is made; for each call, the format parameter specifies an available clipboard format, and the function returns the next available clipboard format.

Because EnumClipboardFormats enumerates formats in the order that they were placed on the clipboard, a well behaved and well designed application will add information to the clipboard in order from the most descriptive clipboard format to the least descriptive format. Similarly, a well designed application retrieves data for pasting using the first clipboard format that can be handled, as that will be the most descriptive clipboard format.

Windows performs automatic type conversions for certain clipboard formats. In the case of such a format, EnumClipboardFormats enumerates the specified format, then enumerates the formats to which it can be converted. Because such conversion is a behind-the-scene operation, there is no differentiation in the listings returned by EnumClipboardFormats for a data format actually on the clipboard and one that has been synthesised by Windows. With this conversion functionality it is therefore possible, using the clipboard APIs, to request that Windows provide data in a specific format (for example, when the data format on the clipboard is CF_DIB you could request CF_PALETTE. If the requested data is one of the supported Synthesized Clipboard Formats, Windows will convert the data to the requested format if possible, noting that some conversions are only available on later versions of Windows).

 BAS Module Code
None:

 Form Code
Add a command button (Command1), a label (Label1) and a list box (List1), along with the following code. The load event takes care of positioning:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'predefined clipboard formats
Private Const CF_TEXT As Long = 1
Private Const CF_BITMAP As Long = 2
Private Const CF_METAFILEPICT As Long = 3
Private Const CF_SYLK As Long = 4
Private Const CF_DIF As Long = 5
Private Const CF_TIFF As Long = 6
Private Const CF_OEMTEXT As Long = 7
Private Const CF_DIB As Long = 8
Private Const CF_PALETTE As Long = 9
Private Const CF_PENDATA As Long = 10
Private Const CF_RIFF As Long = 11
Private Const CF_WAVE As Long = 12
Private Const CF_UNICODETEXT As Long = 13
Private Const CF_ENHMETAFILE As Long = 14
Private Const CF_FILES As Long = 15
Private Const CF_HDROP As Long = CF_FILES
Private Const CF_LOCALE As Long = 16
Private Const CF_DIBV5 As Long = 17

Private Const CF_OWNERDISPLAY As Long = &H80
Private Const CF_DSPTEXT As Long = &H81
Private Const CF_DSPBITMAP As Long = &H82
Private Const CF_DSPMETAFILEPICT As Long = &H83
Private Const CF_DSPENHMETAFILE As Long = &H8E

Private Const ERROR_SUCCESS As Long = 0&
Private Const LB_SETTABSTOPS As Long = &H192

Private Declare Function OpenClipboard Lib "user32" _
  (ByVal hwnd As Long) As Long
   
Private Declare Function EnumClipboardFormats Lib "user32" _
  (ByVal wFormat As Long) 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 Declare Function CloseClipboard Lib "user32" () As Long

Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long

Private Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
   (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long



Private Sub Form_Load()

   ReDim TabStop(0) As Long

   TabStop(0) = 90
   
   With List1
      .Move 200, 200, 7000, 2400
      .Clear
      Call SendMessage(.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
      Call SendMessage(.hwnd, LB_SETTABSTOPS, 1, TabStop(0))
      .Refresh
   End With

   With Command1
      .Move 200, List1.Top + List1.Height + 200, 2000, 345
      .Caption = "EnumClipboardFormats"
   End With
   
   With Label1
      .Move Command1.Left + Command1.Width + 200, Command1.Top + 50
      .AutoSize = True
      .Caption = ""
   End With
   
   
   
End Sub


Private Sub Command1_Click()

   Dim fmt As Long
   
   List1.Clear
   
   If OpenClipboard(0&) <> 0 Then
      
      Do
      
        'This may appear confusing. To start an
        'enumeration of clipboard formats, set
        'format to zero. When format is zero,
        'EnumClipboardFormats retrieves the first
        'available clipboard format. For subsequent
        'calls during an enumeration the value from
        'the result of the previous EnumClipboardFormats
        'call is assigned to the format member.
         fmt = EnumClipboardFormats(fmt)
         
         If fmt > ERROR_SUCCESS Then
            List1.AddItem GetCBTypeDesc(fmt)
         End If
         
      Loop Until fmt = ERROR_SUCCESS
   
      Call CloseClipboard
   
   End If  'OpenClipboard
   
   Label1.Caption = List1.ListCount & " formats on clipboard."
   
End Sub


Private Function TrimNull(startstr As String) As String

   TrimNull = Left$(startstr, lstrlenW(StrPtr(startstr)))
   
End Function


Private Function GetCBTypeDesc(ByVal nFormat As Long) As String

   Dim cbBuff As Long
   Dim buff As String

  'descriptions of various clipboard formats
   Select Case nFormat
      Case CF_TEXT
         buff = "CF_TEXT" & vbTab & "Null-terminated plain ANSI text in global memory block"
      
      Case CF_BITMAP
         buff = "CF_BITMAP" & vbTab & "A bitmap compatible with Windows 2.x"
      
      Case CF_METAFILEPICT
         buff = "CF_METAFILEPICT" & vbTab & "Windows metafile w/additional info " & _
                "re how the metafile should be displayed"
      
      Case CF_SYLK
         buff = "CF_SYLK" & vbTab & "Symbolic Link (SYLK) - ASCII text format " & _
                "used by older MS products)"
         
      Case CF_DIF
         buff = "CF_DIF" & vbTab & "Software Art's data interchange format " & _
                "(DIF - ASCII text format)"
         
      Case CF_TIFF
         buff = "CF_TIFF" & vbTab & "Tagged Interchange File Format (TIF) data " & _
                "in a global memory block"
         
      Case CF_OEMTEXT
         buff = "CF_OEMTEXT" & vbTab & "Similar to CF_TEXT but using the OEM " & _
                "character set"
         
      Case CF_DIB
         buff = "CF_DIB" & vbTab & "Global memory block containing a Windows " & _
                "device-independent bitmap (DIB) as a BITMAPINFO " & _
                "structure followed by the bitmap bits"
         
      Case CF_PALETTE
         buff = "CF_PALETTE" & vbTab & "Color-palette handle (used in conjunction " & _
                "with CF_DIB)"
         
      Case CF_PENDATA
         buff = "CF_PENDATA" & vbTab & "Data for the pen extensions to Windows"
         
      Case CF_RIFF
         buff = "CF_RIFF" & vbTab & "Resource interchange file format (RIFF) " & _
                "data as a global memory block"
         
      Case CF_WAVE
         buff = "CF_WAVE" & vbTab & "Specific case of RIFF in which the contained " & _
                "data is a waveform (sampled sound)"
         
      Case CF_UNICODETEXT
         buff = "CF_UNICODETEXT" & vbTab & "Unicode text format, available only " & _
                "under Windows NT 4.0 or later"
         
      Case CF_ENHMETAFILE
         buff = "CF_ENHMETAFILE" & vbTab & "Handle to an enhanced metafile (HENHMETAFILE)"
         
      Case CF_FILES, CF_HDROP
         buff = "CF_FILES/CF_HDROP" & vbTab & "Handle that identifies a list " & _
                "of files, such as a set of files dragged from " & _
                "the Windows Explorer"
         
      Case CF_LOCALE
         buff = "CF_LOCALE" & vbTab & "Handle to the locale identifier associated " & _
                "with text in the clipboard"
         
      Case CF_DIBV5
         buff = "CF_DIBV5" & vbTab & "Memory object containing BITMAPV5HEADER " & _
                "structure followed by the bitmap color space " & _
                "information and the bitmap bits (2000/XP only)"
      
      Case CF_OWNERDISPLAY
         buff = "CF_OWNERDISPLAY" & vbTab & "Owner-display format. The clipboard " & _
                "owner must display and update the clipboard viewer"
      
      Case CF_DSPTEXT
         buff = "CF_DSPTEXT" & vbTab & "Text display format associated with a " & _
                "private format"

      Case CF_DSPBITMAP
         buff = "CF_DSPBITMAP" & vbTab & "Bitmap display format associated with " & _
                "a private format"
      
      Case CF_DSPMETAFILEPICT
         buff = "CF_DSPMETAFILEPICT" & vbTab & "Metafile-picture display format " & _
                "associated with a private format"
      
      Case CF_DSPENHMETAFILE
         buff = "CF_DSPENHMETAFILE" & vbTab & "Enhanced metafile display format " & _
                "associated with a private format"
        
      Case Else
      
        'try to get the data type as
        'used by the clipboard
         buff = Space$(256)
         cbBuff = Len(buff)
         
         If GetClipboardFormatName(nFormat, buff, cbBuff) = 0 Then
            buff = "Unknown" & vbTab & CStr(nFormat)
         Else
            buff = "PRIVATE" & vbTab & TrimNull(buff)
         End If

   End Select
   
   GetCBTypeDesc = buff
   
End Function
 Comments
 

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter