天天看點

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