如何将应用程序作为最顶层的窗口。我在excel中打开了两个excel文件。我关闭第一个文件,然后重新打开它。但我希望第二个文件成为最顶层的窗口。如何在不关闭第二个文件并重新打开的情况下完成该操作。我只需要在win32com中调度即可。
import time, os.path, os
from win32com.client import Dispatch
path1 = 'C:\\Todolist.xls'
path2 = 'C:\\Todolist2.xlsx'
xla = Dispatch("Excel.Application")
xla.DisplayAlerts = False
xla.Visible = True
xl = Dispatch("Excel.Application")
xl.DisplayAlerts = False
xl.Visible = True
wb1= xla.Workbooks.Open(Filename=path1)
wb2= xl.Workbooks.Open(Filename=path2)
wb1.Close()
time.sleep(3)
wb1= xl.Workbooks.Open(Filename=path1)
#Need to bring wb2 back as the top most window
答案 0 :(得分:0)
试试这个
import win32gui
def windowEnumerationHandler(hwnd, windows):
windows.append((hwnd, win32gui.GetWindowText(hwnd)))
windows = []
win32gui.EnumWindows(windowEnumerationHandler, windows)
for i in windows:
if "window_heading" in i[1].lower():
win32gui.ShowWindow(i[0],5)
win32gui.SetForegroundWindow(i[0])
break