[python-win32] Bringing a window to the front.

Greg Landrum greg.landrum at gmail.com
Wed Feb 8 18:32:59 CET 2006


Mark Mc Mahon sent me personal email with the answer that I ought to
be using SetForegroundWindow. For this I needed a window handle, and
for that I think I needed a window name. Since I can't be sure of the
full window name (it changes based on the active document), I wrote
the following function that uses a regexp to match the names of all
active windows:

#------------------------------------------------------------
def RaiseWindowNamed(nameRe):
  import win32gui
  # start by getting a list of all the windows:
  cb = lambda x,y: y.append(x)
  wins = []
  win32gui.EnumWindows(cb,wins)

  # now check to see if any match our regexp:
  tgtWin = -1
  for win in wins:
    txt = win32gui.GetWindowText(win)
    if nameRe.match(txt):
      tgtWin=win
      break

  if tgtWin>=0:
    win32gui.SetForegroundWindow(tgtWin)
#------------------------------------------------------------

This seems to do what I want it to do.

Thanks Mark!

-greg


More information about the Python-win32 mailing list