Python win32gui.SetForegroundWindow() Examples

The following are 29 code examples for showing how to use win32gui.SetForegroundWindow(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may want to check out the right sidebar which shows the related API usage.

You may also want to check out all available functions/classes of the module win32gui , or try the search function .

Example 1
Project: ironpython2   Author: IronLanguages   File: win32gui_taskbar.py    License: Apache License 2.0 6 votes vote down vote up
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 
Example 2
Project: LIFX-Control-Panel   Author: samclane   File: SysTrayIcon.py    License: MIT License 6 votes vote down vote up
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) 
Example 3
Project: peach   Author: MozillaSecurity   File: process.py    License: Mozilla Public License 2.0 6 votes vote down vote up
def enumCallback(hwnd, self):
        title = win32gui.GetWindowText(hwnd)

        if title.find(self.windowName) > -1:
            try:
                win32gui.SetActiveWindow(hwnd)
                win32gui.SetForegroundWindow(hwnd)

                FInputs = Input * 1
                extra = c_ulong(0)
                ii_ = Input_I()
                ii_.ki = KeyBdInput(win32con.VK_F5, 0x3f, 0, 0, pointer(extra))
                x = FInputs(( 1, ii_ ))
                windll.user32.SendInput(1, pointer(x), sizeof(x[0]))

            except:
                pass

        return True 
Example 4
Project: MouseTracks   Author: Peter92   File: main.py    License: GNU General Public License v3.0 6 votes vote down vote up
def bring_to_front(self, new=True):
        """Bring a window into focus.
        Kept the old way just to be on the safe side.
        """
        if new:
            win32gui.ShowWindow(self.hwnd, True)
        else:
            self.restore()
            
        #Sometimes it seems to fail but then work a second time
        try:
            win32gui.SetForegroundWindow(self.hwnd)
        except pywintypes.error:
            time.sleep(0.5)
            win32gui.ShowWindow(self.hwnd, True)
            try:
                win32gui.SetForegroundWindow(self.hwnd)
            except pywintypes.error:
                pass 
Example 5
Project: MouseTracks   Author: Peter92   File: tray.py    License: GNU General Public License v3.0 6 votes vote down vote up
def show_menu(self):
        """Draw the popup menu."""
        menu = win32gui.CreatePopupMenu()
        self._create_menu(menu, self.menu_options)
        
        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)
        self.logger.debug('Menu displayed.') 
Example 6
Project: pyrexecd   Author: euske   File: __init__.py    License: MIT License 6 votes vote down vote up
def _notify(klass, hwnd, msg, wparam, lparam):
        self = klass._instance[hwnd]
        if lparam == win32con.WM_LBUTTONDBLCLK:
            menu = self.get_popup()
            wid = win32gui.GetMenuDefaultItem(menu, 0, 0)
            if 0 < wid:
                win32gui.PostMessage(hwnd, win32con.WM_COMMAND, wid, 0)
        elif lparam == win32con.WM_RBUTTONUP:
            menu = self.get_popup()
            pos = win32gui.GetCursorPos()
            win32gui.SetForegroundWindow(hwnd)
            win32gui.TrackPopupMenu(
                menu, win32con.TPM_LEFTALIGN,
                pos[0], pos[1], 0, hwnd, None)
            win32gui.PostMessage(hwnd, win32con.WM_NULL, 0, 0)
        elif lparam == win32con.WM_LBUTTONUP:
            pass
        return True 
