3

I am running a program with a built-in Python interpreter. Periodically I want that program to be able to go "full screen" or be minimized.

This will be running on Windows 7.

I am wondering if there is a way to do this in Python (so that I could call the function from my program). Using only standard libraries would be ideal but if there is a way to do this with an external module, that would be fine too.

Thanks.

Startec
  • 12,496
  • 23
  • 93
  • 160

1 Answers1

7

To minimize a window you need to know either the title of the window, or its window class. The window class is useful when the exact window title is not known. For example the following script shows two different ways to minimize the Microsoft Windows Notepad application assuming:

import ctypes

notepad_handle = ctypes.windll.user32.FindWindowW(None, "Untitled - Notepad")
ctypes.windll.user32.ShowWindow(notepad_handle, 6)

notepad_handle = ctypes.windll.user32.FindWindowW(u"Notepad", None) 
ctypes.windll.user32.ShowWindow(notepad_handle, 6)  

To determine the class name to use, you would need to use an tool such as Microsoft's Spy++. Obviously if Notepad was opened with a file, it would have a different title such as test.txt - Notepad. If this was the case, the first example would now fail to find the window, but the second example would still work.

If two copies of notepad were running, then only one would be closed. If all copies needed to be closed, you would need to enumerate all windows which requires more code.

The ShowWindow command can also be used to restore the Window. The 6 used here is the Windows code for SW_MINIMIZE.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97