在 Windows 中为文件显示资源管理器的属性对话框 [英] Show Explorer's properties dialog for a file in Windows

查看:69
本文介绍了在 Windows 中为文件显示资源管理器的属性对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以使用 Python 在 Windows 中显示文件的属性对话框?

Is there an easy way to show the properties dialog for a file in Windows using Python?

我正在尝试显示在资源管理器中右键单击文件并选择属性时弹出的相同窗口.

I'm trying to show the same window that pops up when you right click a file in Explorer and choose Properties.

推荐答案

这样做的方法是调用 Windows ShellExecuteEx() API 传递 properties 动词.有各种高级 Python 包装器,但我没有成功地让它们中的任何一个与 properties 动词一起工作.相反,我会使用旧的 ctypes.

The way to do this is to call the Windows ShellExecuteEx() API passing the properties verb. There are various high-level Python wrappers of this but I have not succeeded in getting any of them to work with the properties verb. Instead I would use good old ctypes.

import time
import ctypes
import ctypes.wintypes

SEE_MASK_NOCLOSEPROCESS = 0x00000040
SEE_MASK_INVOKEIDLIST = 0x0000000C

class SHELLEXECUTEINFO(ctypes.Structure):
    _fields_ = (
        ("cbSize",ctypes.wintypes.DWORD),
        ("fMask",ctypes.c_ulong),
        ("hwnd",ctypes.wintypes.HANDLE),
        ("lpVerb",ctypes.c_char_p),
        ("lpFile",ctypes.c_char_p),
        ("lpParameters",ctypes.c_char_p),
        ("lpDirectory",ctypes.c_char_p),
        ("nShow",ctypes.c_int),
        ("hInstApp",ctypes.wintypes.HINSTANCE),
        ("lpIDList",ctypes.c_void_p),
        ("lpClass",ctypes.c_char_p),
        ("hKeyClass",ctypes.wintypes.HKEY),
        ("dwHotKey",ctypes.wintypes.DWORD),
        ("hIconOrMonitor",ctypes.wintypes.HANDLE),
        ("hProcess",ctypes.wintypes.HANDLE),
    )

ShellExecuteEx = ctypes.windll.shell32.ShellExecuteEx
ShellExecuteEx.restype = ctypes.wintypes.BOOL

sei = SHELLEXECUTEINFO()
sei.cbSize = ctypes.sizeof(sei)
sei.fMask = SEE_MASK_NOCLOSEPROCESS | SEE_MASK_INVOKEIDLIST
sei.lpVerb = "properties"
sei.lpFile = "C:\\Desktop\\test.txt"
sei.nShow = 1
ShellExecuteEx(ctypes.byref(sei))
time.sleep(5)

我调用 sleep 的原因是属性对话框在调用过程中显示为一个窗口.如果 Python 可执行文件在调用 ShellExecuteEx 后立即终止,则没有任何内容可以为对话框提供服务,并且不会显示.

The reason I put in the call to sleep is that the properties dialog is shown as a window in the calling process. If the Python executable terminates immediately following the call to ShellExecuteEx, then there is nothing there to service the dialog and it does not show.

这篇关于在 Windows 中为文件显示资源管理器的属性对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