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()
python类STGMEDIUM的实例源码
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, 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, 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 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 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 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 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"))