[ACCEPTED]-Get other running processes window sizes in Python-pywin32
Using hints from WindowMover article and Nattee Niparnan's blog post I managed to create 3 this:
import win32con
import win32gui
def isRealWindow(hWnd):
'''Return True iff given window is a real Windows application window.'''
if not win32gui.IsWindowVisible(hWnd):
return False
if win32gui.GetParent(hWnd) != 0:
return False
hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0
lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE)
if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner)
or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)):
if win32gui.GetWindowText(hWnd):
return True
return False
def getWindowSizes():
'''
Return a list of tuples (handler, (width, height)) for each real window.
'''
def callback(hWnd, windows):
if not isRealWindow(hWnd):
return
rect = win32gui.GetWindowRect(hWnd)
windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1])))
windows = []
win32gui.EnumWindows(callback, windows)
return windows
for win in getWindowSizes():
print win
You need the Win32 Extensions for Python module for this to work.
EDIT: I 2 discovered that GetWindowRect
gives more correct results 1 than GetClientRect
. Source has been updated.
I'm a big fan of AutoIt. They have a COM version 2 which allows you to use most of their functions 1 from Python.
import win32com.client
oAutoItX = win32com.client.Dispatch( "AutoItX3.Control" )
oAutoItX.Opt("WinTitleMatchMode", 2) #Match text anywhere in a window title
width = oAutoItX.WinGetClientSizeWidth("Firefox")
height = oAutoItX.WinGetClientSizeHeight("Firefox")
print width, height
Check out the win32gui
module in the Windows extensions 2 for Python. It may provide some of the functionality 1 you're looking for.
I updated the GREAT @DZinX code adding the 1 title/text of the windows:
import win32con
import win32gui
def isRealWindow(hWnd):
#'''Return True iff given window is a real Windows application window.'''
if not win32gui.IsWindowVisible(hWnd):
return False
if win32gui.GetParent(hWnd) != 0:
return False
hasNoOwner = win32gui.GetWindow(hWnd, win32con.GW_OWNER) == 0
lExStyle = win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE)
if (((lExStyle & win32con.WS_EX_TOOLWINDOW) == 0 and hasNoOwner)
or ((lExStyle & win32con.WS_EX_APPWINDOW != 0) and not hasNoOwner)):
if win32gui.GetWindowText(hWnd):
return True
return False
def getWindowSizes():
Return a list of tuples (handler, (width, height)) for each real window.
'''
def callback(hWnd, windows):
if not isRealWindow(hWnd):
return
rect = win32gui.GetWindowRect(hWnd)
text = win32gui.GetWindowText(hWnd)
windows.append((hWnd, (rect[2] - rect[0], rect[3] - rect[1]), text ))
windows = []
win32gui.EnumWindows(callback, windows)
return windows
for win in getWindowSizes():
print(win)
I Modify someone code ,
This well help to 1 run other application and get PID ,
import win32process
import subprocess
import win32gui
import time
def get_hwnds_for_pid (pid):
def callback (hwnd, hwnds):
if win32gui.IsWindowVisible (hwnd) and win32gui.IsWindowEnabled (hwnd):
_, found_pid = win32process.GetWindowThreadProcessId (hwnd)
if found_pid == pid:
hwnds.append (hwnd)
return True
hwnds = []
win32gui.EnumWindows (callback, hwnds)
return hwnds
# This the process I want to get windows size.
notepad = subprocess.Popen ([r"C:\\Users\\dniwa\\Adb\\scrcpy.exe"])
time.sleep (2.0)
while True:
for hwnd in get_hwnds_for_pid (notepad.pid):
rect = win32gui.GetWindowRect(hwnd)
print(hwnd, "=>", win32gui.GetWindowText (hwnd))
# You need to test if your resolution really get exactly because mine is doesn't .
# I use . 16:9 Monitor , Calculate the percent using this calculations , , (x * .0204082) and (y * .0115774)
print((hwnd, (rect[2] - rect[0], rect[3] - rect[1])))
x = rect[2] - rect[0]
y = rect[3] - rect[1]
print(type(x), type(y))
time.sleep(1)
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.