Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleKeep track of what's on the clipboard
Keywordsclipboard, clip board, copy, cut paste
CategoriesSoftware Engineering, Tips and Tricks
 
In a timer's Timer event handler, use the Clipboard object's GetFormat, GetText, and GetData methods to see what data is available and display it.
 
' Initialize the format values.
Private Sub Form_Load()
    ReDim FormatValue(0 To lblFormat.ubound)
    FormatValue(0) = vbCFLink
    FormatValue(1) = vbCFText
    FormatValue(2) = vbCFBitmap
    FormatValue(3) = vbCFMetafile
    FormatValue(4) = vbCFDIB
    FormatValue(5) = vbCFPalette
    FormatValue(6) = vbCFRTF
    FormatValue(7) = vbCFFiles
End Sub

' See which formats are available.
Private Sub tmrCheckClip_Timer()
Dim i As Integer
Dim files() As String

    For i = 0 To lblFormat.ubound
        If Clipboard.GetFormat(FormatValue(i)) Then
            lblFormat(i).ForeColor = vbBlack
        Else
            lblFormat(i).ForeColor = &H808080
        End If
    Next i

    ' Check for text.
    If Clipboard.GetFormat(vbCFText) Then
        txtClipboardText.Text = Clipboard.GetText
        txtClipboardText.Visible = True
    Else
        txtClipboardText.Visible = False
    End If

    ' Check for a bitmap.
    If Clipboard.GetFormat(vbCFBitmap) Then
        picBitmap.Picture = Clipboard.GetData(vbCFBitmap)
        picBitmap.Visible = True
    Else
        picBitmap.Visible = False
    End If

    ' Check for files.
    If Clipboard.GetFormat(vbCFFiles) Then
        lstFiles.Clear
        files = ClipboardGetFiles()
        For i = LBound(files) To UBound(files)
            lstFiles.AddItem files(i)
        Next i
        lstFiles.Visible = True
    Else
        lstFiles.Visible = False
    End If
End Sub
 
Use the OpenClipboard, GetClipboardData, and CloseClipboard API functions to read file lists selected by Windows Explorer.
 
' Get an array of the files listed in the clipboard.
Public Function ClipboardGetFiles() As String()
Dim drop_handle As Long
Dim num_file_names As Long
Dim file_names() As String
Dim file_name As String * 1024
Dim i As Long

    ' Make sure there is file data.
    If Clipboard.GetFormat(vbCFFiles) Then
        ' File data exists. Get it.
        ' Open the clipboard.
        If OpenClipboard(0) Then
            ' The clipboard is open.

            ' Get the handle to the dropped list of files.
            drop_handle = GetClipboardData(CF_HDROP)

            ' Get the number of dropped files.
            num_file_names = DragQueryFile(drop_handle, -1, _
                vbNullString, 0)

            ' Get the file names.
            ReDim file_names(1 To num_file_names) As String
            For i = 1 To num_file_names
                ' Get the file name.
                DragQueryFile drop_handle, i - 1, _
                    file_name, Len(file_name)

                ' Truncate at the NULL character.
                file_names(i) = Left$(file_name, _
                    InStr(file_name, vbNullChar) - 1)
            Next

            ' Close the clipboard.
            CloseClipboard

            ' Assign the return value.
            ClipboardGetFiles = file_names
        End If
    End If
End Function

' Copy the file names into the clipboard.
' Return True if we succeed.
Public Function ClipboardSetFiles(file_names() As String) _
    As Boolean
Dim file_string As String
Dim drop_files As DROPFILES
Dim memory_handle As Long
Dim memory_pointer As Long
Dim i As Long

    ' Clear the clipboard.
    Clipboard.Clear

    ' Open the clipboard.
    If OpenClipboard(0) Then
        ' Build a null-terminated list of file names.
        For i = LBound(file_names) To UBound(file_names)
            file_string = file_string & file_names(i) & _
                vbNullChar
        Next
        file_string = file_string & vbNullChar

        ' Initialize the DROPFILES structure.
        drop_files.pFiles = Len(drop_files)
        drop_files.fWide = 0    ' ANSI characters.
        drop_files.fNC = 0      ' Client coordinates.

        ' Get global memory to hold the DROPFILES
        ' structure and the file list string.
        memory_handle = GlobalAlloc(GHND, Len(drop_files) + _
            Len(file_string))
        If memory_handle Then
            ' Lock the memory while we initialize it.
            memory_pointer = GlobalLock(memory_handle)

            ' Copy the DROPFILES structure and the
            ' file string into the global memory.
            CopyMem ByVal memory_pointer, drop_files, _
                Len(drop_files)
            CopyMem ByVal memory_pointer + Len(drop_files), _
                ByVal file_string, Len(file_string)
            GlobalUnlock memory_handle

            ' Copy the data to the clipboard.
            SetClipboardData CF_HDROP, memory_handle
            ClipboardSetFiles = True
        End If

        ' Close the clipboard.
        CloseClipboard
    End If
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated