Search code examples
pythonwindowssubprocessresize

How to programmatically resize process windows in Windows 10?


I'm using python to open 4 videos at once with vlc and I want them to be resized automatically in each quarter of the screen (as it happens when you drag the window in the corner).

I'm using the Popen method of the subprocess library as follows:

for i in range(0,4):
    p = subprocess.Popen(['vlc location','file name'])
p.wait()

So far the videos open but I can't figure it out how to pin them in the corners like this:

screenshot of desired results


Solution

  • If you are going to use the Popen subprocess method as I did you will have to remove p.wait() because it will wait for the video to end before running anymore code (it's putting the processes in a queue instead of threading them).

    With the help from martineau and the answer he provided I used the following (after installing the pywin32 for python 3.5):

    import pywintypes
    import win32gui
    
    displays = [[-10,0,980,530],
            [954,0,980,530],
            [-10,515,980,530],
            [954,515,980,530]] #these are the x1,y1,x2,y2 to corner all 4 videos on my res (1920x1080)
    
    def enumHandler(hwnd, lParam):
        if win32gui.IsWindowVisible(hwnd):
            print(win32gui.GetWindowText(hwnd)) #this will print all the processes title
            if name in win32gui.GetWindowText(hwnd): #it checks if the process I'm looking for is running
                win32gui.MoveWindow(hwnd,i0,i1,i2,i3,True) #resizes and moves the process
    
    win32gui.EnumWindows(enumHandler, None) #this is how to run enumHandler
    

    The x1,y1,x2,y2 might be different for your processes but these work just fine for vlc media player. I hope I was clear enough but if you don't get it done you should definitely check the answer provided by martineau.