Module Variables
Module variables in Python might look like other languages' global variables, but in Python every name at the module level is scoped by the module name, so there are no global variables. The
default_targets module variable defined below contains the list of windows that WindowMover can move from one display to another:
# All windows on this list will be kept on
# the primary display or moved to the secondary
# display based on the 'keep' parameter below
default_targets = []
The
keep variable determines whether the target windows should be moved or whether they should remain in their original display while all
other windows get moved. In some testing situations it helps to switch back and forth:
# determine if the targets on the primary display
# should be kept there or moved to the secondary display.
keep = True
The
check_delay variable defined below controls the delay between successive iterations. Finding the right delay is very important; a delay that's too short will negatively impact system performance, because it will use up resources by constantly checking every window. A delay that's too long lets undesirable windows show up on the wrong display without moving them to the other display quickly enough:
# Determine how often to check for targets (in seconds).
# As this number approaches zero program responsiveness
# improves (windows move almost immediately to the other
# display) but it can also hog the CPU (the computer
# won't be able to do much else)
check_delay = 0.1 # in seconds
The
height_difference variable helps to adjust the location of a window moving between displays with different resolutions:
# This parameter is very important when resolutions
# of the two displays are different.
# If the primary display is taller than the secondary,
# then the top of the secondary display doesn't start at 0!
# The top of the secondary will actually be the difference
# in heights.
height_difference = 177
It turns out that
height_difference works in a very unintuitive way; you'll see more about that later.
You need to get the actual dimensions of the stretched desktop (across both displays), which you can do using two Windows API functions.
GetDesktopWindow() returns the window handle of the desktop itself (which is also a window), and
GetWindowRect() gets the left/top coordinate (always
0, 0 for the desktop) and the width/height:
monitor_width, monitor_height =
GetWindowRect(GetDesktopWindow())[2:]
The main() Function
The
main() function launches the MonitorThread and determines whether it should run forever, or only for the specified number of seconds:
def main(seconds=10.0):
print 'Monitor dimensions:',
(monitor_width, monitor_height)
print
t = MonitorThread(verbose=False)
t.start()
if seconds == 0.0:
# run forever
while (True):
time.sleep(5.0)
time.sleep(seconds)
t.stop()
t.join()
The
main() function accepts the number of seconds to run (10 seconds by default). If the parameter is
0 the program goes into an infinite loop, otherwise it sleeps for the specified number of seconds, letting the monitor thread do its thing, and eventually stops the thread.