如何让窗口专注于 Windows 并重新调整它的大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2335721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:01:44  来源:igfitidea点击:

How can I get the window focused on Windows and re-size it?

pythonwindows

提问by Shady

I want to get the focused window so I can resize it... how can I do it?

我想获得焦点窗口,以便调整其大小...我该怎么做?

回答by Brian R. Bondy

Use the GetForegroundWindowWin32 API to get the window handle.

使用GetForegroundWindowWin32 API 获取窗口句柄。

Then use the MoveWindow(or SetWindowPosif you prefer) win32 API to resize the window.

然后使用MoveWindow(或SetWindowPos,如果您愿意)win32 API 来调整窗口大小。

Working with the Win32 API can be done directly with ctypes and working with the dlls or by using the pywin32project.

使用 Win32 API 可以直接使用 ctypes 并使用 dll 或使用pywin32项目来完成。

Edit:Sure here is an example (Make sure you have pywin32 installed):

编辑:当然这是一个例子(确保你已经安装了 pywin32):

import win32gui
hwnd = win32gui.GetForegroundWindow()
win32gui.MoveWindow(hwnd, 0, 0, 500, 500, True)