0

I'm writing a Python script to toggle the "Hidden Items" status of the Windows Explorer. It changes the value of Computer\HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced\Hidden, but for the change to take effect all instances of the Explorer have to be refreshed.

I implemented the same idea a few months ago with AutoHotkey, there I could solve the refresh problem with the following commands I found on the AutoHotkey Forum:

WinGetClass, CabinetWClass
PostMessage, 0x111, 28931, , , A
PostMessage, 0x111, 41504, , , A

I tried different approaches to translate it, but wasn't able to get it working with Python. While searching for an answer I also found a solution with C# (Refresh Windows Explorer in Win7), which is far more complicated than the AutoHotkey version. I could let the Python script call the C# script, but I would much prefer a solution without auxiliary files.

How can I implement such behavior with Python?

1
  • There are Python bindings for the Windows API (WINAPI). Look into pywin32.
    – Paul M.
    Commented Nov 23, 2020 at 9:19

1 Answer 1

0

Using pywin module:

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))
1
  • Thanks a lot, this is exactly what I needed :) Commented Nov 23, 2020 at 23:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.