python资源管理器选择文件_Python-在当前Windows资源管理器中获取所选文件的路径...

#!python3importwin32gui,timefromwin32conimportPAGE_READWRITE,MEM_COMMIT,MEM_RESERVE,MEM_RELEASE,PROCESS_ALL_ACCESS,WM_GETTEXTLENGTH,WM_GETTEXTfromcommctrlimportLVM_GETITEMTEXT,LVM_GETITEMCOUNT,LVM_GETNEXTITEM,LVNI_SELECTEDimportosimportstructimportctypesimportwin32apiGetWindowThreadProcessId=ctypes.windll.user32.GetWindowThreadProcessIdVirtualAllocEx=ctypes.windll.kernel32.VirtualAllocExVirtualFreeEx=ctypes.windll.kernel32.VirtualFreeExOpenProcess=ctypes.windll.kernel32.OpenProcessWriteProcessMemory=ctypes.windll.kernel32.WriteProcessMemoryReadProcessMemory=ctypes.windll.kernel32.ReadProcessMemorymemcpy=ctypes.cdll.msvcrt.memcpydefreadListViewItems(hwnd,column_index=0):# Allocate virtual memory inside target processpid=ctypes.create_string_buffer(4)p_pid=ctypes.addressof(pid)GetWindowThreadProcessId(hwnd,p_pid)# process owning the given hwndhProcHnd=OpenProcess(PROCESS_ALL_ACCESS,False,struct.unpack("i",pid)[0])pLVI=VirtualAllocEx(hProcHnd,0,4096,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)pBuffer=VirtualAllocEx(hProcHnd,0,4096,MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)# Prepare an LVITEM record and write it to target process memorylvitem_str=struct.pack('iiiiiiiii',*[0,0,column_index,0,0,pBuffer,4096,0,0])lvitem_buffer=ctypes.create_string_buffer(lvitem_str)copied=ctypes.create_string_buffer(4)p_copied=ctypes.addressof(copied)WriteProcessMemory(hProcHnd,pLVI,ctypes.addressof(lvitem_buffer),ctypes.sizeof(lvitem_buffer),p_copied)# iterate items in the SysListView32 controlnum_items=win32gui.SendMessage(hwnd,LVM_GETITEMCOUNT)item_texts=[]foritem_indexinrange(num_items):win32gui.SendMessage(hwnd,LVM_GETITEMTEXT,item_index,pLVI)target_buff=ctypes.create_string_buffer(4096)ReadProcessMemory(hProcHnd,pBuffer,ctypes.addressof(target_buff),4096,p_copied)item_texts.append(target_buff.value)VirtualFreeEx(hProcHnd,pBuffer,0,MEM_RELEASE)VirtualFreeEx(hProcHnd,pLVI,0,MEM_RELEASE)win32api.CloseHandle(hProcHnd)returnitem_textsdefgetSelectedListViewItem(hwnd):returnwin32gui.SendMessage(hwnd,LVM_GETNEXTITEM,-1,LVNI_SELECTED)defgetSelectedListViewItems(hwnd):items=[]item=-1whileTrue:item=win32gui.SendMessage(hwnd,LVM_GETNEXTITEM,item,LVNI_SELECTED)ifitem==-1:breakitems.append(item)returnitemsdefgetEditText(hwnd):# api returns 16 bit characters so buffer needs 1 more char for null and twice the num of charsbuf_size=(win32gui.SendMessage(hwnd,WM_GETTEXTLENGTH,0,0)+1)*2target_buff=ctypes.create_string_buffer(buf_size)win32gui.SendMessage(hwnd,WM_GETTEXT,buf_size,ctypes.addressof(target_buff))returntarget_buff.raw.decode('utf16')[:-1]#remove the null char on the enddef_normaliseText(controlText):'''Remove '&' characters, and lower case.

Useful for matching control text.'''returncontrolText.lower().replace('&','')def_windowEnumerationHandler(hwnd,resultList):'''Pass to win32gui.EnumWindows() to generate list of window handle,

window text, window class tuples.'''resultList.append((hwnd,win32gui.GetWindowText(hwnd),win32gui.GetClassName(hwnd)))defsearchChildWindows(currentHwnd,wantedText=None,wantedClass=None,selectionFunction=None):results=[]childWindows=[]try:win32gui.EnumChildWindows(currentHwnd,_windowEnumerationHandler,childWindows)exceptwin32gui.error:# This seems to mean that the control *cannot* have child windows,# i.e. not a container.returnforchildHwnd,windowText,windowClassinchildWindows:descendentMatchingHwnds=searchChildWindows(childHwnd)ifdescendentMatchingHwnds:results+=descendentMatchingHwndsifwantedTextand\not_normaliseText(wantedText)in_normaliseText(windowText):continueifwantedClassand\notwindowClass==wantedClass:continueifselectionFunctionand\notselectionFunction(childHwnd):continueresults.append(childHwnd)returnresults

w=win32guiwhileTrue:time.sleep(5)window=w.GetForegroundWindow()print("window: %s"%window)if(window!=0):if(w.GetClassName(window)=='CabinetWClass'):# the main explorer windowprint("class: %s"%w.GetClassName(window))print("text: %s "%w.GetWindowText(window))children=list(set(searchChildWindows(window)))addr_edit=Nonefile_view=Noneforchildinchildren:if(w.GetClassName(child)=='ComboBoxEx32'):# the address baraddr_children=list(set(searchChildWindows(child)))foraddr_childinaddr_children:if(w.GetClassName(addr_child)=='Edit'):addr_edit=addr_childpasselif(w.GetClassName(child)=='SysListView32'):# the list control within the window that shows the filesfile_view=childifaddr_edit:path=getEditText(addr_edit)else:print('something went wrong - no address bar found')path=''iffile_view:files=[item.decode('utf8')foriteminreadListViewItems(file_view)]indexes=getSelectedListViewItems(file_view)print('path: %s'%path)print('files: %s'%files)print('selected files:')forindexinindexes:print("\t%s - %s"%(files[index],os.path.join(path,files[index])))else:print('something went wrong - no file view found')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值