Hi只想自定义运行窗口的大小,但是正在运行的窗口具有"静态"大小,无法使用鼠标调整大小使用python是否可以解决此问题?
当我在这里搜索主题时,尝试使用这个import win32gui hwnd = win32gui.FindWindow(None, 'Window Title') x0, y0, x1, y1 = win32gui.GetWindowRect(hwnd) w = x1 - x0 h = y1 - y0 win32gui.MoveWindow(hwnd, x0, y0, w+100, h+100, True)
*但是,我收到这个特定的错误"pywintypes.error:(5,"MoveWindow","访问被拒绝。"(">
要从Python访问窗口,请使用以下步骤。
- 使用
win32gui.EnumWindows
查找具有特定标题的窗口 - 调用
win32.Dispatch
在桌面上设置焦点 - 使用
SendKeys('%')
启动窗口搜索 - 使用
win32gui
函数修改窗口属性
尝试此代码来调整窗口大小:
import win32com.client as win32
import win32gui
title = "Untitled - Notepad2" # find first window with this title
def windowEnumerationHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
top_windows = [] # all open windows
win32gui.EnumWindows(windowEnumerationHandler, top_windows)
winlst = [] # windows to cycle through
for i in top_windows: # all open windows
if i[1] == title:
winlst.append(i)
hwnd = winlst[0][0] # first window with title, get hwnd id
shell = win32.Dispatch("WScript.Shell") # set focus on desktop
shell.SendKeys('%') # Alt key, send key
rect = win32gui.GetWindowRect(hwnd)
x0, y0, x1, y1 = win32gui.GetWindowRect(hwnd)
w = x1 - x0
h = y1 - y0
win32gui.MoveWindow(hwnd, x0, y0, w+100, h+100, True)