wxpython 拖拽

来源:互联网 发布:算法1mints.txt 编辑:程序博客网 时间:2024/06/05 10:11

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

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

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

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

from win32com.shell import shell, shellcondef BeginDragFile(self):data = wx.FileDataObject()dropSource = wx.DropSource(self)dropSource.SetData(data)result= dropSource.DoDragDrop(0)if not result:returnh=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:returnpath=''#结束是鼠标在文件资源管理器上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)breakif os.path.isdir(path):return path








原创粉丝点击