如何确保应用程序窗口始终位于顶部? [英] How can I ensure that the application windows is always on top?

查看:54
本文介绍了如何确保应用程序窗口始终位于顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在控制台窗口中运行的简单 Python 脚本.

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?

推荐答案

要使用 cmd 窗口执行此操作,您可能必须调用大量 win32 调用.

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

  1. 使用 win32gui.EnumWindows 枚举所有窗口以获取窗口句柄
  2. 找到窗口标题"这与您运行程序的方式相匹配.例如,双击我系统上的 .py 文件,窗口标题为C:\Python26\python.exe".在命令行上运行它,它被称为 c:\Windows\system32\cmd.exe - c:\python26\python.exe test.py
  3. 使用适当的标题获取 cmd 窗口句柄.
  4. 使用 win32gui.SetWindowPos 制作你的窗户是最顶层的"窗口等...
  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

这篇关于如何确保应用程序窗口始终位于顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