在Windows中获取桌面上选定的文件(7)

2024-10-02 02:39:33 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能告诉我如何用Python在Windowsd桌面上获取所有选中的文件吗?我一直在寻找一种方法来做,但我找不到任何人。我找到的唯一一个是针对C的,但我不使用C编写代码,所以我甚至不知道它是否有效:Get list of selected files from Windows Desktop(如果有人理解它并能解释/转换它,我也很感激)。我发现了一些非常接近这个的东西,但是我只能让它获得所选文件的数量,而不是它们的路径,正如我所希望的那样:

import ctypes
from commctrl import LVM_GETITEMCOUNT,LVM_GETSELECTEDCOUNT
#The LVM_GETITEMCOUNT came with the script, I got the other one from Microsoft documentation about SendMessage(), and both are near, but none returns the paths, only numbers
import pywintypes
import win32gui

GetShellWindow = ctypes.windll.user32.GetShellWindow

def get_desktop():
    """Get the window of the icons, the desktop window contains this window"""
    shell_window = GetShellWindow()
    shell_dll_defview = win32gui.FindWindowEx(shell_window, 0, "SHELLDLL_DefView", "")
    if shell_dll_defview == 0:
        sys_listview_container = []
        try:
            win32gui.EnumWindows(_callback, sys_listview_container)
        except pywintypes.error as e:
            if e.winerror != 0:
                raise
        sys_listview = sys_listview_container[0]
    else:
        sys_listview = win32gui.FindWindowEx(shell_dll_defview, 0, "SysListView32", "FolderView")
    return sys_listview

def _callback(hwnd, extra):
    class_name = win32gui.GetClassName(hwnd)
    if class_name == "WorkerW":
        child = win32gui.FindWindowEx(hwnd, 0, "SHELLDLL_DefView", "")
        if child != 0:
            sys_listview = win32gui.FindWindowEx(child, 0, "SysListView32", "FolderView")
            extra.append(sys_listview)
            return False
    return True

def get_item_count(window):
    return win32gui.SendMessage(window, LVM_GETSELECTEDCOUNT)

desktop = get_desktop()
print(get_item_count(desktop))

我已经搜索了可以发送到窗口的命令,但没有找到任何人来获取所选项目的路径(可能我错过了一个?)。在

我发现了一种从Windows资源管理器窗口获取所选文件的方法,但现在是从桌面:https://stackoverflow.com/a/21250927/8228163。在

任何帮助(任何窗口,最好是7)是非常感谢。提前谢谢!在


Tags: 文件thefromimportgetreturnifsys

热门问题