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

1 Comments

rob

so for an example, it works like this:

this is in your card's script (not the stack, like the example says, because the event, "preOpenStack" is part of the card (not really that obvious).

So in your card script:

-- add these:
on preOpenStack
centerMe
end preOpenStack

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

When you 'run' the stack in LiveCode, you won't see this take effect because the stack/card is already open. If you either close and reopen the stack, or compile it and run it, it should work.

Add your comment

E-Mail me when someone replies to this comment