天天看点

wxpython 拖拽

从本地拖拽到窗口比较简单没有太大问题,

但是从窗口中拖拽带 资源管理器中搞了半天

由于文件全部在远程,从窗口拖拽到桌面中,需要使用com 比较麻烦,先简单实现吧

使用windowfrompoint获取窗口句柄来获取拖拽结束路径

from win32com.shell import shell, shellcon
	def BeginDragFile(self):
		data = wx.FileDataObject()
		dropSource = wx.DropSource(self)
		dropSource.SetData(data)
		result= dropSource.DoDragDrop(0)
		if not result:return
		h=win32gui.WindowFromPoint(win32api.GetCursorPos())
		#结束是鼠标在文件资源管理器上
		if win32gui.GetWindowText(h)=='FolderView':
			f=win32gui.GetForegroundWindow()
			#获取文件资源管理器title就是路径
			path=win32gui.GetWindowText(f)
			#是否为桌面
			if path=='Program Manager':
				path=shell.SHGetPathFromIDList(shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP))
			if os.path.isdir(path):return path
           

最后获取的就是拖拽结束的路径,直接使用该路径执行下载操作

2012 12 24 更新 添加win7支持

def RawDragPath(self):
	data = wx.FileDataObject()
	dropSource = wx.DropSource(self)
	dropSource.SetData(data)
	result= dropSource.DoDragDrop(0)
	h=win32gui.WindowFromPoint(win32api.GetCursorPos())
	f=win32gui.GetForegroundWindow()
	if not result:return
	path=''
	#结束是鼠标在文件资源管理器上
	if f==windll.user32.GetAncestor(h,2):
		path=shell.SHGetPathFromIDList(shell.SHGetSpecialFolderLocation(0, shellcon.CSIDL_DESKTOP))
	else:
		s = win32com.client.Dispatch("Shell.Application")
		for w in s.Windows():
			if int(w.Hwnd) == f:
				path = w.LocationURL.split('///')[1]
				path=urllib2.unquote(path)
				break

	if os.path.isdir(path):return path