我们从Python开源项目中,提取了以下24个代码示例,用于说明如何使用pythoncom.TYMED_HGLOBAL。
def clip_files(file_list): offset = ctypes.sizeof(DROPFILES) length = sum(len(p) + 1 for p in file_list) + 1 size = offset + length * ctypes.sizeof(ctypes.c_wchar) buf = (ctypes.c_char * size)() df = DROPFILES.from_buffer(buf) df.pFiles, df.fWide = offset, True for path in file_list: path = path.decode('gbk') print "copying to clipboard, filename = " + path array_t = ctypes.c_wchar * (len(path) + 1) path_buf = array_t.from_buffer(buf, offset) path_buf.value = path offset += ctypes.sizeof(path_buf) stg = pythoncom.STGMEDIUM() stg.set(pythoncom.TYMED_HGLOBAL, buf) win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() try: win32clipboard.SetClipboardData(win32clipboard.CF_HDROP, stg.data) print "clip_files() succeed" finally: win32clipboard.CloseClipboard()
def GetData(self, fe): ret_stg = None cf, target, aspect, index, tymed = fe if aspect & pythoncom.DVASPECT_CONTENT and \ tymed==pythoncom.TYMED_HGLOBAL: if cf == win32con.CF_TEXT: ret_stg = pythoncom.STGMEDIUM() # ensure always 'bytes' by encoding string. ret_stg.set(pythoncom.TYMED_HGLOBAL, str2bytes(self.strval)) elif cf == win32con.CF_UNICODETEXT: ret_stg = pythoncom.STGMEDIUM() ret_stg.set(pythoncom.TYMED_HGLOBAL, unicode(self.strval)) if ret_stg is None: raise COMException(hresult=winerror.E_NOTIMPL) return ret_stg
def GetData(self, fe): ret_stg = None cf, target, aspect, index, tymed = fe if aspect & pythoncom.DVASPECT_CONTENT and \ tymed==pythoncom.TYMED_HGLOBAL: if cf == win32con.CF_TEXT: ret_stg = pythoncom.STGMEDIUM() # ensure always 'bytes' by encoding string. ret_stg.set(pythoncom.TYMED_HGLOBAL, str2bytes(self.strval)) elif cf == win32con.CF_UNICODETEXT: ret_stg = pythoncom.STGMEDIUM() ret_stg.set(pythoncom.TYMED_HGLOBAL, str(self.strval)) if ret_stg is None: raise COMException(hresult=winerror.E_NOTIMPL) return ret_stg
def __init__(self, strval): global num_do_objects num_do_objects += 1 self.strval = strval self.supported_fe = [] for cf in (win32con.CF_TEXT, win32con.CF_UNICODETEXT): fe = cf, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL self.supported_fe.append(fe)
def QueryGetData(self, fe): cf, target, aspect, index, tymed = fe if aspect & pythoncom.DVASPECT_CONTENT == 0: raise COMException(hresult=winerror.DV_E_DVASPECT) if tymed!=pythoncom.TYMED_HGLOBAL: raise COMException(hresult=winerror.DV_E_TYMED) return None # should check better
def testWin32ToCom(self): # Set the data via the std win32 clipboard functions. val = str2bytes("Hello again!") # ensure always bytes, even in py3k win32clipboard.OpenClipboard() win32clipboard.SetClipboardData(win32con.CF_TEXT, val) win32clipboard.CloseClipboard() # and get it via an IDataObject provided by COM do = pythoncom.OleGetClipboard() cf = win32con.CF_TEXT, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL stg = do.GetData(cf) got = stg.data # The data we get back has the \0, as our STGMEDIUM has no way of # knowing if it meant to be a string, or a binary buffer, so # it must return it too. self.failUnlessEqual(got, str2bytes("Hello again!\0"))
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): print "QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags # Query the items clicked on format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL sm = self.dataobj.GetData(format_etc) num_files = shell.DragQueryFile(sm.data_handle, -1) if num_files>1: msg = "&Hello from Python (with %d files selected)" % num_files else: fname = shell.DragQueryFile(sm.data_handle, 0) msg = "&Hello from Python (with '%s' selected)" % fname idCmd = idCmdFirst items = ['First Python content menu item'] if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 print "CMF_NORMAL..." items.append(msg) elif uFlags & shellcon.CMF_VERBSONLY: print "CMF_VERBSONLY..." items.append(msg + " - shortcut") elif uFlags & shellcon.CMF_EXPLORE: print "CMF_EXPLORE..." items.append(msg + " - normal file, right-click in Explorer") elif uFlags & CMF_DEFAULTONLY: print "CMF_DEFAULTONLY...\r\n" else: print "** unknown flags", uFlags win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, 0, None) indexMenu += 1 for item in items: win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_STRING|win32con.MF_BYPOSITION, idCmd, item) indexMenu += 1 idCmd += 1 win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, 0, None) indexMenu += 1 return idCmd-idCmdFirst # Must return number of menu items we added.
def DumpClipboard(): do = pythoncom.OleGetClipboard() print "Dumping all clipboard formats..." for fe in do.EnumFormatEtc(): fmt, td, aspect, index, tymed = fe tymeds_this = [getattr(pythoncom, t) for t in tymeds if tymed & getattr(pythoncom, t)] print "Clipboard format", format_name_map.get(fmt,str(fmt)) for t_this in tymeds_this: # As we are enumerating there should be no need to call # QueryGetData, but we do anyway! fetc_query = fmt, td, aspect, index, t_this try: do.QueryGetData(fetc_query) except pythoncom.com_error: print "Eeek - QGD indicated failure for tymed", t_this # now actually get it. try: medium = do.GetData(fetc_query) except pythoncom.com_error, exc: print "Failed to get the clipboard data:", exc continue if medium.tymed==pythoncom.TYMED_GDI: data = "GDI handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_MFPICT: data = "METAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_ENHMF: data = "ENHMETAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_HGLOBAL: data = "%d bytes via HGLOBAL" % len(medium.data) elif medium.tymed==pythoncom.TYMED_FILE: data = "filename '%s'" % data elif medium.tymed==pythoncom.TYMED_ISTREAM: stream = medium.data stream.Seek(0,0) bytes = 0 while 1: chunk = stream.Read(4096) if not chunk: break bytes += len(chunk) data = "%d bytes via IStream" % bytes elif medium.tymed==pythoncom.TYMED_ISTORAGE: data = "a IStorage" else: data = "*** unknown tymed!" print " -> got", data do = None
def main(): hwnd = 0 # Create an instance of the object picker. picker = pythoncom.CoCreateInstance(adsi.CLSID_DsObjectPicker, None, pythoncom.CLSCTX_INPROC_SERVER, adsi.IID_IDsObjectPicker) # Create our scope init info. siis = adsi.DSOP_SCOPE_INIT_INFOs(1) sii = siis[0] # Combine multiple scope types in a single array entry. sii.type = DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | \ DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN # Set uplevel and downlevel filters to include only computer objects. # Uplevel filters apply to both mixed and native modes. # Notice that the uplevel and downlevel flags are different. sii.filterFlags.uplevel.bothModes = DSOP_FILTER_COMPUTERS sii.filterFlags.downlevel = DSOP_DOWNLEVEL_FILTER_COMPUTERS # Initialize the interface. picker.Initialize( None, # Target is the local computer. siis, # scope infos DSOP_FLAG_MULTISELECT, # options ('objectGUID','displayName') ) # attributes to fetch do = picker.InvokeDialog(hwnd) # Extract the data from the IDataObject. format_etc = (cf_objectpicker, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL) medium = do.GetData(format_etc) data = adsi.StringAsDS_SELECTION_LIST(medium.data) for item in data: name, klass, adspath, upn, attrs, flags = item print "Item", name print " Class:", klass print " AdsPath:", adspath print " UPN:", upn print " Attrs:", attrs print " Flags:", flags
def QueryContextMenu(self, hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags): print("QCM", hMenu, indexMenu, idCmdFirst, idCmdLast, uFlags) # Query the items clicked on format_etc = win32con.CF_HDROP, None, 1, -1, pythoncom.TYMED_HGLOBAL sm = self.dataobj.GetData(format_etc) num_files = shell.DragQueryFile(sm.data_handle, -1) if num_files>1: msg = "&Hello from Python (with %d files selected)" % num_files else: fname = shell.DragQueryFile(sm.data_handle, 0) msg = "&Hello from Python (with '%s' selected)" % fname idCmd = idCmdFirst items = ['First Python content menu item'] if (uFlags & 0x000F) == shellcon.CMF_NORMAL: # Check == here, since CMF_NORMAL=0 print("CMF_NORMAL...") items.append(msg) elif uFlags & shellcon.CMF_VERBSONLY: print("CMF_VERBSONLY...") items.append(msg + " - shortcut") elif uFlags & shellcon.CMF_EXPLORE: print("CMF_EXPLORE...") items.append(msg + " - normal file, right-click in Explorer") elif uFlags & CMF_DEFAULTONLY: print("CMF_DEFAULTONLY...\r\n") else: print("** unknown flags", uFlags) win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, 0, None) indexMenu += 1 for item in items: win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_STRING|win32con.MF_BYPOSITION, idCmd, item) indexMenu += 1 idCmd += 1 win32gui.InsertMenu(hMenu, indexMenu, win32con.MF_SEPARATOR|win32con.MF_BYPOSITION, 0, None) indexMenu += 1 return idCmd-idCmdFirst # Must return number of menu items we added.
def main(): hwnd = 0 # Create an instance of the object picker. picker = pythoncom.CoCreateInstance(adsi.CLSID_DsObjectPicker, None, pythoncom.CLSCTX_INPROC_SERVER, adsi.IID_IDsObjectPicker) # Create our scope init info. siis = adsi.DSOP_SCOPE_INIT_INFOs(1) sii = siis[0] # Combine multiple scope types in a single array entry. sii.type = DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | \ DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN # Set uplevel and downlevel filters to include only computer objects. # Uplevel filters apply to both mixed and native modes. # Notice that the uplevel and downlevel flags are different. sii.filterFlags.uplevel.bothModes = DSOP_FILTER_COMPUTERS sii.filterFlags.downlevel = DSOP_DOWNLEVEL_FILTER_COMPUTERS # Initialize the interface. picker.Initialize( None, # Target is the local computer. siis, # scope infos DSOP_FLAG_MULTISELECT, # options ('objectGUID','displayName') ) # attributes to fetch do = picker.InvokeDialog(hwnd) # Extract the data from the IDataObject. format_etc = (cf_objectpicker, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL) medium = do.GetData(format_etc) data = adsi.StringAsDS_SELECTION_LIST(medium.data) for item in data: name, klass, adspath, upn, attrs, flags = item print("Item", name) print(" Class:", klass) print(" AdsPath:", adspath) print(" UPN:", upn) print(" Attrs:", attrs) print(" Flags:", flags)
def DumpClipboard(): do = pythoncom.OleGetClipboard() print("Dumping all clipboard formats...") for fe in do.EnumFormatEtc(): fmt, td, aspect, index, tymed = fe tymeds_this = [getattr(pythoncom, t) for t in tymeds if tymed & getattr(pythoncom, t)] print("Clipboard format", format_name_map.get(fmt,str(fmt))) for t_this in tymeds_this: # As we are enumerating there should be no need to call # QueryGetData, but we do anyway! fetc_query = fmt, td, aspect, index, t_this try: do.QueryGetData(fetc_query) except pythoncom.com_error: print("Eeek - QGD indicated failure for tymed", t_this) # now actually get it. try: medium = do.GetData(fetc_query) except pythoncom.com_error as exc: print("Failed to get the clipboard data:", exc) continue if medium.tymed==pythoncom.TYMED_GDI: data = "GDI handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_MFPICT: data = "METAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_ENHMF: data = "ENHMETAFILE handle %d" % medium.data elif medium.tymed==pythoncom.TYMED_HGLOBAL: data = "%d bytes via HGLOBAL" % len(medium.data) elif medium.tymed==pythoncom.TYMED_FILE: data = "filename '%s'" % data elif medium.tymed==pythoncom.TYMED_ISTREAM: stream = medium.data stream.Seek(0,0) bytes = 0 while 1: chunk = stream.Read(4096) if not chunk: break bytes += len(chunk) data = "%d bytes via IStream" % bytes elif medium.tymed==pythoncom.TYMED_ISTORAGE: data = "a IStorage" else: data = "*** unknown tymed!" print(" -> got", data) do = None