Example 7
Project: OpenBazaar-Installer   Author: OpenBazaar   File: systray.py    License: MIT License 6 votes vote down vote up
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) 
Example 8
Project: ComicStreamer   Author: beville   File: gui_win.py    License: Apache License 2.0 6 votes vote down vote up
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) 
Example 9
Project: gui-o-matic   Author: mailpile   File: winapi.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def _show_menu( self ):
        menu = win32gui.CreatePopupMenu()
        for action in self.menu_actions:
            if action:
                flags = win32con.MF_STRING
                if not action.sensitive:
                    flags |= win32con.MF_GRAYED
                win32gui.AppendMenu( menu, flags, action.get_id(), action.label )
            else:
                win32gui.AppendMenu( menu, win32con.MF_SEPARATOR, 0, '' )
        
        pos = win32gui.GetCursorPos()
        
        win32gui.SetForegroundWindow( self.window_handle )
        win32gui.TrackPopupMenu( menu,
                                 win32con.TPM_LEFTALIGN | win32con.TPM_BOTTOMALIGN,
                                 pos[ 0 ],
                                 pos[ 1 ],
                                 0,
                                 self.window_handle,
                                 None )
        win32gui.PostMessage( self.window_handle, win32con.WM_NULL, 0, 0 ) 
Example 10
Project: roc   Author: Sunuba   File: Clicker.py    License: MIT License 5 votes vote down vote up
def find_window_movetop(cls):
        hwnd = win32gui.FindWindow(None, cls.processname)
        win32gui.ShowWindow(hwnd,5)
        win32gui.SetForegroundWindow(hwnd)
        rect = win32gui.GetWindowRect(hwnd)
        sleep(0.2)
        return rect 
Example 11
Project: airtest   Author: NetEase   File: windows.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def snapshot(self, filename=None ):
        ''' Capture device screen '''
        range_ = self._range()
        win32gui.SetForegroundWindow(self.HWND)
        time.sleep(0.1)
        pic = ImageGrab.grab(range_)
        if filename !=None:
            pic.save(filename)
        return pic 
Example 12
Project: pyautotrade_tdx   Author: ynzheng   File: winguiauto.py    License: GNU General Public License v2.0 5 votes vote down vote up
def focusWindow(hwnd):
    '''
    捕捉窗口焦点
    :param hwnd: 窗体句柄
    :return:
    '''
    win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED)
    win32gui.SetForegroundWindow(hwnd) 
Example 13
Project: PUBG   Author: xulusjb   File: main.py    License: The Unlicense 5 votes vote down vote up
def enum_window_callback(hwnd, pid):
    tid, current_pid = win32process.GetWindowThreadProcessId(hwnd)
    if pid == current_pid and win32gui.IsWindowVisible(hwnd):
        win32gui.SetForegroundWindow(hwnd)
        l("window activated") 
Example 14
Project: pymiere   Author: qmasingarbe   File: demo_ui.py    License: GNU General Public License v3.0 5 votes vote down vote up
def swap_focus(func):
    """
    Use this decorator for any method that does an action in premiere. Not needed for queries but for any action
    that will change something in the UI we have to give focus to Premiere
    """
    def wrapper(self, *args, **kwargs):
        win32gui.SetForegroundWindow(self.premiere_window_id)
        result = func(self, *args, **kwargs)
        win32gui.SetForegroundWindow(self.ui_id)
        return result
    return wrapper 
Example 15
Project: bot   Author: maaaxim   File: functions.py    License: MIT License 5 votes vote down vote up
def set_window_coordinates(hwnd, window_info):
    if win32gui.IsWindowVisible(hwnd):
        if WINDOW_SUBSTRING in win32gui.GetWindowText(hwnd):
            rect = win32gui.GetWindowRect(hwnd)
            x = rect[0]
            y = rect[1]
            w = rect[2] - x
            h = rect[3] - y
            window_info['x'] = x
            window_info['y'] = y
            window_info['width'] = w
            window_info['height'] = h
            window_info['name'] = win32gui.GetWindowText(hwnd)
            win32gui.SetForegroundWindow(hwnd) 
Example 16
Project: PyAutoTrading   Author: bluestinger   File: winguiauto.py    License: GNU General Public License v2.0 5 votes vote down vote up
def focusWindow(hwnd):
    '''
    捕捉窗口焦点
    :param hwnd: 窗体句柄
    :return:
    '''
    win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED)
    win32gui.SetForegroundWindow(hwnd) 
