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

时间:2018-10-31 23:22:37

标签: python python-3.x winapi pywin32

有人可以告诉我如何使用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

任何帮助(对于任何Windows,最好是7)都非常感谢。预先感谢!

0 个答案:

没有答案