PyXR

c:\python24\lib\site-packages\win32 \ com \ demos \ dump_clipboard.py



0001 import pythoncom
0002 import win32con
0003 
0004 formats = """CF_TEXT CF_BITMAP CF_METAFILEPICT CF_SYLK CF_DIF CF_TIFF
0005             CF_OEMTEXT CF_DIB CF_PALETTE CF_PENDATA CF_RIFF CF_WAVE
0006             CF_UNICODETEXT CF_ENHMETAFILE CF_HDROP CF_LOCALE CF_MAX
0007             CF_OWNERDISPLAY CF_DSPTEXT CF_DSPBITMAP CF_DSPMETAFILEPICT
0008             CF_DSPENHMETAFILE""".split()
0009 format_name_map = {}
0010 for f in formats:
0011     val = getattr(win32con, f)
0012     format_name_map[val]=f
0013 
0014 tymeds = [attr for attr in pythoncom.__dict__.keys() if attr.startswith("TYMED_")]
0015 
0016 def DumpClipboard():
0017     do = pythoncom.OleGetClipboard()
0018     print "Dumping all clipboard formats..."
0019     for fe in do.EnumFormatEtc():
0020         fmt, td, aspect, index, tymed = fe
0021         tymeds_this = [getattr(pythoncom, t) for t in tymeds if tymed & getattr(pythoncom, t)]
0022         print "Clipboard format", format_name_map.get(fmt,str(fmt))
0023         for t_this in tymeds_this:
0024             # As we are enumerating there should be no need to call
0025             # QueryGetData, but we do anyway!
0026             fetc_query = fmt, td, aspect, index, t_this
0027             try:
0028                 do.QueryGetData(fetc_query)
0029             except pythoncom.com_error:
0030                 print "Eeek - QGD indicated failure for tymed", t_this
0031             # now actually get it.
0032             medium = do.GetData(fetc_query)
0033             if medium.tymed==pythoncom.TYMED_GDI:
0034                 data = "GDI handle %d" % medium.data
0035             elif medium.tymed==pythoncom.TYMED_MFPICT:
0036                 data = "METAFILE handle %d" % medium.data
0037             elif medium.tymed==pythoncom.TYMED_ENHMF:
0038                 data = "ENHMETAFILE handle %d" % medium.data
0039             elif medium.tymed==pythoncom.TYMED_HGLOBAL:
0040                 data = "%d bytes via HGLOBAL" % len(medium.data)
0041             elif medium.tymed==pythoncom.TYMED_FILE:
0042                 data = "filename '%s'" % data
0043             elif medium.tymed==pythoncom.TYMED_ISTREAM:
0044                 stream = medium.data
0045                 stream.Seek(0,0)
0046                 bytes = 0
0047                 while 1:
0048                     chunk = stream.Read(4096)
0049                     if not chunk:
0050                         break
0051                     bytes += len(chunk)
0052                 data = "%d bytes via IStream" % bytes
0053             elif medium.tymed==pythoncom.TYMED_ISTORAGE:
0054                 data = "a IStorage"
0055             else:
0056                 data = "*** unknown tymed!"
0057             print " -> got", data
0058     do = None
0059 
0060 if __name__=='__main__':
0061     DumpClipboard()
0062     if pythoncom._GetInterfaceCount()+pythoncom._GetGatewayCount():
0063         print "XXX - Leaving with %d/%d COM objects alive" % \
0064               (pythoncom._GetInterfaceCount(), pythoncom._GetGatewayCount())
0065 

Generated by PyXR 0.9.4
SourceForge.net Logo