def _GetFullDesktopName(window_station, desktop) -> str: """Returns a full name to a desktop. Args: window_station: Handle to window station. desktop: Handle to desktop. """ return "\\".join([ win32service.GetUserObjectInformation(handle, win32service.UOI_NAME) for handle in [window_station, desktop] ])
def icon_wndproc(hwnd, msg, wp, lp): """Window proc for the tray icons""" if lp == win32con.WM_LBUTTONDOWN: ## popup menu won't disappear if you don't do this win32gui.SetForegroundWindow(hwnd) curr_desktop = win32service.OpenInputDesktop(0, True, win32con.MAXIMUM_ALLOWED) curr_desktop_name = win32service.GetUserObjectInformation( curr_desktop, win32con.UOI_NAME ) winsta = win32service.GetProcessWindowStation() desktops = winsta.EnumDesktops() m = win32gui.CreatePopupMenu() desktop_cnt = len(desktops) ## *don't* create an item 0 for d in range(1, desktop_cnt + 1): mf_flags = win32con.MF_STRING ## if you switch to winlogon yourself, there's nothing there and you're stuck if desktops[d - 1].lower() in ("winlogon", "disconnect"): mf_flags = mf_flags | win32con.MF_GRAYED | win32con.MF_DISABLED if desktops[d - 1] == curr_desktop_name: mf_flags = mf_flags | win32con.MF_CHECKED win32gui.AppendMenu(m, mf_flags, d, desktops[d - 1]) win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt + 1, "Create new ...") win32gui.AppendMenu(m, win32con.MF_STRING, desktop_cnt + 2, "Exit") x, y = win32gui.GetCursorPos() d = win32gui.TrackPopupMenu( m, win32con.TPM_LEFTBUTTON | win32con.TPM_RETURNCMD | win32con.TPM_NONOTIFY, x, y, 0, hwnd, None, ) win32gui.PumpWaitingMessages() win32gui.DestroyMenu(m) if d == desktop_cnt + 1: ## Create new get_new_desktop_name(hwnd) elif d == desktop_cnt + 2: ## Exit win32gui.PostQuitMessage(0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, window_info[hwnd]) del window_info[hwnd] origin_desktop.SwitchDesktop() elif d > 0: hdesk = win32service.OpenDesktop( desktops[d - 1], 0, 0, win32con.MAXIMUM_ALLOWED ) hdesk.SwitchDesktop() return 0 else: return win32gui.DefWindowProc(hwnd, msg, wp, lp)
get_new_desktop_name(hwnd) elif d==desktop_cnt+2: ## Exit win32gui.PostQuitMessage(0) win32gui.Shell_NotifyIcon(win32gui.NIM_DELETE, window_info[hwnd]) del window_info[hwnd] origin_desktop.SwitchDesktop() elif d>0: hdesk=win32service.OpenDesktop(desktops[d-1],0,0,win32con.MAXIMUM_ALLOWED) hdesk.SwitchDesktop() return 0 else: return win32gui.DefWindowProc(hwnd, msg, wp, lp) window_info={} origin_desktop=win32service.OpenInputDesktop(0, True, win32con.MAXIMUM_ALLOWED) origin_desktop_name=win32service.GetUserObjectInformation(origin_desktop, win32service.UOI_NAME) hinst=win32api.GetModuleHandle(None) try: hicon=win32gui.LoadIcon(hinst, 1) ## python.exe and pythonw.exe except win32gui.error: hicon=win32gui.LoadIcon(hinst, 135) ## pythonwin's icon id=0 create_desktop(str(origin_desktop_name),0) ## wait for first thread to initialize its icon while not window_info: time.sleep(1) ## exit when last tray icon goes away
def GetDesktopName(hdesk): name = win32service.GetUserObjectInformation(hdesk, win32con.UOI_NAME) return name