How can I ensure that the application windows is always on top?

后端 未结 3 876
旧巷少年郎
旧巷少年郎 2020-12-09 14:09

I have a simple Python script that runs in a console windows.

How can I ensure that the console window is always on top and if possible resize it?

3条回答
  •  半阙折子戏
    2020-12-09 14:50

    To do this with the cmd window, you'll probably have to invoke a lot of win32 calls.

    1. Enumerate all the windows using win32gui.EnumWindows to get the window handles
    2. Find the "window title" that matches how you run your program. For example, doubling clicking on a .py file on my system the window title is "C:\Python26\python.exe". Running it on a command line, it is called c:\Windows\system32\cmd.exe - c:\python26\python.exe test.py
    3. Using the appropriate title get the cmd window handle.
    4. Using win32gui.SetWindowPos make your window a "top-most" window, etc...

    import win32gui, win32process, win32con
    import os
    
    windowList = []
    win32gui.EnumWindows(lambda hwnd, windowList: windowList.append((win32gui.GetWindowText(hwnd),hwnd)), windowList)
    cmdWindow = [i for i in windowList if "c:\python26\python.exe" in i[0].lower()]
    win32gui.SetWindowPos(cmdWindow[0][1],win32con.HWND_TOPMOST,0,0,100,100,0) #100,100 is the size of the window
    

提交回复
热议问题