Centering a window on the screen

This lesson demonstrates how to center a window on the screen.

The centerMe command

The centerMe command centers a stack on the screen.

You might call it in a stack's preOpenStack handler: the stack then would automatically center itself whenever it opens.  You might call it in a deskTopChanged handler so the stack would automatically center itself whenever the user changes the size of the desktop, e.g. by switching to a new monitor resolution setting.

This is the simplest of handlers: it has no parameters and only one statement, which changes the stack's location property. The word loc is a LiveCode synonym for location.

The location property

A stack's location is its center point. The handler moves the stack window so its center is at the center of the screen. The screenLoc function refers to the main screen; if the computer has more than one monitor, the screenLoc is the center of the main monitor. The main monitor is defined depending on the operating system: it may be the monitor that contains the menu bar, task bar, etc.

on centerMe
	set the loc of this stack to the screenLoc
end centerMe

A variation

The expression this stack refers to the current stack. One simple variation on this handler is to provide it with a stack name, and to center the specified stack instead of the current stack. Such a handler looks like this:

on centerMe theStackName
	set the loc of stack theStackName to the screenLoc
end centerMe

You call this handler with a statement like one of the following:

centerMe "My Stack"
centerMe (the short name of stack ID 1620)
centerMe the short name of this stack


Add your comment

E-Mail me when someone replies to this comment