windows 如何使用 Python 最大化特定窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2790825/
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
How can I maximize a specific window with Python?
提问by Bruno 'Shady'
I'm trying to maximize a specific window with python...
我正在尝试使用 python 最大化特定窗口...
Here is the deal: I have a script that opens 2 firefox windows (selenium rc), and I need to maximize the second window, the last one that opens... How can I do it?
这是交易:我有一个脚本可以打开 2 个 Firefox 窗口(selenium rc),我需要最大化第二个窗口,最后一个打开的窗口......我该怎么做?
I'm using this command
我正在使用这个命令
window = win32gui.GetForegroundWindow()
win32gui.MoveWindow(window, 0, 0, 1440, 900, True)
that works perfectly, but only with the focus window... and the second window of firefox witch opens with the script doesnt get focused...
这完美地工作,但仅适用于焦点窗口......并且firefox女巫的第二个窗口打开时脚本没有得到焦点......
回答by ars
This should work
这应该工作
import win32gui, win32con
hwnd = win32gui.GetForegroundWindow()
win32gui.ShowWindow(hwnd, win32con.SW_MAXIMIZE)
回答by Simon
If you don't want to install separate modules you could use the inbuilt ctypesmodule. The usage is not that different from the accepted answer above, except that you interact with the DLLs themselves and don't have to install anything else.
如果您不想安装单独的模块,您可以使用内置的ctypes模块。用法与上面接受的答案没有什么不同,除了您与 DLL 本身交互并且不必安装任何其他东西。
First here is the code:
首先是代码:
import ctypes
user32 = ctypes.WinDLL('user32')
SW_MAXIMISE = 3
hWnd = user32.GetForegroundWindow()
user32.ShowWindow(hWnd, SW_MAXIMISE)
Now the explanation:
现在解释:
- Get ctypes module.
- Get the appropriate runtime, for reference use the Windows documentationand look under "Requirements".
- Set the
SW_MAXIMISE
value to 3 since this is the value (indicated in the documentation) to set the window to maximum. hWnd = user32.GetForegroundWindow()
retrieves the foreground window (the window that is in front of all the others) - see herefor the complete description on the function.- Use
ShowWindow()
to control the windows show state. This takes two arguments, the handle to the window (defined above ashWnd
) and how the window should be seen (set as 3 inSW_MAXIMISE = 3
). You can see the documentationfor a more complete list of the various options.
- 获取 ctypes 模块。
- 获取适当的运行时,参考使用Windows 文档并查看“要求”。
- 将
SW_MAXIMISE
值设置为 3,因为这是将窗口设置为最大值的值(在文档中指明)。 hWnd = user32.GetForegroundWindow()
检索前景窗口(在所有其他窗口之前的窗口) -有关该功能的完整说明,请参见此处。- 使用
ShowWindow()
控制窗口显示状态。这需要两个参数,窗口的句柄(上面定义为hWnd
)和窗口的显示方式(设置为 3 inSW_MAXIMISE = 3
)。您可以查看文档以获取更完整的各种选项列表。
You could of course put this into a function to make it easy to use.
您当然可以将其放入一个函数中以使其易于使用。
Another approach:
另一种方法:
Since in this case there are no worries about being cross platform, you could build a C or C++ extension instead.
由于在这种情况下不必担心跨平台,您可以改为构建 C 或 C++ 扩展。
Benefits:
好处:
- no ctypes (which is sometimes worth considering, see the answers to this question)
Downfalls:
垮台:
- needs to be compiled (since it's on Windows only, you only need to worry about compiling for x32 bit and x64 bit)
- mustbe a module (i.e. you can't intergrate it in one file)
- requires a minimum knowlege of either C or C++ as well as the Python api itself
- 需要编译(因为它只在 Windows 上,你只需要担心编译 x32 位和 x64 位)
- 必须是一个模块(即你不能将它集成到一个文件中)
- 需要最少的 C 或 C++ 知识以及 Python api 本身
The actual function to be called should not be that difficult:
要调用的实际函数应该没有那么难:
static PyObject * max_win(PyObject *self, PyObject *args) {
ShowWindow(GetForegroundWindow(), SW_MAXIMISE);
return Py_BuildValue(""); // Return nothing
}
Note that this is only a fragment of the actual code needed
请注意,这只是所需实际代码的一个片段