Example 17
Project: Auto-Lianliankan   Author: TheThreeDog   File: run.py    License: Apache License 2.0 5 votes vote down vote up
def getGameWindowPosition():
    # FindWindow(lpClassName=None, lpWindowName=None)  窗口类名 窗口标题名
    window = win32gui.FindWindow(None,WINDOW_TITLE)
    # 没有定位到游戏窗体
    while not window:
        print('定位游戏窗体失败,5秒后重试...')
        time.sleep(5)
        window = win32gui.FindWindow(None,WINDOW_TITLE)
    # 定位到游戏窗体
    win32gui.SetForegroundWindow(window) # 将窗体顶置
    pos = win32gui.GetWindowRect(window)
    print("定位到游戏窗体:" + str(pos))
    return (pos[0],pos[1])

# 获取一张完整的屏幕截图 
Example 18
Project: ATX   Author: NetEaseGame   File: windows.py    License: Apache License 2.0 5 votes vote down vote up
def set_foreground(self):
        win32gui.SetForegroundWindow(self.hwnd) 
Example 19
Project: ATX   Author: NetEaseGame   File: windows.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 20
Project: fame_modules   Author: certsocietegenerale   File: cutthecrap.py    License: GNU General Public License v3.0 5 votes vote down vote up
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 
Example 21
Project: pyautotrade_tdx   Author: wangdkchina   File: winguiauto.py    License: GNU General Public License v2.0 5 votes vote down vote up
def focusWindow(hwnd):
    '''
    捕捉窗口焦点
    :param hwnd: 窗体句柄
    :return:
    '''
    win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED)
    win32gui.SetForegroundWindow(hwnd) 
Example 22
Project: pyAutoTrading   Author: drongh   File: winguiauto.py    License: GNU General Public License v2.0 5 votes vote down vote up
def restoreFocusWindow(hwnd):
    win32gui.ShowWindow(hwnd, win32con.SW_RESTORE)
    win32gui.SetForegroundWindow(hwnd)
    time.sleep(0.2) 
Example 23
Project: pyAutoTrading   Author: drongh   File: winguiauto.py    License: GNU General Public License v2.0 5 votes vote down vote up
def focusWindow(hwnd):
    """
    捕捉窗口焦点
    :param hwnd: 窗体句柄
    :return:
    """
    win32gui.ShowWindow(hwnd, win32con.SW_SHOWMAXIMIZED)
    win32gui.SetForegroundWindow(hwnd) 
Example 24
Project: eavatar-me   Author: eavatar   File: console.py    License: Apache License 2.0 5 votes vote down vote up
def bring_to_top(self):
        if not self.hwnd:
            return

        # win32gui.BringWindowToTop(self.hwnd)
        win32gui.SetForegroundWindow(self.hwnd) 
Example 25
Project: eavatar-me   Author: eavatar   File: notice_dlg.py    License: Apache License 2.0 5 votes vote down vote up
def bring_to_top(self):
        if not self.hwnd:
            return

        # win32gui.BringWindowToTop(self.hwnd)
        win32gui.SetForegroundWindow(self.hwnd) 
Example 26
Project: eavatar-me   Author: eavatar   File: shell.py    License: Apache License 2.0 5 votes vote down vote up
def show_menu(self):
        menu = win32gui.CreatePopupMenu()
        self.create_menu(menu)

        pos = win32gui.GetCursorPos()
        win32gui.SetForegroundWindow(self.shell.main_frame.hwnd)
        win32gui.TrackPopupMenu(menu,
                                win32con.TPM_LEFTALIGN,
                                pos[0],
                                pos[1],
                                0,
                                self.shell.main_frame.hwnd,
                                None) 
Example 27
Project: gui-o-matic   Author: mailpile   File: winapi.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def focus( self ):
        win32gui.SetForegroundWindow( self.window_handle ) 
Example 28
Project: ironpython2   Author: IronLanguages   File: desktopmanager.py    License: Apache License 2.0 4 votes vote down vote up
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) 
Example 29
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