我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用win32gui.SetForegroundWindow()。
def show_menu(self): menu = win32gui.CreatePopupMenu() self.create_menu(menu, self.menu_options) # win32gui.SetMenuDefaultItem(menu, 1000, 0) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam): if lparam == win32con.WM_LBUTTONUP: print "You clicked me." elif lparam == win32con.WM_LBUTTONDBLCLK: print "You double-clicked me - goodbye" win32gui.DestroyWindow(self.hwnd) elif lparam == win32con.WM_RBUTTONUP: print "You right clicked me." menu = win32gui.CreatePopupMenu() win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, gbk("????")) win32gui.AppendMenu(menu, win32con.MF_STRING, 1024, gbk("????")) win32gui.AppendMenu(menu, win32con.MF_STRING, 1025, gbk("????")) win32gui.AppendMenu(menu, win32con.MF_STRING, 1026, gbk("????")) pos = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1
def enter(): time.sleep(2) appname = "?????" enter = 0x0D window = win32gui.FindWindow(None, appname) try: win32gui.SetForegroundWindow(window) except: pass win32api.keybd_event(enter,0,0,0) time.sleep(0.1) win32api.keybd_event(enter,0,0,0) time.sleep(0.1) win32api.keybd_event(enter,0,0,0) time.sleep(0.1) win32api.keybd_event(enter,0,0,0) time.sleep(0.1) win32api.keybd_event(enter,0,0,0) time.sleep(0.1)
def hawk_enter(): appname = "?????" enter = 0x0D count = 0 while True: if count % 10 == 0: window = win32gui.FindWindow(None, appname) try: win32gui.SetForegroundWindow(window) except: pass if window != 0: win32api.keybd_event(enter,0,0,0) time.sleep(0.05) win32api.keybd_event(enter,0 ,win32con.KEYEVENTF_KEYUP ,0) time.sleep(1) count += 1
def show_menu(self): menu = win32gui.CreatePopupMenu() self.create_menu(menu, self.menu_options) #win32gui.SetMenuDefaultItem(menu, 1000, 0) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam): if lparam==win32con.WM_LBUTTONUP: print "You clicked me." elif lparam==win32con.WM_LBUTTONDBLCLK: print "You double-clicked me - goodbye" win32gui.DestroyWindow(self.hwnd) elif lparam==win32con.WM_RBUTTONUP: print "You right clicked me." menu = win32gui.CreatePopupMenu() win32gui.AppendMenu( menu, win32con.MF_STRING, 1023, "Display Dialog") win32gui.AppendMenu( menu, win32con.MF_STRING, 1024, "Say Hello") win32gui.AppendMenu( menu, win32con.MF_STRING, 1025, "Exit program" ) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1
def do_close_session(): hwnd = QQ_checkTargetWindow() if hwnd == 0: print "Window doesn't exist, no need to close session." return else: print "move the window to front." win32gui.SetForegroundWindow(hwnd) time.sleep(1.0) for try_time in range(0, 10, 1): print "try to close the current session, try_time = " + str(try_time) QQ_CloseWindow() time.sleep(1.0) if QQ_checkTargetWindow() != 0: QQ_CloseWindow() print "close fails, probably the file is still in transmission." time.sleep(5.0) else: print "session closed." return print "can't close the current session, abort."
def OnTaskbarNotify(self, hwnd, msg, wparam, lparam): if lparam==win32con.WM_LBUTTONUP: print("You clicked me.") elif lparam==win32con.WM_LBUTTONDBLCLK: print("You double-clicked me - goodbye") win32gui.DestroyWindow(self.hwnd) elif lparam==win32con.WM_RBUTTONUP: print("You right clicked me.") menu = win32gui.CreatePopupMenu() win32gui.AppendMenu( menu, win32con.MF_STRING, 1023, "Display Dialog") win32gui.AppendMenu( menu, win32con.MF_STRING, 1024, "Say Hello") win32gui.AppendMenu( menu, win32con.MF_STRING, 1025, "Exit program" ) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1
def web_upload_file(self, filepath): """????(??flash????) filepath ?????????????????????????${CURDIR}${/}Res${/}Plus_Web${/}pic0.jpg ${CURDIR}???????????? RF?filepath???????unicode ?????????????????????????????????? | Web Upload File | ${filepath} | | Web Upload File | ${CURDIR}${/}Res${/}Plus_Web${/}pic0.jpg | """ filepath=os.path.abspath(filepath) change = str(filepath) time.sleep(1) self._handle = win32gui.FindWindow(None, u"??")#??“??”????? win32gui.SetForegroundWindow(self._handle)#?????? SendKeys.SendKeys(change) time.sleep(1) SendKeys.SendKeys("{ENTER}")
def _show_menu(self): self.FIRST_ID = 1023 self.menu_options = (('Show', None, self.onShow),('Run', None, self.onRun),('Quit', None, self.onQuit)) self._next_action_id = self.FIRST_ID self.menu_actions_by_id = set() self.menu_options = self._add_ids_to_menu_options(list(self.menu_options)) self.menu_actions_by_id = dict(self.menu_actions_by_id) del self._next_action_id menu = win32gui.CreatePopupMenu() self.create_menu(menu, self.menu_options) pos = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def omnipeek_close(self): print 'Close the omnipeek application software' hwnd = win32gui.FindWindow('OmniPeek',None) win32gui.SetForegroundWindow(hwnd) self.shortcut_keys(['Alt','F']) self.sleep('1') self.shortcut_keys(['X']) self.sleep('1') hwd = win32gui.FindWindow(None,'OmniPeek') #print hwd #The window exist if hwd: win32gui.SetForegroundWindow(hwd) #ö self.sleep('1') self.shortcut_keys(['Alt','Y']) self.sleep('5') return True
def OnInitDialog(self, hwnd, msg, wparam, lparam): self.hwnd = hwnd desktop = win32gui.GetDesktopWindow() dt_l, dt_t, dt_r, dt_b = win32gui.GetWindowRect(desktop) centre_x, centre_y = win32gui.ClientToScreen( desktop, ( (dt_r-dt_l)/2, (dt_b-dt_t)/2) ) bmCtrl = win32gui.GetDlgItem(self.hwnd, IDC_BITMAP) win32gui.SendMessage(bmCtrl, win32con.STM_SETIMAGE, win32con.IMAGE_BITMAP, self.hSplash) win32gui.SetWindowPos(self.hwnd, win32con.HWND_TOPMOST, centre_x-(self.bmWidth/2), centre_y-(self.bmHeight/2), self.bmWidth, self.bmHeight, win32con.SWP_HIDEWINDOW) win32gui.SetForegroundWindow(self.hwnd)
def show_window(pid, delay = 0.5): """set a windows as principal based on it's process id""" time.sleep(delay) for hwnd in get_hwnds(pid): win32gui.SetForegroundWindow(hwnd) win32gui.ShowWindow(hwnd, win32con.SW_NORMAL)
def set_foreground(self): win32gui.SetForegroundWindow(self.hwnd)
def _input_left_mouse(self, x, y): left, top, right, bottom = self.rect width, height = right - left, bottom - top if x < 0 or x > width or y < 0 or y > height: return win32gui.SetForegroundWindow(self.hwnd) pos = win32gui.GetCursorPos() win32api.SetCursorPos((left+x, top+y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) win32api.Sleep(100) #ms win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) win32api.Sleep(100) #ms # win32api.SetCursorPos(pos)
def set_foreground(self): """Get the focus on the desired open window""" logging.debug("set the window foreground") if self.hwnd: logging.debug("Find the window and set it as foreground") win32gui.SetForegroundWindow(self.hwnd)
def getstate(self): win32gui.SetForegroundWindow(self.hwnd) bbox = win32gui.GetWindowRect(self.hwnd) img = ImageGrab.grab(bbox) img = img.resize([84, 84], Image.ANTIALIAS) pilim = np.array(img, dtype=np.uint8) pilim = rgb2gray(pilim) return pilim
def QQ_SendTextWithAt(str): os.system(qq_shortcut) try_time = 0 while True: time.sleep(0.5) hwnd = win32gui.FindWindow(None, '??&??') # hwnd = win32gui.FindWindow(None, 'OSVT?O?') print('try_time = %d, hwnd = %d' % (try_time, hwnd)) if hwnd != 0: break elif try_time >= 60: print ('SendTextToQQ Error.') return else: try_time = try_time + 1 win32gui.SetForegroundWindow(hwnd) QQ_PrintTextWithAt(str) QQ_Enter() # win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('v'), 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('v'), 0) #win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, 'aaa') #win32gui.SetWindowText(hwnd, 'aaa') #win32gui.ReplaceSel() #win32gui.PostMessage(hwnd, win32con.WM_CHAR, '', 3) # win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_CONTROL, 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, ord('V'), 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYUP, ord('V'), 0) # win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_CONTROL, 0)
def QQ_OpenActiveWindow(): hwnd = QQ_checkTargetWindow() if hwnd == 0: return False win32gui.SetForegroundWindow(hwnd) return True # Press the "Alt + H' key.
def focus_window(hwnd): """ Focuses the given window. Args: hwnd (int): The window handler. """ wg.ShowWindow(hwnd, wc.SW_SHOW) wg.ShowWindow(hwnd, wc.SW_SHOWNOACTIVATE) wg.SetForegroundWindow(hwnd)
def foreach_window(self): def callback(hwnd, lparam): title = win32gui.GetWindowText(hwnd).lower() for window in self.to_close: if window in title: win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) print "Closed window ({})".format(title) for window in self.clicks: if window in title: self._windows[hwnd] = { "matches": self.clicks[window], "to_click": [], "buttons": [] } try: win32gui.EnumChildWindows(hwnd, self.foreach_child(), hwnd) except: print "EnumChildWindows failed, moving on." for button_toclick in self._windows[hwnd]['to_click']: for button in self._windows[hwnd]['buttons']: if button_toclick in button['text']: win32gui.SetForegroundWindow(button['handle']) win32gui.SendMessage(button['handle'], win32con.BM_CLICK, 0, 0) print "Clicked on button ({} / {})".format(title, button['text']) del self._windows[hwnd] return True return callback
def restoreFocusWindow(hwnd): win32gui.ShowWindow(hwnd, win32con.SW_RESTORE) win32gui.SetForegroundWindow(hwnd) time.sleep(0.2)
def focusWindow(hwnd): """ ?????? :param hwnd: ???? :return: """ win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED) win32gui.SetForegroundWindow(hwnd)
def SetAsForegroundWindow(cls, hwnd): #??ALT??ALT???%??? cls.shell.SendKeys('%') win32gui.SetForegroundWindow(hwnd)
def SetForegroundWindow(cls, whd): cls.SetAsForegroundWindow(whd)
def _click(x, y): handle = win32gui.GetForegroundWindow() x_old, y_old = win32api.GetCursorPos() # see https://github.com/asweigart/pyautogui/issues/23 try: pyautogui.click(x, y, 1) except FileNotFoundError: pass win32api.SetCursorPos((x_old, y_old)) win32gui.SetForegroundWindow(handle)
def set_foreground(self): """Get the focus on the desired open window""" win32gui.SetForegroundWindow(self._handle)
def _set_foreground_window(hwnd): shell = win32com.client.Dispatch('WScript.Shell') shell.SendKeys('%') win32gui.SetForegroundWindow(hwnd)
def show_menu(self): if self.MENU is None: self.create_menu() position = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.HWND) win32gui.TrackPopupMenu( self.MENU, win32con.TPM_LEFTALIGN, position[0], position[1], 0, self.HWND, None ) win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)
def _return_focus_w32(self): w32win = win32gui.FindWindow(None, KWARGS['parent_title']) win32gui.SetForegroundWindow(w32win)
def app_first(self,str_app): hwnd = win32gui.FindWindow(None, str_app) print hwnd if hwnd < 1: hwnd = self.find_main_window(str_app) if hwnd>0: win32gui.SetForegroundWindow(hwnd) #win32gui.ShowWindow(hwnd,win32con.SW_SHOWMAXIMIZED) return True return False
def Find_Gui_button(self,str_app='´ò¿ª',control_class='Button',control_name='´ò¿ª(&O)'): time.sleep(1) result = False print "*********Find_Gui_button function**********" #self.Mousepos_print() #print 'control_name:',str_app,',',control_name hwnd = win32gui.FindWindow(None, str_app) if hwnd < 1: hwnd = self.find_main_window(str_app) #print 'hwnd :',hwnd,str_app win32gui.SetForegroundWindow(hwnd) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) #print 'comboHwnd',comboHwnd while comboHwnd: print "control_class:",comboHwnd,control_name cla = win32gui.GetClassName(comboHwnd) print 'cla:' ,cla bufLen=1024 buf =win32gui.PyMakeBuffer(bufLen) if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] print 'buttonname ,len,n,find :',str,len(control_name ),n,str.find(control_name) if (len(control_name ) ==0 and n==0) or ((len(control_name )>0 and str.find(control_name)>-1 )): #win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONDOWN,0) time.sleep(0.05) #win32gui.SendMessage(comboHwnd,win32con.WM_LBUTTONUP,0) win32gui.PostMessage(comboHwnd,win32con.WM_LBUTTONUP,0) time.sleep(0.05) #win32gui.SendMessage(win32con.WM_CLOSE,0,0) #print '111' result = True break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return result
def Find_Gui_edit(self,str_app='´ò¿ª',control_class='ComboBox',filename='wtp_cuo1.pcap',control_name='',stop_flag='0'): print "*********Find_Gui_edit function**********",str_app time.sleep(1) #self.Mousepos_print() print 'str_app',str_app hwnd = win32gui.FindWindow(None, str_app) print 'hwnd',hwnd win32gui.SetForegroundWindow(hwnd) comboHwnd = win32gui.FindWindowEx(hwnd,0,control_class,None) bufLen=256 buf =win32gui.PyMakeBuffer(bufLen) while comboHwnd: if stop_flag=='1': win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break print "control_class:",comboHwnd cla = win32gui.GetClassName(comboHwnd) print cla if control_class in cla: n = win32gui.SendMessage(comboHwnd,win32con.WM_GETTEXT,bufLen,buf) str = buf[:n] if (len(control_name ) ==0 and n==0) or (len(control_name )>0 and str.find(control_name)>-1 ): win32gui.SendMessage(comboHwnd,win32con.WM_SETTEXT,bufLen,filename) time.sleep(1) break comboHwnd = win32gui.GetWindow(comboHwnd,win32con.GW_HWNDNEXT) time.sleep(1) return True
def is_win_ok(self,hwnd,starttext,lb_dx='869',lb_dy='38',win_dx='',win_dy=''): #print "*********is_win_ok function**********" #print "*********is_win_ok function starttext**********",starttext if len(win_dx)>0: self.wdx = string.atoi(win_dx) if len(win_dy)>0: self.wdy = string.atoi(win_dy) s = win32gui.GetWindowText(hwnd) #print s if s.startswith(starttext): #print "*********is_win_ok function s**********",s #print (s) dlg=win32gui.FindWindow(None,s) time.sleep(1) #win32gui.ShowWindow(dlg,win32con.SW_SHOWMAXIMIZED) win32gui.ShowWindow(dlg,win32con.SW_SHOW) time.sleep(1) #print 'self.wdx,self.wdy:',self.wdx,self.wdy #win32gui.MoveWindow(dlg,0,0,self.wdx,self.wdy,1) time.sleep(1) win32gui.SetForegroundWindow(dlg) time.sleep(1) #win32gui.ShowWindow(dlg,win32con.SW_SHOWMAXIMIZED) win32gui.ShowWindow(dlg,win32con.SW_SHOW) time.sleep(1) #self.Mouse_LB(lb_dx,lb_dy) global MAIN_HWND MAIN_HWND = hwnd return None return 1
def startapp(self,path): print 'Open the pingtest software,path:',path os.startfile(path) self.sleep('2') hwd = win32gui.FindWindow(None,"PingTest") if hwd: win32gui.SetForegroundWindow(hwd) #ö return True
def find_wind(self,str_app): hwnd = win32gui.FindWindow(None, str_app) log_print(hwnd) if hwnd>0: #win32gui.SetForegroundWindow(hwnd) win32gui.MoveWindow(hwnd,40,40,840,640,1) time.sleep(1) win32gui.MoveWindow(hwnd,0,0,800,600,1) #win32gui.ShowWindow(hwnd,win32con.SW_SHOWMAXIMIZED) #win_GUI.win_gui().Mousepos_print('20') time.sleep(1) return True return False
def OnTaskbarNotify( self, hwnd, msg, wparam, lparam, ): if lparam == win32con.WM_LBUTTONUP: pass elif lparam == win32con.WM_LBUTTONDBLCLK: pass elif lparam == win32con.WM_RBUTTONUP: menu = win32gui.CreatePopupMenu() win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, 'Toggle Display') win32gui.AppendMenu(menu, win32con.MF_SEPARATOR, 0, '') if self.serverState == self.EnumServerState.STOPPED: win32gui.AppendMenu(menu, win32con.MF_STRING, 1024, 'Start Server') win32gui.AppendMenu(menu, win32con.MF_STRING | win32con.MF_GRAYED, 1025, 'Restart Server') win32gui.AppendMenu(menu, win32con.MF_STRING | win32con.MF_GRAYED, 1026, 'Stop Server') else: win32gui.AppendMenu(menu, win32con.MF_STRING | win32con.MF_GRAYED, 1024, 'Start Server') win32gui.AppendMenu(menu, win32con.MF_STRING, 1025, 'Restart Server') win32gui.AppendMenu(menu, win32con.MF_STRING, 1026, 'Stop Server') win32gui.AppendMenu(menu, win32con.MF_SEPARATOR, 0, '') win32gui.AppendMenu(menu, win32con.MF_STRING, 1027, 'Quit (pid:%i)' % os.getpid()) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu( menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None, ) win32api.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0) return 1
def FX_SetForegroundWindow(FXWindow): try: win32gui.SetForegroundWindow(FXWindow.GetWin32Window()) return True except: return False
def FX_ShowWindow(Win32Window): win32gui.SetForegroundWindow(Win32Window) win32gui.SetWindowPos(Win32Window, \ win32con.HWND_TOP, \ 0,0,0,0, \ win32con.SWP_SHOWWINDOW)
def FX_ShowWindowMax(Win32Window): win32gui.SetForegroundWindow(Win32Window) ## win32gui.SetWindowPos(Win32Window, \ ## win32con.HWND_TOP, \ ## 0,0,0,0, \ ## win32con.SWP_SHOWWINDOW) win32gui.ShowWindow(Win32Window, win32con.SW_MAXIMIZE)
def init_window(self, hwnd=None, pos=None, borderless=False, config = None): """ At the moment only sets the window in the foreground and moves it to a posistion set in the config. Args: hwnd (int): the window handle to "initialize". If not supplied the hwnd from the last get_hwnd_by_title will be used pos (tuple): a tuple describing the X,Y,Height and Width of the window borderless (Bool): Removes extra styling like borders if true. Can be reapplied with: hide_extra_ui(hwnd,remove=False) Returns: If the window was brought to the foreground, the return value is nonzero. If the window was not brought to the foreground, the return value is zero. """ if hwnd is None: hwnd = self.get_hwnd() logging.debug("Init window 0x{:2x}".format(hwnd)) if borderless: self.hide_extra_ui() if pos is not None: self.move(pos, hwnd) return win32gui.SetForegroundWindow(hwnd)
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)