2011-11-02 51 views

回答

5

執行此操作的方法是調用傳遞properties動詞的Windows ShellExecuteEx() API。這裏有各種各樣的高級Python包裝,但我還沒有成功地讓它們中的任何一個使用properties動詞。相反,我會用好舊的​​。

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後立即終止,則沒有任何內容可用於對話並且不顯示。

+0

太棒了,這個作品很棒 - 非常感謝!我嘗試過的其中一件事與此類似。我沒有在我的測試文件中調用睡眠,所以會停止它的工作。我也沒有使用wintypes,只是將大部分ShellExecuteEx參數都傳遞給None。時間閱讀ctypes! –

+0

@大衛,非常感謝!你真棒! –

相關問題