import ctypes
import time
import logging
import os
from ctypes import wintypes
user32 = ctypes.WinDLL("user32", use_last_error=True)
SW_HIDE = 0
SW_SHOW = 5
GW_OWNER = 4
logging.basicConfig(
filename=os.path.join(
os.environ.get("LOCALAPPDATA", "."),
"my_taskbar.log"
),
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s"
)
EnumWindowsProc = ctypes.WINFUNCTYPE(
wintypes.BOOL,
wintypes.HWND,
wintypes.LPARAM
)
def find_taskbar():
return user32.FindWindowW("Shell_TrayWnd", None)
def has_visible_app_window(taskbar):
found = False
@EnumWindowsProc
def callback(hwnd, _):
nonlocal found
if hwnd == taskbar:
return True
if not user32.IsWindowVisible(hwnd):
return True
if user32.IsIconic(hwnd):
return True
if user32.GetWindowTextLengthW(hwnd) == 0:
return True
if user32.GetWindow(hwnd, GW_OWNER):
return True
class_name = ctypes.create_unicode_buffer(256)
user32.GetClassNameW(hwnd, class_name, 256)
if class_name.value in (
"Progman",
"WorkerW",
"Shell_TrayWnd",
"Shell_SecondaryTrayWnd",
):
return True
found = True
return False
user32.EnumWindows(callback, 0)
return found
def main():
logging.info("My Taskbar запущена.")
try:
while True:
taskbar = find_taskbar()
if taskbar:
visible_window = has_visible_app_window(taskbar)
if visible_window:
user32.ShowWindow(taskbar, SW_HIDE)
else:
user32.ShowWindow(taskbar, SW_SHOW)
time.sleep(0.3)
except Exception:
logging.exception("Ошибка программы.")
finally:
taskbar = find_taskbar()
if taskbar:
user32.ShowWindow(taskbar, SW_SHOW)
if __name__ == "__main__":
main()