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

7 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.

Trevix

Hello.
Which is the fasted method to find the center loc of each screen in a multiple screen system?
Should I just do the math of the working ScreenRects?
Thanks

Trevix

I forgot: the reason why I need the screen center of each of my multiple screen setup, is because I would like to center, trough code, a stack in one of my screen, at choice.

Heather Laine

Hello Trevix. Yes, you should use the working ScreenRects. This snippet of code should do the job:

repeat for each line tScreenRect in the screenRects
put (item 1 of tScreenRect + item 3 of tScreenRect) div 2, (item 2 of tScreenRect + item 4 of tScreenRect) div 2 & return after tScreenLocs
end repeat

Trevix

Mmmh... I have 3 monitors, with the one in the middle being the main monitor. The screenRects are:
0,25,2560,1707
-1920,435,0,1610
2560,-456,3712,1567
Your solution seems to work for the main monitor and the one on my right, but not for the one on my left (-1920,435,0,1610) wich strangely shows my stack in the middle of the main monitor.

Heather Laine

I think probably you should make a bug report at this point and we can investigate further.

Roberto

Sorry. I fixed the problem probably due to the difference of screen handling in the IDE or in the standalone

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.