Kelv1nG
Kelv1nG

Reputation: 67

Resizing window size of current windows application

Hi just want to customize the size of the running window, however running window has a 'static' size, cant be resized using mouse is there a work around with this using python?

tried using this from when i searched for a topic here 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)

*however im receiving this particular error 'pywintypes.error: (5, 'MoveWindow', 'Access is denied.')'

Upvotes: 0

Views: 801

Answers (1)

Mike67
Mike67

Reputation: 11342

To access a window from Python, use these steps.

  • Use win32gui.EnumWindows to find windows with a specific title
  • Call win32.Dispatch to set focus on the desktop
  • Use SendKeys('%') to start the window search
  • Use a win32gui function to modify a window property

Try this code to resize a window:

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)

Upvotes: 1

Related Questions