Python Windows 资源管理器刷新 PostMessage
Python Windows Explorer Refresh PostMessage
我正在编写 Python 脚本来切换 Windows 资源管理器的“隐藏项目”状态。它更改了 Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden
的值,但要使更改生效,必须刷新资源管理器的所有实例。
我几个月前用 AutoHotkey 实现了同样的想法,在那里我可以使用我在 AutoHotkey 论坛上找到的以下命令解决刷新问题:
WinGetClass, CabinetWClass
PostMessage, 0x111, 28931, , , A
PostMessage, 0x111, 41504, , , A
我尝试了不同的方法来翻译它,但无法让它与 Python 一起使用。在寻找答案时,我还找到了一个使用 C# (Refresh Windows Explorer in Win7) 的解决方案,它比 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))
我正在编写 Python 脚本来切换 Windows 资源管理器的“隐藏项目”状态。它更改了 Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden
的值,但要使更改生效,必须刷新资源管理器的所有实例。
我几个月前用 AutoHotkey 实现了同样的想法,在那里我可以使用我在 AutoHotkey 论坛上找到的以下命令解决刷新问题:
WinGetClass, CabinetWClass
PostMessage, 0x111, 28931, , , A
PostMessage, 0x111, 41504, , , A
我尝试了不同的方法来翻译它,但无法让它与 Python 一起使用。在寻找答案时,我还找到了一个使用 C# (Refresh Windows Explorer in Win7) 的解决方案,它比 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))