Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


Sometimes in your application you might want to check if any Clipboard data available and if available then in which format. You can check format of clipboard data and you can enable/disable menu options based on available clipboard data formats. For example in timer event you can check for CF_BITMAP clipboard format and if available then enable "Paste Image" command button.

Step-By-Step Example

- Create a standard exe project
- Add one listbox control and one timer control on the form1
- Now try to copy something from word or excel or may be from any html page and see the updated clipboard formats in the list box.

Click here to copy the following block
Option Explicit
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 Declare Function GetClipboardSequenceNumber Lib "user32.dll" () As Long

Private Enum enumClipboardFormat
  [_First] = 1 '//This is required if u want to check range
  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 '//This is required if u want to check range
End Enum

Dim LastClipboardSqnNumber As Long

Private Property Get FormatName(ByVal lFormatId As Long) As String

  ' Returns the format name for a clipboard format id:
  Dim lSize As Long
  Dim sBuf As String
  Dim lR As Long

  If (lFormatId >= enumClipboardFormat.[_First] _
      And lFormatId <= enumClipboardFormat.[_Last]) Then

    ' For pre-defined formats, we have to make the text
    ' up ourselves:
    Select Case lFormatId
      Case CF_TEXT
        FormatName = "CF_TEXT : Text"
      Case CF_BITMAP
        FormatName = "CF_BITMAP : Bitmap Picture"
      Case CF_METAFILEPICT
        FormatName = "CF_METAFILEPICT : Meta-File Picture"
      Case CF_SYLK
        FormatName = "CF_SYLK : Microsoft Symbolic Link (SYLK) data."
      Case CF_DIF
        FormatName = "CF_DIF : Software Arts' Data Interchange information."
      Case CF_TIFF = 6
        FormatName = "CF_TIFF : Tagged Image File Format (TIFF) Picture"
      Case CF_OEMTEXT
        FormatName = "CF_OEMTEXT : Text (OEM)"
      Case CF_DIB
        FormatName = "CF_DIB : DIB Bitmap Picture"
      Case CF_PALETTE
        FormatName = "CF_PALETTE : Colour Palette"
      Case CF_PENDATA
        FormatName = "CF_PENDATA : Pen Data"
      Case CF_RIFF
        FormatName = "CF_RIFF : RIFF Audio data"
      Case CF_WAVE
        FormatName = "CF_WAVE : Wave File"
      Case CF_UNICODETEXT
        FormatName = "CF_UNICODETEXT : Text (Unicode)"
      Case CF_ENHMETAFILE
        FormatName = "CF_ENHMETAFILE : Enhanced Meta-File Picture"
      Case CF_HDROP
        FormatName = "CF_HDROP : File List"
      Case CF_LOCALE
        FormatName = "CF_LOCALE : Text Locale Identifier"
    End Select

  Else

    ' For custom formats, we can ask the Clipboard for
    ' the registered name:
    lSize = 255
    sBuf = String$(lSize, 0)
    lR = GetClipboardFormatName(lFormatId, sBuf, lSize)
    If (lR <> 0) Then
      FormatName = "Custom Format :" & Left$(sBuf, lR)
    End If
    
  End If

End Property

Sub RefreshClipboardDataFormats()
  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

Private Sub Form_Load()
  Timer1.Enabled = True
  Timer1.Interval = 500
End Sub

Private Sub Timer1_Timer()
  Dim SequenceNum As Long
  SequenceNum = GetClipboardSequenceNumber()
  '//Check for clipboard data changed
  If LastClipboardSqnNumber <> SequenceNum Then
    RefreshClipboardDataFormats
    LastClipboardSqnNumber = SequenceNum
    Me.Caption = "Monitoring Clipboard changes >> Clipboard Sequence Number : " & LastClipboardSqnNumber
  End If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.