[英]Python Windows Explorer Refresh PostMessage
我正在編寫一個 Python 腳本來切換 Windows 資源管理器的“隱藏項目”狀態。 它更改了Computer\\HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced\\Hidden
,但Computer\\HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced\\Hidden
更改生效,必須刷新 Explorer 的所有實例。
幾個月前我用 AutoHotkey 實現了同樣的想法,在那里我可以使用我在 AutoHotkey 論壇上找到的以下命令解決刷新問題:
WinGetClass, CabinetWClass
PostMessage, 0x111, 28931, , , A
PostMessage, 0x111, 41504, , , A
我嘗試了不同的方法來翻譯它,但無法讓它與 Python 一起工作。 在尋找答案時,我還找到了一個使用 C#( 在 Win7 中刷新 Windows 資源管理器)的解決方案,它比 AutoHotkey 版本復雜得多。 我可以讓 Python 腳本調用 C# 腳本,但我更喜歡沒有輔助文件的解決方案。
如何使用 Python 實現此類行為?
使用pywin模塊:
import win32con
import win32gui
# List for handles:
handles = []
def collect_handles(h, _):
' Get window class name and add it to list '
if win32gui.GetClassName(h) == 'CabinetWClass':
handles.append(h)
def refresh_window(h):
' Send messages to window '
win32gui.PostMessage(h, win32con.WM_COMMAND, 28931, None)
win32gui.PostMessage(h, win32con.WM_COMMAND, 41504, None)
# Fill our list:
win32gui.EnumWindows(collect_handles, None)
# Perform action on our handles:
list(map(refresh_window, handles))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.